Calling legacy C Functions from Java
hi,
All the libraries are on the same path I mean current path where I am executing the existing c as well as my experimentations.
OS = HP-UX
Existing Functionality:
I make a call to "int PE(char *pReport, char *pOutput)" in "parseexp.c" from "testexp.c".
*************************** parseexp.c ******************************
"int PE(char *pReport, char *pOutput)
{
.................
................
} "
***************************************************************************
******************************* testexp.c *********************************
char outrec[20000];
int ret;
if (ret=PE("",outrec)) {
printf("Error: %d\n",ret);
} else {
printf("%s\n",outrec);
}
***************************************************************************
the "outrec" variable is populated with some xml data.
My implementation:
I have written a wrapper to call the same c function and here is the code
************************** HelloWorld.c *****************************
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world! namskara \n");
jchar outrec[20000];
jint ret;
if (ret=PE(" some text here ",outrec))
{
printf("Error: %d\n",ret);
} else {
printf("%s\n",outrec);
}
***********************************************************************
when I run the i get the error message as follows...
Error Message:
******************************************************************************************
/usr/lib/dld.sl: Unresolved symbol: OutputTemplate (data) from ./libHelloWorldLib.sl
/usr/lib/dld.sl: Unresolved module for symbol: ParseExperian (code) from ./libHelloWorldLib.sl
/usr/lib/dld.slAbort(coredump)
******************************************************************************************
and all my files java's, c's , object libs, shared libs are all reside in the same directory.
I use following commands to
1) javac HelloWorld.java
2) javah -jni HelloWorld
3) cc -Ae +u4 +z -c -D_HPUX \ -I/opt/java1.4/include
-I/opt/java1.4/include/hp-ux \ HelloWorld.c
4) ld -b -o libHelloWorldLib.sl HelloWorld.o
5) export SHLIB_PATH=.;
6) java HelloWorld
I have few concern here
1) Are the data types I am passing to PE() are correct.if not what to pass?
2) Do I need to set any evnt variables?
cheersz
Tiger
> hi,
>
>
> All the libraries are on the same path I mean current
> path where I am executing the existing c as well as
> my experimentations.
>
> OS = HP-UX
>
> Existing Functionality:
>
> I make a call to "int PE(char *pReport, char
> *pOutput)" in "parseexp.c" from "testexp.c".
>
<snip>
> when I run the i get the error message as follows...
>
> Error Message:
> ******************************************************
> ************************************
> /usr/lib/dld.sl: Unresolved symbol: OutputTemplate
> (data) from ./libHelloWorldLib.sl
> /usr/lib/dld.sl: Unresolved module for symbol:
> ParseExperian (code) from ./libHelloWorldLib.sl
> /usr/lib/dld.slAbort(coredump)
>
<snip>
>
> I have few concern here
> 1) Are the data types I am passing to PE() are
> correct.if not what to pass?
> 2) Do I need to set any evnt variables?
Your implementation code is not the source of the problem. Note that the error message says that the symbol "OutputTemplate" cannot be found. In what shared library is this symbol? Perhaps the non-JNI version locates this library properly, but when you use:
export SHLIB_PATH=.;
the library is no longer found. Try
export SHLIB_PATH=$SHLIB_PATH:.
to append the current directory ot the SHLIB_PATH (assuming a colon is the proper path separator character)
>
> cheersz
> Tiger
Hi,
Thanks for the reply.
I tried with simple programme to add two numbers and return the result back to Java.
I have used the following commands now to execute the above.
javac Sum.java
javah -jni Sum
cc -Ae +u4 +z -c -D_HPUX \ -I/opt/java1.4/include -I/opt/java1.4/include/hp-ux \ sum.c (jni specific C programe)
cc -Ae +u4 +z -c -D_HPUX \ -I/opt/java1.4/include -I/opt/java1.4/include/hp-ux \ sum_implemented.c (this is where I have the add function implemented and is non jni c programe)
ld -b -o libSumLib.sl sum.o sum_implemented.o
export SHLIB_PATH=.;
java Sum
But in the similar manner when I tried to execute the existing C program its failing and giving me different error messages each time I compile them.
But the worry here is, the function which I am trying to execute is an existing code and it works fine.There is test.c which calls this existing c and I get a output without any error.
THere are Two header files which are erraring out when I compile them saying "Size of struct or union member is unknown." but My worry is this is an existing code and works fine with pure c calls.
I use the following commands to compile ..
1) javac HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary("HelloWorldLib");
}
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
hw.displayHelloWorld();
}
}
__
2) javah -jni HelloWorld -- I create a header file
3) cc -Ae +u4 +z -c -D_HPUX \ -I/opt/java1.4/include -I/opt/java1.4/include/hp-ux \ HelloWorld.c
#include <jni.h>
#include "Sum.h"
#include <stdio.h>
#include "ParseUtils.h"// this is where
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world! namskara\n");
jchar outrec[20000];
jint ret;
if (ret=ParseExperian(" SOME TEXT GOES HERE ",outrec))
{
printf("Error: %d\n",ret);
} else {
printf("%s\n",outrec);
}
printf("Sum = %d \t\n ", sum(10,20));
printf("-");
}
4) cc -Ae +u4 +z -c -D_HPUX \ -I/opt/java1.4/include -I/opt/java1.4/include/hp-ux \ parseexp.c -- this is where I have an existing code which I am trying to execute
5) ld -b -o libHelloWorldLib.sl HelloWorld.o parseexp.o
6) export SHLIB_PATH=.;
7) java HelloWorld
cheersz
Tiger
THe way I am compiling these might be different.