Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

calling C from java

 
Highlighted
Dennis Handly
Acclaimed Contributor

Re: calling C from java

>can someone point out why I am unable to compile?

You have:
static char cmdbuffer[size];
JNIEXPORT jint JNICALL Java_SYSTEMCALL_SNMPTRAP
(JNIEnv *env, jobject obj, jstring cmdbuffer) {

Your parm name cmdbuffer hides your file scope definition.

If you were using a real language like C++, you could just use ::cmdbuffer to access the global scope. :-)
Martin Vorlaender
Honored Contributor

Re: calling C from java

Apart from cmdbuffer used twice, jstring is NOT an ordinary C char*. You'll have to convert the jstring parameter - see http://java.sun.com/docs/books/jni/html/objtypes.html#4001 , especially the methods GetStringChars and GetStringLength.

cu,
Martin
x2084
Trusted Contributor

Re: calling C from java

After learning C or a "real" languages and how to call their functions from Java in order to do a system() with a C-style string retrieved from a Java and/or C++ string class, you may want to learn what the Runtime class can do for you.
SAMI AHMAD
Regular Advisor

Re: calling C from java

ok I am not getting compiler errors now after i made the following change but iam now not able to run it using java

static char cmdbuffer[size];

/* main(int argc, char **argv ) */

JNIEXPORT jint JNICALL Java_SYSTEMCALL_SNMPTRAP
(JNIEnv *env, jobject obj , jstring msg) {

int i;
int sts;
char *cmd = "mcr sys$system:tcpip$snmp_trapsnd.exe 0.0 local 0 0 0 -h 10.100.18.245 -v2c 1.3.6.1.6.3.1.1.4.1.0 \"D\"";

char buf[512];
const jbyte *str;
str = (*env)->GetStringUTFChars(env, msg, NULL);
if (str == NULL) {
return NULL; /* OutOfMemoryError already thrown */
}
printf("%s", str);
(*env)->ReleaseStringUTFChars(env, msg, str);


/* strncat (cmdbuffer, cmd,size);
strncat (cmdbuffer, " \"",size);
strncat (cmdbuffer, argv[1],size);
strncat (cmdbuffer,"\"",size);
printf ("%s\n",cmdbuffer);
sts = system(cmdbuffer);
printf( " sts = %%x%08x.\n", sts);
*/
return 0;
}

$link/share=sndsnmp_c sndsnmp_c,sndsnmp_c/opt

$ JAVA "SYSTEMCALL" "testing"
Warning: JIT compiler "SUNCOMPILER" not found. Will use interpreter.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no SNDSNMP_C in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1578)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:834)
SAMI AHMAD
Regular Advisor

Re: calling C from java

and my options file looks like this :

SUNNY2$ type sndsnmp_c.opt
case_sensitive=YES
symbol_vector=(Java_SYSTEMCALL_SNMPTRAP=PROCEDURE)
case_sensitive=NO
java$java_shr/share
gsmatch=lequal,1,1
SAMI AHMAD
Regular Advisor

Re: calling C from java

looking at the files in sys$common:[java$142.vms_demo...]build_example.com , I see following which is confusing me, what
is the function of the libgesyi logical pointing to the shared image ? I didnt define any such logical, is this the reason iam getting error? how is this logical name constructed ?

$ link/share=getjpi_shr.exe getjpi_nat.obj,getjpi.opt/opt,java.opt/opt
$ link/debug/share=getjpi_g_shr.exe getjpi_nat.obj,getjpi.opt/opt,java_g.opt/opt
$
$ define/nolog libgetsyi sys$disk:[]getsyi_shr.exe
$ define/nolog libgetsyi_g sys$disk:[]getsyi_g_shr.exe
$

SAMI AHMAD
Regular Advisor

Re: calling C from java

ok I found on HP site how to link the image properly and that fixed the issue so now i can concentrate on application errors , any ideas why iam getting those special characters instead of the string that iam inputting as parameter?


SUNNY2$ link/share/exec=java$sndsnmp_c_shr.exe sndsnmp_c,sndsnmp_c/opt

SUNNY2$ def/job/log java$sndsnmp_c_shr sys$login:java$sndsnmp_c_shr.exe
SUNNY2$ JAVA "SYSTEMCALL" "TESTING"
Warning: JIT compiler "SUNCOMPILER" not found. Will use interpreter.
This is java ..calling the C routing
@²UP²Uà`UU0lŸz;lŸz6for now lets omit this


Martin Vorlaender
Honored Contributor

Re: calling C from java

>>>
any ideas why iam getting those special characters instead of the string that iam inputting as parameter?
<<<

Because you're not using it?

- The jstring parameter msg is probably not UTF8 encoded. That's why I suggested GetStringChars and not GetStringUTFChars.
- The jbyte variable str is not a null-terminated C string. That's why I suggested using GetStringLength

cu,
Martin
Hoff
Honored Contributor

Re: calling C from java

There are Java SNMP libraries widely available that can send SNMP traps, and various of which would seem more supportable than this current approach.
SAMI AHMAD
Regular Advisor

Re: calling C from java

sorry i think the code is not upto date . iam using jstring n not jbyte

jstring jstr;
jstr = (*env)->NewStringUTF(env, result);
if (jstr == NULL) {
fprintf(stderr, "out of memory");
return 0;
}

printf("%s","now in C routine");
printf("%s", jstr);