- Joined
- Jan 7, 2009
- Messages
- 2
- Reaction score
- 0
Hi World,
In the below u can find a sample Java™ program that calls a native method, which has a C implementation:
OS : HP-UX IA64
java : 1.6
Here is the sample program.
####################################
//
// File TestJava2CallingNative.java
//
class TestJava2CallingNative {
native static void sayHelloWorld();
public static void main(String args[])
{
String libname = args[0];
try {
System.loadLibrary(libname);
System.out.println("Library " +
libname + " successfully loaded");
}
catch (UnsatisfiedLinkError Err) {
System.out.println("error: " + Err);
return;
}
System.out.println("Calling sayHelloWorld");
sayHelloWorld();
System.out.println("All done");
}
}
Compile this class:
$ <java_dir>/bin/javac -verbose TestJava2CallingNative.java
Output:
TestJava2CallingNative.class
Generate the JNI header file for this class. You must have the current directory in your CLASSPATH for the javah command to find your newly compiled class file.
$ <java_dir>/bin/javah -verbose -jni TestJava2CallingNative
Output:
TestJava2CallingNative.h
#########################################
Here is the sample c code
Here is the sample C native method implementation for sayHelloWorld:
/*
* File cImpl.c
*/
#include "TestJava2CallingNative.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_TestJava2CallingNative_sayHelloWorld(JNIEnv *env, jclass class)
{
printf("C says HelloWorld via stdio\n");
}
To compile this C source file:
$ cc -Ae +u4 +z -c -mt -I<java_dir>/include \
-I<java_dir>/include/hp-ux cImpl.c
Output:
cImpl.o
Create the shared library containing the native method implementation:
ld -b -o libcImpl.so cImpl.o ( Is this creation correct for HP-UX IA64 )
Output:
libcImpl.so
Have set the path as :
To execute the Java™ program, you must set the SHLIB_PATH environment variable to contain the location of the directory that contains libcImpl.so
Once i set the PATH i did the following steps
$ export SHLIB_PATH=.:$SHLIB_PATH
$ <java_dir>/bin/java TestJava2CallingNative cImpl
This is what i have done...
While extecuting the
$ <java_dir>/bin/java TestJava2CallingNative cImpl
above line i got the error message as
# java TestJava2CallingNative cImpl
error: java.lang.UnsatisfiedLinkError: Can't load library: /home/nalgo01/cImpl
what is the solution for this. Does i need to set any other options while compiling or linking.
Make sure i am using HP-UX IA64.
Any help is apreceated
Thanks in advance
Gopi
In the below u can find a sample Java™ program that calls a native method, which has a C implementation:
OS : HP-UX IA64
java : 1.6
Here is the sample program.
####################################
//
// File TestJava2CallingNative.java
//
class TestJava2CallingNative {
native static void sayHelloWorld();
public static void main(String args[])
{
String libname = args[0];
try {
System.loadLibrary(libname);
System.out.println("Library " +
libname + " successfully loaded");
}
catch (UnsatisfiedLinkError Err) {
System.out.println("error: " + Err);
return;
}
System.out.println("Calling sayHelloWorld");
sayHelloWorld();
System.out.println("All done");
}
}
Compile this class:
$ <java_dir>/bin/javac -verbose TestJava2CallingNative.java
Output:
TestJava2CallingNative.class
Generate the JNI header file for this class. You must have the current directory in your CLASSPATH for the javah command to find your newly compiled class file.
$ <java_dir>/bin/javah -verbose -jni TestJava2CallingNative
Output:
TestJava2CallingNative.h
#########################################
Here is the sample c code
Here is the sample C native method implementation for sayHelloWorld:
/*
* File cImpl.c
*/
#include "TestJava2CallingNative.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_TestJava2CallingNative_sayHelloWorld(JNIEnv *env, jclass class)
{
printf("C says HelloWorld via stdio\n");
}
To compile this C source file:
$ cc -Ae +u4 +z -c -mt -I<java_dir>/include \
-I<java_dir>/include/hp-ux cImpl.c
Output:
cImpl.o
Create the shared library containing the native method implementation:
ld -b -o libcImpl.so cImpl.o ( Is this creation correct for HP-UX IA64 )
Output:
libcImpl.so
Have set the path as :
To execute the Java™ program, you must set the SHLIB_PATH environment variable to contain the location of the directory that contains libcImpl.so
Once i set the PATH i did the following steps
$ export SHLIB_PATH=.:$SHLIB_PATH
$ <java_dir>/bin/java TestJava2CallingNative cImpl
This is what i have done...
While extecuting the
$ <java_dir>/bin/java TestJava2CallingNative cImpl
above line i got the error message as
# java TestJava2CallingNative cImpl
error: java.lang.UnsatisfiedLinkError: Can't load library: /home/nalgo01/cImpl
what is the solution for this. Does i need to set any other options while compiling or linking.
Make sure i am using HP-UX IA64.
Any help is apreceated
Thanks in advance
Gopi