aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes JNI error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JNI error" Watch "JNI error" New topic
Author

JNI error

Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

I followed the JNI tutorial and why everytime I use a gcc compiler to generate a dll in windows, I got the following error.



Kind regards,
Jiafan


SCJP, SCJD, SCWCD, SCBCD, SCEA
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

The gcc I am using now is:


I wonder if the gcc is compatiable with the JNI interface.

Regards,
Jiafan
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

I also downloaded the MS cl compiler and still does not work with the following exception.



Since in the basic c file I was writing, it definately includes the printf..so a stdio.h is definately used..why it complains about the stdio.h not found..

I love Java more than others.

Regards,
Jiafan
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Try hardcoding the include statement to where the ".h" file is. For example:

#include <D:\blah\blah\stdio.h>

(You may have to do this for other ones as well)
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

I fixed the stdio.h problem by explicitly add a set inthe AUTOEXEC.bat..

but I still have the problem when I link the object file into a dll...



Jiafan
[ September 18, 2006: Message edited by: Jiafan Zhou ]
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

I think I fixed the problem with the CL compiler..well I stll have a fatal problem at last..



Do I need to setup a LD_LIBRARY_PATH Environment Variable?( I have created one where I located my dll and exp files)

Regards,
Jiafan
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

LD_LIBRARY_PATH is a Unix thing only.

Use the -Djava.library.path=<path> Java command-line switch.


[Jess in Action][AskingGoodQuestions]
Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101
1- When using gcc you must add the following compiler/linker flags:

-D__int64="long long"
-shared
-Wl,--add-stdcall-alias

So that your command line should look like:

gcc -D__int64="long long" -I"C:\jdk1.5.0_06\include" -I"C:\jdk1.5.0_06\include\win32" -shared -Wl,--add-stdcall-alias -o hello.dll HelloWorldImp.c

Also if your are using gcc under Cygwin you should also use the flag -mno-cygwin to be able to run your program outside Cygwin.


2- When using MS cl.exe and link.exe you should allways first call vcvars32.bat located in the MSVC bin folder
(C:\Program Files\Microsoft Visual Studio 8\VC\bin for instance).
This script sets all the necessary variables to the appropriate values.
There is albsolutely NO NEED to set anything by hand.
And I wouln't call it in autoexec.bat since this will load your system/user environment at any time,
even when you are not building c/c++ files.


3- When you have successfully build your DLL you should either:
a- Put it in a folder already pointed by the Path enviroment variable (usually not a good idea)
b- Update the Path enviroment variable to add the folder where your DLL is located.
You could do that each time just before executing the Java program:
>set Path=yourDllFolder;%Path%
>java YourClass
You could also update your Path enviroment variable once and for all with the help of the 'System Properties' window.
c- (Preferably) Set the folder of your DLL while executing your Java program with the java.library.path property:
> java -Djava.library.path=yourDllFolder YourClass


Regards
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

I still have the following error..


Regards,
Jiafan
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

It works...

I made a silly mistake in the C program which does not make the function signature same in the .h file.

I will post the c program in a second for others to track the errors.


Notice that I used the fully qualified package modifier here, I ignored this for the first time and it ends up with errors in the runtime.

Thanks guys very much for the help.
[ September 19, 2006: Message edited by: Jiafan Zhou ]
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Originally posted by Jean-Francois Briere:
1- When using gcc you must add the following compiler/linker flags:

-D__int64="long long"
-shared
-Wl,--add-stdcall-alias

So that your command line should look like:

gcc -D__int64="long long" -I"C:\jdk1.5.0_06\include" -I"C:\jdk1.5.0_06\include\win32" -shared -Wl,--add-stdcall-alias -o hello.dll HelloWorldImp.c

Also if your are using gcc under Cygwin you should also use the flag -mno-cygwin to be able to run your program outside Cygwin.


2- When using MS cl.exe and link.exe you should allways first call vcvars32.bat located in the MSVC bin folder
(C:\Program Files\Microsoft Visual Studio 8\VC\bin for instance).
This script sets all the necessary variables to the appropriate values.
There is albsolutely NO NEED to set anything by hand.
And I wouln't call it in autoexec.bat since this will load your system/user environment at any time,
even when you are not building c/c++ files.


3- When you have successfully build your DLL you should either:
a- Put it in a folder already pointed by the Path enviroment variable (usually not a good idea)
b- Update the Path enviroment variable to add the folder where your DLL is located.
You could do that each time just before executing the Java program:
>set Path=yourDllFolder;%Path%
>java YourClass
You could also update your Path enviroment variable once and for all with the help of the 'System Properties' window.
c- (Preferably) Set the folder of your DLL while executing your Java program with the java.library.path property:
> java -Djava.library.path=yourDllFolder YourClass


Regards


This is an awesome post and I vote for it as one of the best/most informative posts ever.

It raises the question, however, as to why Sun's JNI tutorial doesn't mention any of this.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Dan Bizman:

It raises the question, however, as to why Sun's JNI tutorial doesn't mention any of this.


Because some of it is subjective opinion, and its validity depends on some unstated assumptions about versions of various things. Not saying it's not useful, but for some folks this recipe wouldn't work.

Note that Sun's JNI tutorial ain't there anymore -- I'd guess because providing compiler recipes which worked for everyone was getting too tricky.
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

I cant agree more.

Jiafan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JNI error