473,545 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 */
/*************** *************** *************** **********hasEx ceptionOccurred
* 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.Throw able
* unable to find method id getMessage in java.lang.Throw able
* unable to call method getMessage in java.lang.Throw able
* unable to UTF charchaters from String object returned from
getMessage
*/
char *hasExceptionOc curred( JNIEnv *env) {
jthrowable jthr;
jclass jThrowableClass ;
jmethodID mid;
jstring errorString;
const jbyte *errorInCString ;
jthr = (*env)->ExceptionOccur red ( 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.Throw able is %x\n",jThrowabl eClass);
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)->CallObjectMeth od(env, jthr, mid);
if ( verbose )
printf("The java string object with the message is %x\n",
errorString);
if ( errorString == 0 )
exit(-1);
errorInCString = (*env)->GetStringUTFCh ars(env,errorSt ring,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)->ReleaseStringU TFChars(env,err orString,errorI nCString);
} 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.pat h=.";

//vm_args.version = JNI_VERSION_1_4 ;
vm_args.version = 0x00010004;
vm_args.options = options;
vm_args.nOption s = 5;
vm_args.ignoreU nrecognized = JNI_FALSE;
printf("Reached Here1");
fflush(stdout);

//vm_args.ignoreU nrecognized = JNI_TRUE;
res = JNI_CreateJavaV M(&jvm, (void**)&env, &vm_args);

/*
JDK1_1InitArgs vm_args;
vm_args.version = 0x00010001;
JNI_GetDefaultJ avaVMInitArgs(& vm_args);
// Append USER_CLASSPATH to the default system class path
sprintf(classpa th, "%s%c%s", vm_args.classpa th, PATH_SEPARATOR,
USER_CLASSPATH) ;
vm_args.classpa th = classpath;
// Create the Java VM
res = JNI_CreateJavaV M(&jvm, &env, &vm_args);
vm_args.classpa th = ".;/users8/e43636/JNI";
printf("\n Now Classpath [%s]\n", vm_args.classpa th);
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(en v , "java/lang/String");
printf ( "Class: %x" , testcls );

cls = (*env)->FindClass( env, "InstantiatedFr omC" );
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)->CallStaticIntM ethod(env, cls, mid, 1) );
fflush(stdout);
getchar();
/* Check for an exception */
// if ( hasExceptionOcc urred ( env ) != (char *)0 ) {
// printf("Excepti on has occurred.\n");
// }
/* Call the method we want to use and raise an exception */
// printf("Second call to Java returns:%d\n",
// (*env)->CallStaticIntM ethod(env, cls, mid, 2) );
/* Check for an exception */
// if ( hasExceptionOcc urred ( env ) != (char *)0 ) {
// printf("Excepti on has occurred.\n");
// }
/*jvm->DestroyJavaV M( );*/
}
*************** *************** *************** *************** *************** *************** *************** *****

Java Class whose method is being called

public class InstantiatedFro mC {

public int test(int number) throws Exception {
System.out.prin tln("Number from C: " + number);
if ( number == 2 )
throw new Exception("Exce ption 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_P ATH
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..0x6 80f9628]
Stack_Trace: error while unwinding stack
( 0) 0x9139d884 report_error__F bPCciN22e + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 1) 0x9139d680 report_should_n ot_reach_here__ FPCci + 0x3c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 2) 0x913e1ecc sender__5frameC FP11RegisterMap P8CodeBlob + 0x1ac
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 3) 0x913de8bc real_sender__5f rameCFP11Regist erMap + 0x20
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 4) 0x91779430 sender__6vframe CFv + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 5) 0x91744760 last_java_vfram e__10JavaThread FP11RegisterMap +
0x128 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 6) 0x9174432c print_stack__10 JavaThreadFv + 0x8c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 7) 0x91649524 report_fatal_er ror__2osSFP12ou tputStreamPUci + 0x584
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 8) 0x9164a3c0 handle_unexpect ed_exception__2 osSFP6ThreadiPU cPv +
0x680 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 9) 0x91655398
JVM_handle_hpux _signal__Q2_2os 4HpuxSFiP9__sig infoPvT1 + 0xa10
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
(10) 0x91651bbc signalHandler__ Q2_2os4HpuxSFiP 9__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__F bPCciN22e + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 1) 0x9139d680 report_should_n ot_reach_here__ FPCci + 0x3c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 2) 0x913e1ecc sender__5frameC FP11RegisterMap P8CodeBlob + 0x1ac
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 3) 0x913de8bc real_sender__5f rameCFP11Regist erMap + 0x20
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 4) 0x91779430 sender__6vframe CFv + 0x6c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 5) 0x91744760 last_java_vfram e__10JavaThread FP11RegisterMap +
0x128 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 6) 0x9174432c print_stack__10 JavaThreadFv + 0x8c
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 7) 0x91649524 report_fatal_er ror__2osSFP12ou tputStreamPUci + 0x584
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 8) 0x9164a3c0 handle_unexpect ed_exception__2 osSFP6ThreadiPU cPv +
0x680 [/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
( 9) 0x91655398
JVM_handle_hpux _signal__Q2_2os 4HpuxSFiP9__sig infoPvT1 + 0xa10
[/opt/java1.4/jre/lib/PA_RISC/server/libjvm.sl]
(10) 0x91651bbc signalHandler__ Q2_2os4HpuxSFiP 9__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__F bPCciN22e + 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_n ot_reach_here__ FPCci + 0x3c
[/opt/jav
Da1.4/jre/lib/PA_RISC/server/libjvm.sl]^M


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


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


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


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


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


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


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


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


D27 (10) 0x91651bbc
signalHandler__ Q2_2os4HpuxSFiP 9__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__F bPCciN22e + 0x6c
[/opt/java1.4/jre/li
Db/PA_RISC/server/libjvm.sl]^M


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


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


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


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


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


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


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


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


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


D41 (10) 0x91651bbc
signalHandler__ Q2_2os4HpuxSFiP 9__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 2817


On Nov 23, 10:10 am, "ramasubramania n.ra...@gmail.c om"
<ramasubramania n.ra...@gmail.c omwrote:
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_P ATH
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, "ramasubramania n.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
6788
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 the issue is. Thanks Ravi
1
7199
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
7914
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 as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only...
11
9217
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
4
5794
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 jdk11_path to: Java Development Kit 1.1 installation path (JDK11_PATH) =
14
7310
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 my C# program. But this new API is written in Java. They gave me an example source code that uses their API but the actual API looks like it is...
4
3720
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 database for other application is SQL-SERVER. they have 12 users.
2
1439
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 applications from Python. I'm developing web-based application using Zope. Thanks in advance.
2
1874
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 managed types. I am a java programmer and not aware of C++ programming.Is there any open source tool available that can do JNI wrapping for me?...
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.