Please Help: Invoking GetStaticMethodID in cpp/c++ exception error
843829Jul 24 2001 — edited Jul 25 2001Hello everyone:
This is my first post, so please no bashing if I don't get it all the first time.
I have looked at this for days now to no avail. I also have read many many threads oon this site trying to get this fixed.
The issues is when I do a GetStaticMedthodID call from C++ on RedHat Linux 7.1, I get an exception thread in "main" ... java.lang.NoSuchMethodID: main.
Here is all of the cpp/c++ code, sorry about formating:
//Declare Preprocessor & Includes
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define PATH_SEPERATOR ':'
#include <copyright.txt.h>
#define classpath "-Djava.class.path=./client.jar:-Djava.class.path=/usr/java/jdk1.3/jre/lib/rt.jar:";
#include <jni.h>
//******** Start Main Function *****************
int main(int argc, char *argv[])
{
char *usg = "\nUsage: \n";
char *copytext = Copyright_Text;
char *load = "rpic/client/cli/CLI";
char *lineopt = "\nError: Not enough options on command line\n";
char *clinusg = "\nError: Command line does not have the right options, see usage:";
char *cargs0 = argv[0];
char *cargs1 = argv[1];
char *cargs2 = argv[2];
char *cargs3 = argv[3];
//Declare JNI Enviroment Settings
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
jclass cls;
jclass c;
jobjectArray ja;
jmethodID mid;
options[0].optionString = classpath;
options[1].optionString = "-Djava.library.path=/usr/java/jdk1.3/jre/lib"; options[2].optionString= "-verbose:jni";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 3;
vm_args.ignoreUnrecognized = JNI_FALSE;
if(argc < 3){
printf(lineopt);
printf(usg);
printf(copytext);
return 0;
}
else if(! ((strcmp(argv[2], "-auto")) || (strcmp(argv[2], "-norm"))) ) {
printf(clinusg);
printf(usg);
printf(copytext);
return 0;
}
else {
printf("\nTrying to Create JVM..............\n\n");
jint res=JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if(! (res == JNI_OK) ){
printf("\nCould not initiate Java Virual Machine session. Verify Java Resource Environment (JRE 1.2.2 or higher) installation. \n\n");
return 0;
}
else{
c = env->FindClass("rpic/client/cli/CLI"); /*Classpath*/
if (env->ExceptionOccurred())
env->ExceptionDescribe();
return 0;
}
cls = env->FindClass("java/lang/String"); /*Java Classpath*/
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
return 0;
}
if(c == 0){
printf("\nError: Could not load class /rpic/client/CLI from ./client.jar. Please ensure ./client.jar file from your **** is application in working directory\n");
return 0;
}
if(c !=0)
printf("\nSucsessfuly loaded /rpic/client/cli/CLI from ./client.jar..........\n");
if(cls == 0){
printf("\nError: Could not load class java/lang/string.");
return 0;
}
if(cls !=0)
printf("\nSucsessfuly loaded java/lang/string..........\n");
if(argc == 3)
ja = env->NewObjectArray(2,cls,NULL);
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
printf("\nError: NewObjectArray for the command line options %s %s %s could not be set. \n",cargs0,cargs1,cargs2);
return 0;
}
else
ja = env->NewObjectArray(3,cls,NULL);
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
printf("\nError: NewObjectArray for %s %s %scommand line options could not be set. \n",cargs1,cargs2,cargs3);
return 0;
}
env->SetObjectArrayElement(ja, 0, env->NewStringUTF(argv[1]));
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
printf("Error: SetObjectArrayElement 0 for %s could not be loaded.\n",cargs1);
return 0;
}
env->SetObjectArrayElement(ja, 1, env->NewStringUTF(argv[2]));
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
printf("Error: SetObjectArrayElement for %s could not be loaded.\n",cargs2);
return 0;
}
if(c != NULL)
printf("\n\n\nLoaded Find.Class rpic/client/cli/CLI \n");
if(c == NULL) {
printf("\nCould not load %s:. Please ensure working directory contains client.jar.\n\n", load);
return 0;
}
printf("\n\nStarting GetStaticMethodID \n\n");
mid = env->GetStaticMethodID(c, "main", "([java/lang/String)V");
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
printf("Error: GetStaticMethodID Failed.\n");
return 0;
}
env->CallStaticVoidMethod(c,mid,ja);
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
printf("Error: After setting JavaMethodID JVM failed with exception. \n");
return 0;
}
int *exit = 0;
pthread_exit(&exit);
return 1;
}
}
}
I have used the following during compilation
-L/usr/java/jdk1.3/jre/lib/i386 -L/usr/java/jdk1.3/jre/lib/i386/client -L/usr/java/jdk1.3/jre/lib/i386/native_threads
-ljvm -lhpi
I have exported the following enviroments:
LD_LIBRARY_PATH="/usr/java/jdk1.3/jre/lib/i386/classic:/usr/java/jdk1.3/jre/lib/i386/native_threads:/usr/java/jdk1.3/jre/lib/i386
CLASSPATH=/usr/java/jdk1.3/jre/bin/rt.jar:./
and $PATH has the correct path to the bin dir.
If I go to a bash shell and run:
java -classpath ./client.jar rpic/client/cli/CLI option1 -option2
it runs.
This version of the code compiled in visual c++ on a MS platform works as well. I belive this is just some thing I am doing wrong on Linux. Any and all help would be very much appreicated.
Best Regards
JFS