JVM Initialization for .NET examples

The source code

This procedure checks an evironment variable called RUNOPTIONS and interprets its value as the name of a file which contains the configuration information for the JMS .NET example. In addition to JVM initialization settings like classpath, etc., this file is expected to contain some property definitions for JNDI lookup names. This scheme makes a lot of sense for our examples, because we need to be able to demonstrate JMS vendor independence and we want to demonstrate the ability to message between .NET and C++ JMS clients. For you, a less flexible and more easily maintained scheme might make more sense. You could for example hardcode all configuration information.

using System;
using System.IO;
using Codemesh.JuggerNET;
using java.lang;


public class Example
{
    ///
    /// @brief An example JVM initialization method.
    ///
    /// You can use different schemes for the loading of the JVM.  Please
    /// take a look at the rtconfig examples that illustrate several different
    /// ways (XML config file, etc.)
    ///
    /// This function first checks the environment variable 'RUNOPTIONS' for
    /// the name of a configuration file.  If that variable is undefined or
    /// the specified file cannot be found, we check the installation hierarchy
    /// for the default file 'runoptions.xml'.
    ///
    public static void    InitJvm( string[] args )
    {
        // try to get the configfile name from an environment variable
        string    RUNOPTIONS = Environment.GetEnvironmentVariable( "RUNOPTIONS" );

        if( RUNOPTIONS != null || !File.Exists( RUNOPTIONS ) )
            RUNOPTIONS = null;

        // if it is not available, use a relative path and (assuming we're in the 
        // install hierarchy)
        if( RUNOPTIONS == null )
        {
            RUNOPTIONS = @"..\..\runoptions.xml";

            // do a sanity check on the value and reset it if we can't find our
            // configuration file
            if( !File.Exists( RUNOPTIONS ) )
                RUNOPTIONS = null;
        }

        // if it is not available, use a relative path and (assuming we're in the 
        // install hierarchy)
        if( RUNOPTIONS == null )
        {
            RUNOPTIONS = @"..\runoptions.xml";

            // do a sanity check on the value and reset it if we can't find our
            // configuration file
            if( !File.Exists( RUNOPTIONS ) )
                RUNOPTIONS = null;
        }

        // one of the factory methods to acquire a JvmLoader instance for 
        // further customization.  You could also have used parametrized
        // versions to use a configuration file or set tracing levels.
        IJvmLoader         loader = JvmLoader.GetJvmLoader( RUNOPTIONS, 
                                                            true, 
                                                            true, 
                                                            TraceFacility.TraceAll, 
                                                            TraceLevel.TraceErrors );

        // this will print out the required LD_LIBRARY_PATH for Unix-style platforms
        // the pattern here is a double invocation:
        // 1) get the configured JVM, derive the required runtime path from it, and exit
        // 2) invoke again with the required runtime path and load the JVM
        // we have this code here to prepare for a future mono port, but you would
        // typically only use the second branch
        if( args.Length > 1 && "-info".Equals( args[ 1 ] ) )
            Environment.Exit( 1 );
        // this will throw an ArgumentException if something goes wrong
        // potentially, it might also throw a Java proxy exception if you
        // haven't granted the privileges required for the framework to
        // initialize itself
        else
            loader.Load();

        // try to find some classes that should be present to quickly diagnose 
        // configuration problems
        Class.forName( "javax.jms.Message" );
    }
}

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

:
jvm initialization for jms.net
home products support customers partners newsroom about us contact us