By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

Calling Java APIs fr4om C

P: n/a
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,errorInCStr ing);
} 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__siginfoP vT1 + 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__siginfoP vT1 + 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__siginfoP vT1
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__siginfoP vT1
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

Nov 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a


On Nov 23, 10:10 am, "ramasubramanian.ra...@gmail.com"
<ramasubramanian.ra...@gmail.comwrote:
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.
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...
Firstly the standard answer : Off topic. Ask in the java/sun discussion
forums.

<off-topic>
The c code must be exported/converted into a shared library which then
is used by the java code. Consult your HP-UX documentation as to how
to create a shared library and then follow the instructions given on
the sun website to get the JNI program working. The instructions on
the Sun web site are pretty good and ive done it before for Linux .
</off-topic>

Nov 23 '06 #2

P: n/a
On 22 Nov 2006 21:10:55 -0800, "ramasubramanian.rahul" wrote:
>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 ..
<offtopic>
- don't call Java from C (Sun deliberately made that way very
intricate), call C from Java (as suggested by Procrastinx)
- use alternative ways for communication, e.g. sockets
</offtopic>
Nov 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.