Calling java APIs from a c program

  • Thread starter ramasubramanian.rahul
  • Start date
R

ramasubramanian.rahul

hi
i am trying to call some java APIs from c . i use the standatd JNI
calls to load the JVM from a c program and call all java functions by
using a pointer to the jvm which was returned by the JNI call
the source code is given below and also the errors ..
Plz help in resolving these.

This is a code being (slightly modified ) which was downloaded from SUN
website :

#include <jni.h>
#include <errno.h>
#define PATH_SEPARATOR ';'
#define USER_CLASSPATH "."

JavaVM *jvm; /* Pointer to a Java VM */
JNIEnv *env; /* Pointer to native method interface */
JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization requirements */
int verbose = 1;/* Debugging flag */
/*******************************************************hasExceptionOccurred
* Check to see if there is an exception. If there is one
* return the string with the value.
* Returns the following:
* > 0 Exception raised
* null no exception
* Exits for fatal errors such as
*
* unable to find object class for java.lang.Throwable
* unable to find method id getMessage in java.lang.Throwable
* unable to call method getMessage in java.lang.Throwable
* unable to UTF charchaters from String object returned from
getMessage
*/
char *hasExceptionOccurred( JNIEnv *env) {
jthrowable jthr;
jclass jThrowableClass;
jmethodID mid;
jstring errorString;
const jbyte *errorInCString;
jthr = (*env)->ExceptionOccurred ( env );

printf("Is there an exception?" );
/* If there was an exception, extract the message from the object */
if ( jthr != (jthrowable)0 ) {
printf (" Yes." );
jThrowableClass = (*env)->GetObjectClass(env,jthr);
if ( verbose )
printf("The class for java.lang.Throwable is %x\n",jThrowableClass);
if ( jThrowableClass == 0 )
exit(-1);
mid = (*env)->GetMethodID( env, jThrowableClass, "getMessage",
"()Ljava/lang/String;" );
if ( verbose )
printf("The method id for the ThrowableClass is %x\n", mid);
if ( mid == 0 )
exit(-1);
errorString = (*env)->CallObjectMethod(env, jthr, mid);
if ( verbose )
printf("The java string object with the message is %x\n",
errorString);
if ( errorString == 0 )
exit(-1);
errorInCString = (*env)->GetStringUTFChars(env,errorString,0);
if ( errorInCString == 0 ) {
exit(-1);
/* N.B. The following is not valid for all UTF strings */
printf ( "UTF stirng error");
}
if ( verbose )
printf("The error from Java is \"%s\".\n", errorInCString );
(*env)->ReleaseStringUTFChars(env,errorString,errorInCString);
} else
if ( verbose )
printf (" No." );

return ( (char *) 0 );
}
/***********************************************************************Main*/
/*
* Get the default initialization arguments and set the class path
* Call a method in java without raising an exception
* Call a mewthod in Java expecting to have an exception raised
*/
main(int argc, char **argv ) {

jclass cls;
jclass testcls;
jmethodID mid;
jthrowable jthr;
jint res;
char classpath[1024];
JavaVMInitArgs vm_args;
JavaVMOption options[5];

options[0].optionString = "-Xms4M";
options[1].optionString = "-Xmx64M";
options[2].optionString = "-Xss512K";
options[3].optionString = "-Xoss400K";
options[4].optionString = "-Djava.class.path=.";

//vm_args.version = JNI_VERSION_1_4;
vm_args.version = 0x00010004;
vm_args.options = options;
vm_args.nOptions = 5;
vm_args.ignoreUnrecognized = JNI_FALSE;
printf("Reached Here1");
fflush(stdout);

//vm_args.ignoreUnrecognized = JNI_TRUE;
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

/*
JDK1_1InitArgs vm_args;
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
// Append USER_CLASSPATH to the default system class path
sprintf(classpath, "%s%c%s", vm_args.classpath, PATH_SEPARATOR,
USER_CLASSPATH);
vm_args.classpath = classpath;
// Create the Java VM
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);


vm_args.classpath = ".;/users8/e43636/JNI";
printf("\n Now Classpath [%s]\n", vm_args.classpath);
fflush(stdout);
*/
printf("Reached Here");
fflush(stdout);
if (res < 0 )
{
printf("JVM Could not be loaded [%d] and errorno[%d]\n", res,errno);
exit(1);
}
/* Find the class we want to load */
testcls = (*env)->FindClass(env , "java/lang/String");
printf ( "Class: %x" , testcls );

cls = (*env)->FindClass( env, "InstantiatedFromC" );
if ( verbose )
printf ( "Class: %x" , cls );
fflush(stdout);

/* Find the method we want to use */
mid = (*env)->GetMethodID( env, cls, "test", "(I)I" );
if ( verbose )
printf ( "Method: %x" , mid );
fflush(stdout);
/* Call the method we want to use */
printf("First call to Java returns:%d\n",
(*env)->CallStaticIntMethod(env, cls, mid, 1) );
fflush(stdout);
getchar();
/* Check for an exception */
// if ( hasExceptionOccurred ( env ) != (char *)0 ) {
// printf("Exception has occurred.\n");
// }
/* Call the method we want to use and raise an exception */
// printf("Second call to Java returns:%d\n",
// (*env)->CallStaticIntMethod(env, cls, mid, 2) );
/* Check for an exception */
// if ( hasExceptionOccurred ( env ) != (char *)0 ) {
// printf("Exception has occurred.\n");
// }
/*jvm->DestroyJavaVM( );*/
}


**************************************************************************************************************

Java Class whose method is being called

public class InstantiatedFromC {

public int test(int number) throws Exception {
System.out.println("Number from C: " + number);
if ( number == 2 )
throw new Exception("Exception raised in java seen in C");
return ( number + 1 );
}
//public static void main(String args[]) {
//}
}

*******************************************************************************************************************8
THe C Code above is compiled as follows::
SHLIB_PATH=/opt/java1.4/jre/lib/PA_RISC:/opt/java1.4/jre/lib/PA_RISC/server:$SHLIB_PATH
export SHLIB_PATH
gcc -g tcl2JavaVM.c -I/opt/java1.4/include
-I/opt/java1.4/include/hp-ux
-L/opt/java1.4/jre/lib/PA_RISC/native_threads
-L/opt/java1.4/jre/lib/PA_RISC -L/opt/java1.4/jre/lib/PA_RISC/server
-ljvm -lpthread -llwp

on HP-UX machine...

**************************************************************************************************************8

THe error stack being generated :

Reached Here1Reached HereClass: 4000c7a0Class: 4000c7c4Method: 4013fd78
Unexpected Signal : 4 occurred at PC=0x680A9F28
Function=[Unknown.]
Library=/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl

NOTE: We are unable to locate the function name symbol for the error
just occurred. Please refer to release documentation for possible
reason and solutions.


Current Java thread:
"main" prio=7 tid=4000bca8 nid=1 lwp_id=6600282 runnable
[0x680f9000..0x680f9628]
Stack_Trace: error while unwinding stack
( 0) 0x9139d884 report_error__FbPCciN22e + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 1) 0x9139d680 report_should_not_reach_here__FPCci + 0x3c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 2) 0x913e1ecc sender__5frameCFP11RegisterMapP8CodeBlob + 0x1ac
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 3) 0x913de8bc real_sender__5frameCFP11RegisterMap + 0x20
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 4) 0x91779430 sender__6vframeCFv + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 5) 0x91744760 last_java_vframe__10JavaThreadFP11RegisterMap +
0x128 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 6) 0x9174432c print_stack__10JavaThreadFv + 0x8c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 7) 0x91649524 report_fatal_error__2osSFP12outputStreamPUci + 0x584
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 8) 0x9164a3c0 handle_unexpected_exception__2osSFP6ThreadiPUcPv +
0x680 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 9) 0x91655398
JVM_handle_hpux_signal__Q2_2os4HpuxSFiP9__siginfoPvT1 + 0xa10
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
(10) 0x91651bbc signalHandler__Q2_2os4HpuxSFiP9__siginfoPv + 0x14
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
(11) 0xc020d138 _sigreturn [/usr/lib/libc.2]
[error occured during error reporting]
Stack_Trace: error while unwinding stack
( 0) 0x9139d884 report_error__FbPCciN22e + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 1) 0x9139d680 report_should_not_reach_here__FPCci + 0x3c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 2) 0x913e1ecc sender__5frameCFP11RegisterMapP8CodeBlob + 0x1ac
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 3) 0x913de8bc real_sender__5frameCFP11RegisterMap + 0x20
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 4) 0x91779430 sender__6vframeCFv + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 5) 0x91744760 last_java_vframe__10JavaThreadFP11RegisterMap +
0x128 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 6) 0x9174432c print_stack__10JavaThreadFv + 0x8c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 7) 0x91649524 report_fatal_error__2osSFP12outputStreamPUci + 0x584
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 8) 0x9164a3c0 handle_unexpected_exception__2osSFP6ThreadiPUcPv +
0x680 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 9) 0x91655398
JVM_handle_hpux_signal__Q2_2os4HpuxSFiP9__siginfoPvT1 + 0xa10
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
(10) 0x91651bbc signalHandler__Q2_2os4HpuxSFiP9__siginfoPv + 0x14
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
(11) 0xc020d138 _sigreturn [/usr/lib/libc.2]
4 Reached Here1Reached HereClass: 4000c7a0Class: 4000c7c4Method:
4013fd78^
M
5 Unexpected Signal : 4 occurred at PC=0x680A9F28^M
6 Function=[Unknown.]^M
7 Library=/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl^M
8 ^M
9 NOTE: We are unable to locate the function name symbol for the
error^M
10 just occurred. Please refer to release documentation for
possible^
M
11 reason and solutions.^M
12 ^M
13 ^M
14 Current Java thread:^M
15 "main" prio=7 tid=4000bca8 nid=1 lwp_id=6600282 runnable
[0x680f9000..0x
680f9628]^M
16 Stack_Trace: error while unwinding stack^M
17 ( 0) 0x9139d884 report_error__FbPCciN22e + 0x6c
[/opt/java1.4/jre/li
b/PA_RISC/server/libjvm.sl]^M
@ "resu" [Incomplete last line] 44 lines, 3596 characters

18 ( 1) 0x9139d680 report_should_not_reach_here__FPCci + 0x3c
[/opt/jav
Da1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D19 ( 2) 0x913e1ecc sender__5frameCFP11RegisterMapP8CodeBlob
+ 0x1ac [/o
Dpt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D20 ( 3) 0x913de8bc real_sender__5frameCFP11RegisterMap +
0x20 [/opt/jav
Da1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D21 ( 4) 0x91779430 sender__6vframeCFv + 0x6c
[/opt/java1.4/jre/lib/PA_R
DISC/server/libjvm.sl]^M


D22 ( 5) 0x91744760
last_java_vframe__10JavaThreadFP11RegisterMap + 0x128
D [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D23 ( 6) 0x9174432c print_stack__10JavaThreadFv + 0x8c
[/opt/java1.4/jre
D/lib/PA_RISC/server/libjvm.sl]^M


D24 ( 7) 0x91649524
report_fatal_error__2osSFP12outputStreamPUci + 0x584
D
 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D25 ( 8) 0x9164a3c0
handle_unexpected_exception__2osSFP6ThreadiPUcPv + 0x
D680 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D26 ( 9) 0x91655398
JVM_handle_hpux_signal__Q2_2os4HpuxSFiP9__siginfoPvT1
D + 0xa10 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D27 (10) 0x91651bbc
signalHandler__Q2_2os4HpuxSFiP9__siginfoPv + 0x14 [/
Dopt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D28 (11) 0xc020d138 _sigreturn [/usr/lib/libc.2]^M

D29 [error occured during error reporting]^M

D30 Stack_Trace: error while unwinding stack^M

D31 ( 0) 0x9139d884 report_error__FbPCciN22e + 0x6c
[/opt/java1.4/jre/li
Db/PA_RISC/server/libjvm.sl]^M


D32 ( 1) 0x9139d680 report_should_not_reach_here__FPCci +
0x3c [/opt/jav
Da1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D33 ( 2) 0x913e1ecc sender__5frameCFP11RegisterMapP8CodeBlob
+ 0x1ac [/o
Dpt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D34 ( 3) 0x913de8bc real_sender__5frameCFP11RegisterMap +
0x20 [/opt/jav
Da1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D35 ( 4) 0x91779430 sender__6vframeCFv + 0x6c
[/opt/java1.4/jre/lib/PA_R
DISC/server/libjvm.sl]^M


D36 ( 5) 0x91744760
last_java_vframe__10JavaThreadFP11RegisterMap + 0x128
D [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D37 ( 6) 0x9174432c print_stack__10JavaThreadFv + 0x8c
[/opt/java1.4/jre
D/lib/PA_RISC/server/libjvm.sl]^M


D38 ( 7) 0x91649524
report_fatal_error__2osSFP12outputStreamPUci + 0x584
D
 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D39 ( 8) 0x9164a3c0
handle_unexpected_exception__2osSFP6ThreadiPUcPv + 0x
D680 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D40 ( 9) 0x91655398
JVM_handle_hpux_signal__Q2_2os4HpuxSFiP9__siginfoPvT1
D + 0xa10 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D41 (10) 0x91651bbc
signalHandler__Q2_2os4HpuxSFiP9__siginfoPv + 0x14 [/
Dopt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


D42 (11) 0xc020d138 _sigreturn [/usr/lib/libc.2]^M

************************************************************************************************************

any help in this regard will be gtreatly appriciated

Thanks in advance
rahul
 
J

Jean-Francois Briere

From your C code you are trying to call a Java static method.
But in your InstantiatedFromC class your test() method is NOT static.
Maybe you should make the test() method static and execute your code
again.

Regards
 
R

ramasubramanian.rahul

hi jean.. thanks for the help.. it started working but is giving some
other error now.. i am sending the error msg.. couldnt make much out of
it.. would be glad if you could throw some light
kind regards
rahul



*************************************************************************************************************


Unexpected Signal : 11 occurred at PC=0x8D8523CC
Function=find__5JNIidFi
Library=/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl

Current Java thread:
"main" prio=7 tid=4000bce8 nid=1 lwp_id=6729673 runnable
[0x00000000..0x680f9668]

Dynamic libraries:

Heap at VM Abort:
Heap
def new generation total 2112K, used 152K [64880000, 64ac0000,
65dd0000)
eden space 1920K, 7% used [64880000, 648a6020, 64a60000)
from space 192K, 0% used [64a60000, 64a60000, 64a90000)
to space 192K, 0% used [64a90000, 64a90000, 64ac0000)
tenured generation total 1792K, used 0K [61dc0000, 61f80000,
64870000)
the space 1792K, 0% used [61dc0000, 61dc0000, 61dc0200, 61f80000)
compacting perm gen total 16384K, used 1176K [5ddb0000, 5edb0000,
61db0000)
the space 16384K, 7% used [5ddb0000, 5ded6200, 5ded6200, 5edb0000)

Local Time = Thu Nov 23 12:34:26 2006
Elapsed Time = 0
#
# HotSpot Virtual Machine Error : 11
# Please report this error to HP customer support.
#
# Java VM: Java HotSpot(TM) Server VM (1.4.2
1.4.2.02-040225-18:59-PA_RISC1.1 PA1.1 mixed mode)
#
 
T

Thomas Fritsch

hi
i am trying to call some java APIs from c . i use the standatd JNI
calls to load the JVM from a c program and call all java functions by
using a pointer to the jvm which was returned by the JNI call
the source code is given below and also the errors ..
Plz help in resolving these.

This is a code being (slightly modified ) which was downloaded from SUN
website : [...]
jthr = (*env)->ExceptionOccurred ( env );

printf("Is there an exception?" );
/* If there was an exception, extract the message from the object */
if ( jthr != (jthrowable)0 ) {
printf (" Yes." );
The way you handle exception is inherently unsafe, because it provokes
crashes. You should at least insert
(*env)->ExceptionClear(env);
here, before calling any further JNI functions.
jThrowableClass = (*env)->GetObjectClass(env,jthr);
[...]

When a Java exception is pending, only very few JNI functions are
allowed. Quoted from the JNI spec chapter "Exception Handling" at
<http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp17626>
<quote>
When there is a pending exception, the only JNI functions that are safe
to call are ExceptionOccurred(), ExceptionDescribe(), and ExceptionClear().
</quote>

In other words: Calling any unsafe JNI functions, when an exception is
pending, will probably cause a crash of the VM (in your case a crash
with signal 4).

[...]
Unexpected Signal : 4 occurred at PC=0x680A9F28
Function=[Unknown.]
Library=/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl
[...]
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top