How do you link a native library to a jar in IntelliJ? - java

I'm trying to get OpenCV set up in IntelliJ, however I'm stuck on figuring out how tell IntelliJ where to find the native library location.
In Eclipse, after adding a jar, you can set the location of the Native library in the Build Config screen.
e.g.
Does anyone know how to do this in IntelliJ IDE?

I'm not sure you need it as a dependency, you just need to add the option to the VM invocation so the library is found at run-time.
At least in the first sample I have looked at (samples/java/ant/SimpleSample) the native library is loaded at runtime with these lines:
So, the library is not a compiM to find the native library at run-time, specify the java.library.path to point to the directory where the library can be found, using the -D VM option - in your IntelliJ run configuration.
NOTE: If on Mac OS X you need to create a copy/link of the .so shared library built to one with the same name, but the .dynlib extension

Once the library is added, you can set the Native Library Location as follows:
Make sure the library is selected (highlighted). In my case, there only is one Library called lwjgl.
On the right panel, select the little + Symbol (below "Name" and above "Classes")
Select the directory you want to specify as Native Library Location and confirm
Select Native Library Location in the next dialog.

File-->Project Structure-->Modules-->Select Dependencies Tab-->Select Module Source-->at the right corner green '+' button-->Library-->new Library-->java-->select External jar file-->OK-->add Selected
or
Without selecting library from + button directly select jar dependencies
here are some screenshots

https://stackoverflow.com/a/7148086/222867
Windows uses the PATH variable instead of LD_LIBRARY_PATH to load DLL's. I set the PATH environment variable appropriately and then restart Idea.
This however, may not be the most convenient option

Related

Java System.load(libName) looks for wrong lib name

I've run into a strange behaviour on one of our customers machine when trying to load a JNI library at runtime.
When trying to load a library with
System.load("libtestlibrary.so")
I get a
liblibtestlibrary.so.so: cannot open shared object file: No such file or directory
It seems like the given library name is (wrongly) prefixed with "lib" and suffixed with ".so" and thus our library cannot be found.
This only happens on one machine (AIX 6). I was unable to reproduce this behaviour on our own AIX 6 box using the exact same JRE, so I'm guessing this is caused by some OS settings on the customer machine.
Does anybody have an idea how to turn this off?
You can use either:
System.loadLibrary("libraryname");
It will be properly expanded to system specific notation. Or you can use:
System.load("you_can_have_anything_you_like_here")
In second case, all you need to do is to point to proper location of file. If you are looking for cross system development, you might be interested in packaging your native code inside JAR. Take a look here for a sample code where library is taken from JAR file
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo031
Also, make sure that other libraries are available in LD_LIBRARY_PATH. Note that java.library.path is only valid for your "first" native lib. If your code uses some other shared libs, you have to either compile with rpath or make sure libraries are visible system wide.
Yet another approach is to go via stub: http://jnicookbook.owsiak.org/recipe-No-018/
In that case, you can load library as you like and do whatever you like (e.g. change native code on the fly).

How do I add LWJGL 3.1.1 to netbeans to use in a Java project?

I want to use the Light Weight Java Gaming Library(LWJGL) to my Netbeans so I can use it in my Java application. The only videos that I can find show the zip file that they downloaded with separate src and doc folders inside of it. The zip file that I download has everything in one directory. I went to lwjgl.org/download and clicked on Stable and then Generate Bundle. What am I missing?
I had the same problem recently.
So, to begin you want to go to Tools in the context menu and select Libraries (as shown). Next you can add a new library and name it e.g. LWJGL-3.1.1 confirm with ok. You can find 3 tabs in the current window Classpath, Sources and Javadoc. There you add your jar files accordingly (in the downloaded .zip file you find .jar files with different names like lwjgl-{whatever}-sources.jar or lwjgl-{whatever}-javadocs.jar) make sure you put them in the right place. You have to repeat this process for all of the jar files you want. The javadoc files are not required but recommended. Make sure you also collect all the native .dll files and merge them in a folder called \natives. You find them in these jar files that are called like: lwjgl-{whatever}-{your-OS}.jar.
Once you have finished the setup for your library right click on your current project and choose Add Library.... In the window that pops up you scroll down until you find your library that you have just created and you are almost done now.
Last but not least go to the project settings. Select Run and make sure that you set the classpath in VM options to something like in the image: -Djava.library.path="C:\java_workspace\LWJGL Library 3.1\natives. Now this classpath tells netbeans where your native files are located. Your \natives folder that you should have created in the beginning is where this path should lead to. That's it. This is all you have to do for a setup without the use of maven, gradle, ...
You can test if it is working with the code provided by LWJGL HelloWorld example.
I hope this solved your problem.
Best regards.

Installing Java API for GPIB devices in eclipse

This is probably a trivial question, but I am having trouble installing a Java API called JPIB_Windows into eclipse which should allow me to control external devices connected via GPIB.
This is the contents of the folder that I downloaded.
I created a new project in Eclipse, right clicked the project and went to build path -> configure build path.
I then clicked on add external libraries and added the JPIB.jar file. The file was added, but I am still not able to use the classes in the API.
Is there something else that I need to do to be able to use the API? Is there a better way of importing this API into my project?
You jar file is installed correctly. JPIB (and many other Java libraries) is just wrapper for low-level routines. So it is mandatory to load corresponding native libraries before using Java classes.
In Eclipse go to the Run > Run Configurations... > Arguments tab, select configuration for, perhaps, main method. Then specify in VM arguments field:
-Djava.library.path=C:\path\to\jpib\dll
Then add at the beginning of main() the following line:
System.loadLibrary("jpib_32");
Then run already edited configuration. Everything should be OK. But in case of failure you can examine path to DLL:
System.out.println("Libary path: " + System.getProperty("java.library.path"));
Also working directory can be specified in the same tab, avoiding absolute path, but for the first time absolute path is simple and less error-prone.
Also note that Java may not recognize Windows-specific issues (missing drivers, insufficient user privileges, wrong DLL version) and will report about general error.

UnsatisfiedLinkError - Unable to load library - Native library not found in resource path

I have the following error at runtime, while trying to run Tess4J:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract302': Native library (win32-x86-64/libtesseract302.dll) not found in resource path ([myproject/target/classes/, ...some jars...])
My questions are:
1) What exactly it tries to find and where?
2) Why is it apparently searches for myproject/target/classes/ directory? I set it nowhere.
3) Why is it ignores "native directory path" I set for tess4j.jar in user library descripto in Eclipse? My DLLs are there. If it didn't ignore the path, it would find DLLs.
4) Why is it apparently prepending DLL name with win32-x86-64/? I set this nowhere. Is this standard prefix of some API?
5) What is "resource path"? How to set it?
Like the error says, it's looking for win32-x86-64/libtesseract302.dll in java.class.path. Part of your classpath apparently includes myproject/target/classes.
The prefix represents the platform and architecture of the shared library to be loaded, which allows shared libraries for different targets to be included in the same archive. If JNA cannot find the requested library name in the system load path, then it attempts to find it within your resource path (extracting it, if necessary). So if you put the DLL in a jar file, you'll need to give it the win32-x86-64 prefix in order for it to load.
The "resource path" is nominally your class path; basically anywhere reachable by ClassLoader.getResource().
The error stems from your trying to load 32-bit DLLs in 64-bit JVM. The possible solution is switch to 32-bit JVM; alternatively, use 64-bit Tesseract and Leptonica DLLs.
Had the same issue, sorted with the following lines
System.load("/usr/local/lib/liblept.so.5")
System.loadLibrary("tesseract")
For your case, it might be different libraries but in the end is pretty much the same: just load the libraries that you need manually.
Why don't you use JNA API http://www.java2s.com/Code/Jar/j/Downloadjna351jar.htm to load native library? Once you putted into your project classpath, you add this code
NativeLibrary.addSearchPath("libtesseract302", "your native lib path"); make sure you have this libtesseract302.dll file, normally it is located at windows32 folder.
For example, if your libtesseract302.dll file in somewhere c:/abcv/aaa/libtesseract302.dll then you just set the path like this NativeLibrary.addSearchPath("libtesseract302", "c:/abcv/aaa");
I don't know how windows path look like either c:/abcv/aaa or c:\\abcv\\aaa\\
if you want easier way, just put all your necessary dll file into your windows32 folder, JVM will take care of it.
Another issue might be you were not installing the application correctly or the application version is unmatch with your jar version. try to install the latest application and download the latest jar to try again. Hope it helps :)
I had the same problem and found that this "resource path" is not set by "native directory path" .
You can however add new folders to it by using "Add External Class Folder" in the Library tab, even if this folder does not contain any class file but native library files(like DLL on Windows)
A few days ago I ran into the same error message when trying to load a C++ DLL with JNA. It turned out that the cause was a missing DLL that my DLL depended on.
In my case it was the MS Visual Studio 2012 redistributable, which I then downloaded and installed on the machine and the problem was gone. Try using Dependency Walker to find any missing libraries and install them.
I think an easier way to get around this error would be to revert to an earlier version where you were not getting this error. Right click on the project folder and navigate to local history to revert to an earlier version. I verified this workaround on the android studio installed on Mac OS Big sur.

Netbeans: need to include 3'rd party directory in classpath

We have an application developed in NetBeans, based on the NetBeans platform. There's a 3'rd party program that we have a runtime dependency on, more specifically a jar in the other progams lib folder.
How should we include the other progam's jar in our classpath?
The recommendation from the other progam's manufacturer is to set environment variable CLASSPATH to include
C:\Progam Files\Other Program\lib\theJAR.jar
And if that's not possible, we should copy theJAR.jar to JRE-HOME\lib\ext
We must not copy theJAR.jar anywhere else, that might cause it to stop working...
Now NetBeans takes no notice of what's on environment variable CLASSPATH. Their recommended way seems to be to make a wrapper, but that would lead to copying the jar, unless there's some way to make a wrapper module that points to CLASSPATH?
At the moment we are copying the jar into JRE-HOME\lib\ext. But then there's unnecessary hassle when we install some java update.
Do you have any solution to this problem? It seems like something that might be simple, but I haven't found the right place to look yet...
Edit: The application is ant-based.
From the documentation for the Module System API's overview of the runtime infrastructure (bottom of the page under the section "Common Problems and Solutions"):
Q: Can my module add a library JAR to the classpath from outside the
IDE [read: platform] installation?...
A: Not easily. You have a few options:
Add an entry to ide.cfg [your app's .config file]. For example:
-cp:a c:\eak\lib\eak.jar This startup file provides the ability to add classpath entries to the IDE's Java invocation.
...
It goes on to list two more options. The second option is the same solution you've come up with. The third is to "partition your module and use a new classloader" which I can't recommend either way since I have no experience doing this (but it's worth a read).
Assuming that this first option is what you are looking for, you will need to add a custom .conf file to your project and point to it in your project.properties file, like so: app.conf=nbproject/my.conf. This will add your custom .conf file to your app's install directory instead of the default config file that is normally added. Of course, you'll need to add the -cp:a c:\eak\lib\eak.jar entry to this custom config file in order to load the .jar.
During development you'll need to add the following entry to the project.properties file: run.args.extra=-cp:a c:\eak\lib\eak.jar. This will pass the command line option to your debug instance.
You can add that .jar file by following the steps below:
In the left side panel named "Projects" open your project and right click on the "Libraries", then select "Add JAR/Folder...".
Then select your .jar file from the location where you have stored it in the next dialog box that opens and then press "Open".
Vola Finished!!! (with the same process you can add other libraries also like JavaCV, JMF,etc)
And Now You Can Use That .Jar File From Your Project Library.
Hope It Helps.

Categories

Resources