• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

JNI error

 
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jiafan Zhou
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The gcc I am using now is:


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

Regards,
Jiafan
 
Jiafan Zhou
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LD_LIBRARY_PATH is a Unix thing only.

Use the -Djava.library.path=<path> Java command-line switch.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still have the following error..


Regards,
Jiafan
 
Jiafan Zhou
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cant agree more.

Jiafan
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic