473,394 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Calling Java APIs fr4om C

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
2 2806


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: TI | last post by:
Where would you start if you wanted to program perhaps a java application to enable a bluetooth mobile phone to work as walkie-talkie? ( schematics, compilers... ) thanks
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
4
by: acemann7 | last post by:
Windows 2000 udb v 7.2 Got the latest fixpak 13 Applied it. Still can't get a single java sp to run. I used to get reason "2"... now reason "0" - reason 0 is not even documented. I point...
14
by: Mick | last post by:
I wrote a C# program that interfaces with a data vendor over the web using an API they supplied and their examples in C#. Now I have another data vendor's API and example that I want to add to...
4
by: Elhanan | last post by:
hi.. all a client of ours is considering to move from a dos application to windows desktop application. the application is for traveling agency, the database is rather large. their current...
2
by: Manoj Kumar P | last post by:
Hi, I'm new to python. I would like to know whether is it possible to access Java/C++ APIs from python. I have two applications written in Java and API, I wanted to call the APIs of these...
2
by: pragatid | last post by:
Hi, I want to use .Net ADO lib classess and their methods from my java application. I need to write JNI wrapper DLL in C++ for this. calling ADO libraries from C++ is itself challenging due to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.