Actually, now i have another problem:
Here's my Java Code:
//ExceptionExperiment.java
public class ExceptionExperiment {
public native void testException() throws MyException;
static {
System.loadLibrary("ExceptionExperimentImpl");
}
public ExceptionExperiment() {
}
public static void main(String[] args) {
ExceptionExperiment app = new ExceptionExperiment();
try {
app.testException();
System.out.print("a");
}catch(MyException e) {
e.printStackTrace();
}
}
public class MyException extends Exception {
public MyException() {
super();
}
public MyException(String s) {
super(s);
}
}
}
And here's the C++ code:
//ExceptionExperimentImpl.cpp
JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
(JNIEnv *env, jobject obj)
{
jclass cls =
env->FindClass("ExceptionExperiment$MyException");
if (cls != NULL) {
env->ThrowNew(cls, NULL);
}
env->DeleteLocalRef(cls);
}
Now, this doesn't throw the MyException, but throws this:
//output
java.lang.NoSuchMethodError: ExceptionExperiment$MyException: method
<init>()V not found
at ExceptionExperiment.testException(Native Method)
at ExceptionExperiment.main(ExceptionExperiment.java:15)
>From this output it tells me that it can find the class but can't find
the <init>()V method (which is the constructor, right?).
Am i doing something wrong here?
res7cxbi@verizon.net - 02 Jan 2006 00:28 GMT
Problem Solved!
I made the MyException class a separate class rather than an inner
class of ExceptionExperiment
The revised java code:
//ExceptionExperiment.java
public class ExceptionExperiment {
public native void testException() throws MyException;
static {
System.loadLibrary("ExceptionExperimentImpl");
}
public ExceptionExperiment() {
}
public static void main(String[] args) {
ExceptionExperiment app = new ExceptionExperiment();
try {
app.testException();
System.out.print("a");
}catch(MyException e) {
e.printStackTrace();
}
}
}
class MyException extends Exception {
public MyException() {
super();
}
public MyException(String s) {
super(s);
}
}
And here's the revised C++ code:
//ExceptionExperimentImpl.cpp
JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
(JNIEnv *env, jobject obj)
{
jclass cls = env->FindClass("MyException");
if (cls != NULL) {
env->ThrowNew(cls, NULL);
}
env->DeleteLocalRef(cls);
}
Gordon Beaton - 02 Jan 2006 08:20 GMT
> And here's the revised C++ code:
> //ExceptionExperimentImpl.cpp
[quoted text clipped - 7 lines]
> env->DeleteLocalRef(cls);
> }
Note that it is rarely necessary to need DeleteLocalRef(). All local
references will be "deleted" when the native method returns.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
res7cxbi@verizon.net - 03 Jan 2006 01:17 GMT
Really? The "A Utility Function" (6.1.2) at the URL
http://java.sun.com/docs/books/jni/html/exceptions.html#11202 does this
Gordon Beaton - 03 Jan 2006 11:55 GMT
> Really? The "A Utility Function" (6.1.2) at the URL
> http://java.sun.com/docs/books/jni/html/exceptions.html#11202 does this
Yes it does. Not because it's technically necessary to do so, but
rather to follow a guideline (described in section 5.3 rules for
managing references) that says a utility function should avoid
accumulating local references.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Roedy Green - 03 Jan 2006 03:27 GMT
>Actually, now i have another problem:
I would recommend getting a textbook with some examples.
See http://mindprod.com/jgloss/jni.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.