I am new to Java and am currently trying to run the following c++ code using JNI.
I am running into trouble with the FindClass method.
Summary:
As a test, I created a simple class, TestingClass.class, placed it in ~/testclassdir,
and ran a c++ code to create a jvm environment and call the FindClass Method. When I run it in ~/testclassdir without setting the java.class.path option, it works fine (I'm assuming the default path is "."). However, if I try to set the java.class.path option on my own, I get a NoClassDefFound error. I have tried this on its own and setting the environment variable CLASSPATH, but neither works. Could someone please tell me what I am doing wrong with the class path settings?
Details: (Not sure how much to include here, so erred on the side of 'as much as possible')
The code that loads the jvm (TestingFindClass.cxx) is:
#include <jni.h> /* where everything is defined */
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_4;
/*Set the options*/
const int NOPTS(2);
JavaVMOption user_options[NOPTS];
string a("-verbose:class");
string b("-Djava.class.path=/disk02/home/bchristy/testclassdir ");
user_options[0].optionString= (char *)(a.c_str());
user_options[1].optionString= (char *)(b.c_str());
vm_args.options = user_options;
vm_args.nOptions = NOPTS;
vm_args.ignoreUnrecognized = JNI_TRUE;
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
/*Try to load the class*/
jclass test;
jthrowable e;
test = env->FindClass("TestingClass");
e = env->ExceptionOccurred();
if(e) {
cout<<"\n**********************exception found****************\n";
env->ExceptionDescribe();
}
else{
cout<<"Found class";
}
/* We are done. */
jvm->DestroyJavaVM();
return 0;
}
So you know, I did check for exceptions thrown in all the functions before the FindClass call, but nothing.
The class is:
/* this is a simple Java program */
class TestingClass {
// your program begins with a call to main()
public static void main(String args[]){
System.out.println("this is a simple Java program");
}
}
I compiled the class using javac TestingClass.java
Here's the output:
Without java.class.path option (within the testclassdir):
...
[Loaded java.security.PrivilegedActionException from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded sun.misc.URLClassPath$FileLoader from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded sun.misc.Resource from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded sun.misc.URLClassPath$FileLoader$1 from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.CodeSource from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.PermissionCollection from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.Permissions from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.net.URLConnection from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded sun.net.www.URLConnection from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded sun.net.www.protocol.file.FileURLConnection from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.net.ContentHandler from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.net.UnknownContentHandler from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded sun.net.www.MessageHeader from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.io.FilePermission from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.io.FilePermission$1 from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.io.FilePermissionCollection from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.AllPermission from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.UnresolvedPermission from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.BasicPermissionCollection from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.Principal from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.security.cert.Certificate from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded TestingClass from file:/data/disk02/home/bchristy/testclassdir/]
[Loaded java.lang.Shutdown from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
Found class%
With java.class.path option (from any directory including testclassdir):
...
[Loaded java.security.PrivilegedActionException from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.io.IOException from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.io.FileNotFoundException from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
**********************exception found****************
Exception in thread "main" java.lang.NoClassDefFoundError: TestingClass
[Loaded java.lang.Shutdown from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar]
I am using the following java setup:
kaleidoscope% echo $JAVA_HOME
/usr/lib/jvm/java-1.5.0-sun
kaleidoscope% ls -l /usr/lib/jvm/java-1.5.0-sun
lrwxrwxrwx 1 root root 23 2008-01-23 14:03 /usr/lib/jvm/java-1.5.0-sun -> java-1.5.0-sun-1.5.0.13/
Making sure its in the right directory:
kaleidoscope% ls
a.out* TestingClass.class TestingClass.java TestingFindClass.cxx
kaleidoscope% pwd
/disk02/home/bchristy/testclassdir
The command I run:
g++ TestingFindClass.cxx -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -L$JAVA_HOME/jre/lib/i386/server -ljvm
Versions:
kaleidoscope% java -version
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05)
Java HotSpot(TM) Client VM (build 1.5.0_13-b05, mixed mode, sharing)
kaleidoscope% g++ --version
g++ (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This is being run on Ubuntu 7.10.
As I said, I am a rookie when it comes to java and could very well be missing something simple. Any help would be greatly appreciated.