Category: Java/.NET Runtime

Q

Can I configure the classpath and other Java options in code?

A

Yes, let's start with the classpath because most everyone needs to configure this Java option. There are several ways you can configure the classpath in code. All of the configuration functions are declared by the IJvmLoader interface and its JvmLoader implementation. The simplest is via the ClassPath property, which takes a string as input. In addition to this function, there are two other functions to prepend or append a classpath root to a possibly already configured classpath, namely PrependToClassPath() and AppendToClassPath(). The following snippet demonstrates all of these functions:

IJvmLoader    loader = JvmLoader.GetJvmLoader();
 
// this setting would be useless, but it doesn't hurt ...
loader.ClassPath = "garbage";

// ... because this setting replaces the garbage from the first
loader.ClassPath = @".;..;..\..";

// this jar file will be appended to the already set classpath
// with the platform-specific path separator in-between
loader.AppendToClassPath( @"..\lib\myapp.jar" );

if( bDebug )
{
    // this jar file is prepended to the already set classpath
    // and therefore takes precedence over roots that come 
    // after it
    loader.PrependToClassPath( @"..\lib\myapp-debug.jar" );
}

We recommend that you always use the AppendTo or PrependTo variants rather than a direct set. By following this recommendation, you can use default values from configuration callbacks or files.

Other JVM options can be set explicitly via other functions that are declared (or inherited) by the JvmLoader class. The usage pattern is simple and always the same:

  1. acquire the singleton instance of JvmLoader.
    The factory method you're using determines whether a configuration file is read and other basic settings.
  2. start calling config API methods or setting properties.
  3. surround this code by a try/catch handler that catches JuggerNETExceptions because configuration callbacks (config hooks) might throw these exceptions at various points in the initialization process.

If you cannot find a set/get method for a specific -X option, you can use the indexed freeform configuration property DashXOption. This property takes a key and an optional value. The two are concatenated together and prefixed by "-X" to form the JVM configuration option.


Copyright 2006-2011 by Codemesh, Inc., ALL RIGHTS RESERVED

:
frequently asked questions
home products support customers partners newsroom about us contact us