Category: Java/C++ Runtime


Why does my application crash on startup?


In all likelihood, you have a bug in your application, either a runtime misconfiguration or a bug in your code. When you're starting out, there are some common mistakes that almost everyone makes, so don't feel too bad about it.

In addition, you're probably not handling all exceptions that you should be handling, therefore the first thing you should do is to surround all your code with a try/catch block:

    // all your code in main() goes here
catch( ::java::lang::Throwable & t )
    cerr << t.toString().to_chars() << endl;
catch( xmog_exception & xe )
    const char *  msg = xe.get_message_chars();

    if( msg )
        cerr << "XE: " << msg << endl;
        xmog_java_string::free( msg );

When you run your application again, hopefully it will only not work rather than crash. You might even get a good exception message out of this! Having such a top-level proxy exception handler in place in all threads on which you use proxy types is one of the top coding guidelines.

If you get a ClassNotFoundException, you have probably forgotten to configure the classpath to include your specific Java classes. If you're using explicit configuration, you should have something looking like this:

    loader.appendToClassPath( "../lib/myapp.jar" );
    loader.appendToClassPath( "../lib/myutil.jar" );

If you're using file-based configuration, you should have an element like this:

   <add key="AppendToClassPath" value="../lib/myapp.jar"/>   
   <add key="AppendToClassPath" value="../lib/myutil.jar"/>

The other top exception is usually due to you dereferencing a null object, i.e. a NullPointerException. The traditional problem is due to code such as this:

// *** don't do this *** // 
MyJavaType             myt = MyJavaType();
cout << myt.toString().to_chars() << endl; 

The problem is right there in the first line of code. You might read this line as an invocation of a Java default constructor through a proxy constructor. What you're really doing though is the following:

// *** don't do this *** // 
MyJavaType             myt = null;
cout << myt.toString().to_chars() << endl; 

The problem is that the C++ default constructor has to create a proxy instance that is initialized to null. What you really wanted to do in your code looks like this:

MyJavaType             myt( _use_java_ctor );
cout << myt.toString().to_chars() << endl; 

With this change, you are now invoking the Java default constructor as intended.

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

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