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

calling C from java

 
Highlighted
SAMI AHMAD
Regular Advisor

calling C from java

I am seeing few examples here on how to call Cobol or Fortran from java but I couldnt find any document or example on how to call C from java ..can someone point me in the right direction?

thanks
29 REPLIES 29
Mike Kier
Valued Contributor

Re: calling C from java

Tim Sneddon from Kednos had a good article, "Calling OpenVMS native routines from Java," in the OpenVMS Technical Journal, Volume 12 (http://h71000.www7.hp.com/openvms/journal/index.html) which, although using a PLI example, should (and claims to be) extensible to C.
Practice Random Acts of VMS Marketing
Duncan Morris
Honored Contributor

Re: calling C from java

John Gillings
Honored Contributor

Re: calling C from java

SAMI,

Calling C shouldn't be any different from Cobol or Fortran. The VMS calling standard is specifically designed to make intra language calls possible, and relatively painless.

The biggest issue is matching data types between the languages. There should be a better match for data types between Java and C, as you may not need to worry about string descriptors and packed decimal types.

Start with your existing Fortran example, and replace the Fortran routine with a C equivalent. Make sure you can receive and return all the data types you're interested in.

I'd expect far more people to be calling C from Java than Cobol. Chances are, the reason you can't find any example code is it's too easy!
A crucible of informative mistakes
Guy Peleg
Respected Contributor

Re: calling C from java

Sami,

Please contact me offline and I would
be happy to provide you with few examples
that call C from Java.

Regards,

Guy Peleg
Maklee Engineering
www.maklee.com
guy.peleg@company_name.com
SAMI AHMAD
Regular Advisor

Re: calling C from java

whats your email address?
Hoff
Honored Contributor

Re: calling C from java

What is Guy's address? Since you're looking for help with the JNI and that's going to put you rather deep into the Java and C coding requirements, here is the answer to your question in the form of some C coding practice.

:-)

$ type y.c
#include
main() { printf("guy.peleg@%s.com","maklee"); return 1; }
$
$ cc y
$ link y
$ run y
[the answer appears here]
$

SAMI AHMAD
Regular Advisor

Re: calling C from java

iam willing to take the challange :)
thanks
Jean-François Piéronne
Trusted Contributor

Re: calling C from java

Sami,

you can also use SWIG, which generates the jackets routines: http://www.swig.org/

SWIG 1.3.31 has been ported on OpenVMS and I have extensively used it without any problem (but not with java...)

JF
SAMI AHMAD
Regular Advisor

Re: calling C from java

based on Guy Peleg's example I am trying to put together this extern C program call, can someone point out why iam unable to compile ?
Guy thanks for your valuable example to give me a starting point.


SUNNY2$ type snd_snmp.java
class SYSTEMCALL {
public native int SNMPTRAP (String msg);
static {
System.loadLibrary("SNDSNMP_C");
}
public static void main (String[] args) {

System.out.println("This is java ..calling the C routing");
int result = new SYSTEMCALL().SNMPTRAP("TEST TRAP MSG");
System.out.println("for now lets omit this");
}
}



$ javac snd_snmp.java

$ javah -jni "SYSTEMCALL"

$TYPE SYSTEMCALL.H

/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class SYSTEMCALL */

#ifndef _Included_SYSTEMCALL
#define _Included_SYSTEMCALL
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: SYSTEMCALL
* Method: SNMPTRAP
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_SYSTEMCALL_SNMPTRAP
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif



SUNNY2$ type sndsnmp_c.c
#include "SYSTEMCALL.H"
#include
#include
#include

#define size 512
static char cmdbuffer[size];

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

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

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\"";

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);
}
SUNNY2$

SUNNY2$ cc /prefix=all/float=ieee/ieee=denorm/defin=JIT_OPTION/names=(short,as_is)
/reent=multithread/includ=SYS$COMMON:[JAVA$142.INCLUDE...] SNDSNMP_C

SUNNY2$ cc /prefix=all/float=ieee/ieee=denorm/defin=JIT_OPTION/names=(short,as_is) /reent=multithread/includ=SYS$COMMON:[JAVA$142.
INCLUDE...] SNDSNMP_C

strncat (cmdbuffer, cmd,size);
.............^
%CC-W-PTRMISMATCH, In this statement, the referenced type of the pointer value "cmdbuffer" is "struct _jobject", which is not compat
ible with "char".
at line number 18 in file DISK$ORACLE:[ORACLE9I]SNDSNMP_C.C;18