• Post Reply Bookmark Topic Watch Topic
  • New Topic

jni.h

 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,
I am trying to create a header file from a java program (to integrate a C code with java)

the prblem is that when I compile my c code it does not find <jni.h>

where is this fine supposed to be ?
Is something wrong with the class path?
where can I get this file ?

Thank you
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The jni.h file is under the {JDK_HOME}/include folder. You must specify this folder when compiling your C code.

Example with gcc in command line:
gcc -I $JAVA_HOME/include ...
Example with Ms compiler in command line:
cl.exe /I %JAVA_HOME%\include ...

Regards
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
That works.
Now I have another problem.
I created a library but the java code does not know where to find it.
I know I have to make it in some path but I have no idea where I have to put.
pleaaaaaaaaaaaaaaase help.
I am so close to make this work, the last step is to make the path of the library know that run the program.

Here are the error I got when running:


C:\Documents and Settings\Administrator\Desktop\Time>java CPUJavaTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: no libCPUJavaTest in
java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at CPUJavaTest.<clinit>(CPUJavaTest.java:8)


Thank you.
Regards,
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where it says "in java.library.path", that means in the directory referred to by that system property. Here's a useful little bit of code:Run that. It will list out all your system properties. One of them will be java.library.path, and its value will tell you where to put your compiled library.
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did.
I got that java.library.path = c:/windows/system32
so I put my complied library there but still same error.
It does not locate the library.
Do I have to restart the system or what ?

Please help.
thanks
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have overcome this problem using System.load() rather than System.loadLibrary().

Now it gives me the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: cpuCount
at CPUJavaTest.cpuCount(Native Method)
at CPUJavaTest.main(CPUJavaTest.java:19)


My C code is as follow:


and my java code is :


I have attached evrything you may need to find where this error come from.

PLease help me!!!

Thank you.
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1- Don't name your native library libCPUJavaTest.so. Name it: libCPUJavaTest.dll. This is how the dynamic libraries are named on Windows.

2- Question: Which C compiler do you use?

Regards
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gcc
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even when I named it .dll it doesn't find it.

Thanks alot.
 
Henry Wong
author
Sheriff
Posts: 22516
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by aymane chetibi:
even when I named it .dll it doesn't find it.

Thanks alot.


Try... "CPUJavaTest.dll".

Henry
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work either !!!
Here is exactly what I do :

1)) javac CPUJavaTest.java (to compile java code)
2) javah -jni CPUJavaTest (to create header file)
3) gcc -o libCPUJavaTest.dll -O -shared -fPIC CPUJavaTest.c -I C:\j2sdk1.4.2_03\include\win32 -I c:\j2sdk1.4.2_03\include

What are the things (given the code I provided before) that I should change ???

Also I have noticed the following difference:

In this first one, I used : System.loadLibrary("CPUJavaTest.dll"); (with .dll extention)


C:\Documents and Settings\Administrator\Desktop\Time>javac CPUJavaTest.java
C:\Documents and Settings\Administrator\Desktop\Time>javah -jni CPUJavaTest
C:\Documents and Settings\Administrator\Desktop\Time>gcc -o CPUJavaTest.dll -O -
shared -fPIC CPUJavaTest.c -I C:\j2sdk1.4.2_03\include\win32 -I c:\j2sdk1.4.2_03
\include
cc1.exe: warning: -fPIC ignored for target (all code is position independent)

C:\Documents and Settings\Administrator\Desktop\Time>java CPUJavaTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: no CPUJavaTest.dll in
java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at CPUJavaTest.<clinit>(CPUJavaTest.java:8)


In this second one I used : System.loadLibrary("CPUJavaTest"); (without extention)
and we see that it no more gives the error of not finding the library on the java.library.path but rather the method cpuCount is the problem.


C:\Documents and Settings\Administrator\Desktop\Time>javac CPUJavaTest.java
C:\Documents and Settings\Administrator\Desktop\Time>javah -jni CPUJavaTest
C:\Documents and Settings\Administrator\Desktop\Time>gcc -o CPUJavaTest.dll -O -
shared -fPIC CPUJavaTest.c -I C:\j2sdk1.4.2_03\include\win32 -I c:\j2sdk1.4.2_03
\include
cc1.exe: warning: -fPIC ignored for target (all code is position independent)
C:\Documents and Settings\Administrator\Desktop\Time>java CPUJavaTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: cpuCount
at CPUJavaTest.cpuCount(Native Method)
at CPUJavaTest.main(CPUJavaTest.java:17)



PLeaaaaaaaase help.

Thanks!!!
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re-write the CPUJavaTest.java file:

Re-write the CPUJavaTest.c file:

Then do the following:

1) javac CPUJavaTest.java
2) javah -jni CPUJavaTest
3) gcc -o libCPUJavaTest.dll -mno-cygwin -shared -Wl,--add-stdcall-alias CPUJavaTest.c -I C:\j2sdk1.4.2_03\include\win32 -I c:\j2sdk1.4.2_03\include

Before executing the Java class be sure that libCPUJavaTest.dll is either in the same folder -or- dll folder specified in the Path -or- dll folder specified in the java.library.path Java property.
You could put the dll on Windows\System32 but ** that is not a good idea **.

So do either:

1) CPUJavaTest.class and libCPUJavaTest.dll are in the same folder abc:
> cd abc
> java CPUJavaTest

2) CPUJavaTest.class is in folder abc and libCPUJavaTest.dll is in folder def:
> set Path=def;%Path%
> cd abc
> java CPUJavaTest

3) CPUJavaTest.class is in folder abc and libCPUJavaTest.dll is in folder def:
> cd abc
> java -Djava.library.path=def CPUJavaTest

Regards

[ May 12, 2006: Message edited by: Jean-Francois Briere ]

[ May 12, 2006: Message edited by: Jean-Francois Briere ]
[ May 12, 2006: Message edited by: Jean-Francois Briere ]
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a loooooooooooooooooooooooooooooooooooooooooooooooot!
I have no idea what all the options there are for, but it worked.

Do you how to make it work on linux ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!