Category: Java/C++ 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 xmog_jvm_loader class. The simplest is via the setClassPath() method, 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:

xmog_jvm_loader &    loader = xmog_jvm_loader::get_jvm_loader();
 
// this setting would be useless, but it doesn't hurt ...
loader.setClassPath( "garbage" );

// ... because this setting replaces the garbage from the first
loader.setClassPath( ".;..;../.." );

// 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 don't have to worry about the correct path separator and 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 xmog_jvm_loader class. The usage pattern is simple and always the same:

  1. acquire a reference to the singleton instance of xmog_jvm_loader.
    The factory method you're using determines whether a configuration file is read and other basic settings.
  2. start calling config API methods.
  3. surround this code by a try/catch handler that catches xmog_exception & 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 freeform configuration method setDashXOption(). This method takes two string arguments, one of them optional. 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