LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2011, 09:06 AM   #1
dipak1296
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Rep: Reputation: Disabled
Unhappy head to java native interface(c methods)


i have been working on example of jni.......on ubuntu 10.04
after making c file according to jni interface .h file, i compiled it to get .so file(static library)...but unable to run the native code....here is the piece of work what i done...............


1) ReadFile.java

import java.util.*;

class ReadFile {
//Native method declaration
native byte[] loadFile(String name);
//Load the library
static {
System.loadLibrary("nativelib");
}

public static void main(String args[]) {
byte buf[];
//Create class instance
ReadFile mappedFile=new ReadFile();
//Call native method to load ReadFile.java
buf=mappedFile.loadFile("ReadFile.java");
//Print contents of ReadFile.java
for(int i=0;i<buf.length;i++) {
System.out.print((char)buf[i]);
}
}
}



2) compile java file

3) javah -jni ReadFile
and i get ReadFile.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ReadFile */

#ifndef _Included_ReadFile
#define _Included_ReadFile
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ReadFile
* Method: loadFile
* Signature: (Ljava/lang/String[B
*/
JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif


4) and made a c file with nativelib.c and interfaces defined in .h
#include <jni.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv * env, jobject jobj, jstring name) {
caddr_t m;
jbyteArray jb;
jboolean iscopy;
struct stat finfo;
const char *mfile = (*env)->GetStringUTFChars(
env, name, &iscopy);
int fd = open(mfile, O_RDONLY);

if (fd == -1) {
printf("Could not open %s\n", mfile);
}
lstat(mfile, &finfo);
m = mmap((caddr_t) 0, finfo.st_size,
PROT_READ, MAP_PRIVATE, fd, 0);
if (m == (caddr_t)-1) {
printf("Could not mmap %s\n", mfile);
return(0);
}
jb=(*env)->NewByteArray(env, finfo.st_size);
(*env)->SetByteArrayRegion(env, jb, 0,
finfo.st_size, (jbyte *)m);
close(fd);
(*env)->ReleaseStringUTFChars(env, name, mfile);
printf("hurray........done...\n");
return (jb);
}


5) then i execute this command to generate staticlibrary

gcc -I/usr/lib/jni -I/usr/lib/jvm/java-1.6.0-openjdk -

I/usr/lib/jvm/java-1.6.0-openjdk/include -shared -o libnativelib.so nativelib.c

please check whether this is correct or not....it generates static library with name libnativelib.so

6) then i set paths with commands
LD_LIBRARY_PATH=`pwd`
export LD_LIBRARY_PATH


7) at the end when i run the java class with command
java ReadFile it gives following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no nativelib in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at ReadFile.<clinit>(ReadFile.java:8)
Could not find the main class: ReadFile. Program will exit.

please tell me where i lack
thank you in advance
 
Old 08-17-2011, 04:14 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The process works for me, but then I'm using Sun (Oracle) Java, not OpenJDK.

For Step 5, I used the following command:
Code:
gcc -I/usr/lib/jni -I/usr/lib/jvm/java-6-sun-1.6.0.26 -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux -shared -o libnativelib.so nativelib.c
 
Old 08-18-2011, 01:08 AM   #3
dipak1296
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
The process works for me, but then I'm using Sun (Oracle) Java, not OpenJDK.

For Step 5, I used the following command:
Code:

gcc -I/usr/lib/jni -I/usr/lib/jvm/java-6-sun-1.6.0.26 -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux -shared -o libnativelib.so nativelib.c

------------------------------------------------------------------------------------------------------

dwhitney67 thanks for your reply

this step 5 works for me too it's creates a libnativelib.so file, but after that when i tried to execute java code then it is giving exception like

6) then i set paths with commands
LD_LIBRARY_PATH=`pwd`
export LD_LIBRARY_PATH


7) at the end when i run the java class with command
java ReadFile it gives following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no nativelib in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at ReadFile.<clinit>(ReadFile.java:8)
Could not find the main class: ReadFile. Program will exit.
 
Old 08-18-2011, 03:57 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I presume that you performed Steps 1-7 in the same terminal (eg. gnome-terminal) window?

I've Googled and see that some people have created bug reports against openJDK with respect to JNI. Perhaps you have fallen victim to this bug too.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Disabling HEAD, OPTIONS HTTP METHODS in Apache Webserver ankit.thakkar Linux - Security 8 01-28-2020 08:01 AM
LXer: Google Web Toolkit’s JavaScript Native Interface LXer Syndicated Linux News 0 09-21-2007 05:51 AM
how to avoid writing every methods for a listener in a java program ?? alred Programming 2 05-24-2006 10:41 PM
difference between Java compare and compareTo methods kpachopoulos Programming 2 09-19-2004 04:08 AM
java -- methods -- please help Laptop2250 Programming 2 12-13-2003 12:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration