This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes IDEs, Version Control and other tools and the fly likes Eclipse and JNI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Eclipse and JNI" Watch "Eclipse and JNI" New topic

Eclipse and JNI

Randall Fairman

Joined: Apr 18, 2011
Posts: 29
How does one indicate to Eclipse where to find libraries that contain native code? Everything compiles just fine -- I end up with .class files and .dll files just where I want them: in the project's bin directory. In the bin directory, I have Test.dll and nativetest/Test.class. The Test class calls a function in Test.dll that prints "hello". This works if I go to the bin directory (under cygwin) and type "java nativetest.Test", but when I do a "Run" in Eclipse, I get an UnsatisfiedLinkError. It can't find Test.dll.

I tried various settings under Run->Run Configurations, but no luck.
Randall Fairman

Joined: Apr 18, 2011
Posts: 29

Here's a small update and a few more details. In general, a .dll can be loaded with System.load() or System.loadLibrary(). load() takes a relative path and loadLibrary() takes an absolute path.

Under Eclipse, running (which does nothing but call a C function in Test.dll to print "hello") where the library is loaded with System.load("Test") produces a dialog box with "Could not find the main class: nativetest.Test. Program will exit" and in the console Eclipse reports

java.lang.UnsatisfiedLinkError: no Test 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 nativetest.Test.<clinit>(

The entire thing works fine by typing "java nativetest.Test" under cygwin.

Now, if I change to load the .dll with System.loadLibrary([long absolute path down to Test.dll]), then it does work under Eclipse.

It's nice that it can be made to work under Eclipse *somehow*, but this is hardly a robust solution. Anyone know how to make Eclipse work with System.load()?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170

Project->Properties->Java Build Path->Libraries->Add Library->User Library (follow on screen instructions to define your new library)
When done, you should see it in the Libraries list
Expand new library->Native library location->Edit

This question would be more suitable on the IDE forum. Moving..

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Randall Fairman

Joined: Apr 18, 2011
Posts: 29
It works! I could have sworn that I tried that. Thanks.
Randall Fairman

Joined: Apr 18, 2011
Posts: 29

Well, I thought that I had this solved, but now it's acting up again with a new project. First, understand that the program works outside of Eclipse. In the project's bin directory there's an rsf package, which contains the Main class, which calls System.loadLibrary("MyNative"). In the bin directory there's also MyNative.dll. Using the MinGW command-tool, if I cd to the bin directory and type "java rsf.Main", then everything works. However, if I do Eclipse->Run, then I get an UnsatisfiedLinkError in the console.

I created a new project from scratch and did the following to attempt to include the MyNative.dll library.

Project->Properties->Java Build Path, Libraries tab, Add Library, User Library, next..., User Libraries

This brings up a new window, where the only option is to choose "New..." I name it "MyNative" (but don't click "System Library" since it's not one).

Back to Project->Properties->Java Build Path, Libraries tab, where I see that when "MyNative" is opened, it says "Native library location: (none)." Click on that and edit the location, setting it to the bin directory of the project (where MyNative.dll sits). It doesn't matter whether I set this with "Workspace..." or with "External Folder..." Either way, it doesn't work. I tried telling the project that "MyNative" is a system library (even though it's not) and that doesn't work either.

Randall Fairman

Joined: Apr 18, 2011
Posts: 29
OK, I jumped the gun. It was an issue of gcc versus g++ and had nothing to do with Eclipse.

When compiling with g++ (and not gcc), I needed the -static switch. Otherwise, I think the unsatisfied link error arises because the runtime can't find the C++ standard libraries in the .dll.
I agree. Here's the link:
subject: Eclipse and JNI
Similar Threads
Help getting started?
Could not find the main class.
Beginning Lucene
JNI Dependent Libraries Not Found