Cannot find my JAVA class with JNI FindClass from C++

S

Steve

Hi

This subject has been posted and reading them has not helped me
I can call a Java Class "String" but when I try to call a Class I
created "Simple"
I cannot find it. I put the class in C:\WINNT\java\classes

I tried adding to a Jar file Steve.Jar with no luck

Does anyone have a suggestion what I can check????
Could the class be built wrong???

Thanks
Steve

I am running Windows2000, MSVC++, JDK1.4


Java Class
----------------

public class Simple {

public native int SimpleFct(int x) {
return x * 2;
}

}

H-File for class

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Simple */

#ifndef _Included_Simple
#define _Included_Simple
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Simple
* Method: SimpleFct
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_Simple_SimpleFct
(JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif


C++ Code
---------------
#include "stdafx.h"
#include "C:\JBuilder9\jdk1.4\include\jni.h"
#include "SimpleJ.h"

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

long result;
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs vm_args;
JavaVMOption options[4];


/* disable JIT */
options[0].optionString = "-Djava.compiler=NONE";
/* user classes */
options[1].optionString = "-Djava.class.path=C:\\WINNT\\java\\classes";

options[3].optionString =
"-Djava.library.path=C:\\JBuilder9\\jdk1.4\\lib,C:\\JBuilder9\\jdk1.4\\jre\\lib";
/* set native library path */
/* print JNI msgs */
options[2].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = JNI_TRUE;

if ( result = JNI_GetDefaultJavaVMInitArgs( &vm_args ) )
{
// handle error
MessageBox(NULL, "Error Intir Args", "JAVA VM", MB_OK);
return -1;
}

/* Note that in the Java 2 SDK, there is no longer any need to call
* JNI_GetDefaultJavaVMInitArgs.
*/
result = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
if (result < 0)
{
MessageBox(NULL, "Error Create JAVAVM", "JAVA VM", MB_OK);
return -1;
}
else
{
MessageBox(NULL, "Sucess Create JAVAVM", "JAVA VM", MB_OK);

}

/* Find Main.main */
jclass cls2 = env->FindClass("java/lang/String");
if (cls2 == 0)
{
MessageBox(NULL, "This class not found", "JAVA VM", MB_OK);
return(1);
}

jclass cls = env->FindClass("Simple");
//jclass cls = env->FindClass("Steve/Simple"); //Try with JAR


if (cls == 0)
{
MessageBox(NULL, "This class not found", "JAVA VM", MB_OK);
return(1);
}
 
G

Gordon Beaton

This subject has been posted and reading them has not helped me I
can call a Java Class "String" but when I try to call a Class I
created "Simple" I cannot find it. I put the class in
C:\WINNT\java\classes

I tried adding to a Jar file Steve.Jar with no luck

Does anyone have a suggestion what I can check????
Could the class be built wrong???

A few comments:

- don't use JNI_GetDefaultJavaVMInitArgs() with the new argument
structure. You have a comment to that effect, but you call the
function anyway.

- whenever a JNI function fails, you can use something like this to
see the reason:

if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}

- what is exact, fully qualified name of your class? Is there a
package statement in Simple.java?

jclass cls = env->FindClass("Simple");
jclass cls = env->FindClass("Steve/Simple"); //Try with JAR

Note that the above code should not change just because you put the
class in a jar file, the classname is the same in both cases. If you
put the class file in a jar, the jar must be specified in the
classpath. However the class file must be in a directory that
corresponds to the package name, relative to one of the directories
(or jar files) in the classpath. See this for more information:

http://www.yoda.arachsys.com/java/packages.html

/gordon
 
S

Steve

Hi Gordon

Thanks for the info

I have coded this

if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}

But how do you get the error number or message from this ???

Thanks
Steve
 
M

Mark Buckingham

Steve said:
Hi Gordon

Thanks for the info

I have coded this

if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}

But how do you get the error number or message from this ???

Thanks
Steve

It might be overkill, but I do something like this:

if (env->ExceptionCheck()) {
jthrowable e = env->ExceptionOccurred();

char buf[1024];
strnset(buf, 0, 1024);

// have to clear the exception before JNI will work again.
env->ExceptionClear();

jclass eclass = env->GetObjectClass(e);

mid = env->GetMethodID(eclass, "toString", "()Ljava/lang/String;");

jstring jErrorMsg = (jstring) env->CallObjectMethod(e, mid);
const char* cErrorMsg = env->GetStringUTFChars(jErrorMsg, NULL);

strcpy(buf, cErrorMsg);

env->ReleaseStringUTFChars(jErrorMsg, cErrorMsg);

// you can do anything you want with error text in buf now
}

If you have extended exception to add an error number, you could fire
off the getErrorNumber() method as well...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top