I have inherited a jar file with two JNI class files and related libraries (.so files). I am not familiar with c++ or JNI. How do I reference these in my Java project in Eclipse?
So far, I have created a Java project, added the jar file as an external jar to the buildpath and then have attached the folder containing the jni libraries using Properties -> Java Build Path -> Libraries tab, expand the jar file (containing the class files) and edit "Native library location".
I have a driver program in Java with a main method which makes the jni object. Eclipse says "ObjectName cannot be resolved to a type".
Why would this happen even though the class files are in the build path?
I have just put all the libraries under /src/main/resources.
Right click on the project and go to Properties>JavaBuildPath. In the Libraries section expand the jar you need the libraries for and click on "Native library location", then press "Edit..".
You just need to give the /src/main/resources path as location.
Then in the "Order and Export" section of the Properties>JavaBuildPath you need to make sure that /src/main/java is above /src/main/resources.
In the Java file, where you instanciate your JNI defined object, make sure you have imported the class files:
import your.jni.classes;
I suggest you have a look here, at the video:
http://mmbtools.crc.ca/content/view/53/78/
(look at this video on Youtube and set the resolution to 720px to see clearly)
It is a good step-by-step example of how to integrate JNI classes in a Java application, in Eclipse. It is pretty much what you're trying to do.
Hope it helps,
Related
I am trying to communicate to Arduino with Java, so I'm using the rxtx library. I've downloaded the library and placed the respective files in my java project's folder. I put the rxtxxjar in a lib folder in the java project's folder, and I left the serial.jnlib straight into the java project's folder. I've taken the SerialTest code off of the arduino website, and I've tried to run it but I keep getting the UnsatisfiedLinkError. I've researched this question, and I've seen people saying basically the following in different ways:
Go to Project -> properties -> Java build path ->Find your .jar. Click
on + to open the JAR's properties. Select Native library location and
edit it to point to RXTX's shared library (.DLL, .so, .dylib).
However, I have no idea how to do the pointing to the shared library, especially since I don't see any .dll, .so, or .dylib file, and I cannot use the .jnlib because it is greyed out. Also on a smaller note, by my .jar is that just the project? I work in Eclipse Java EE.
You can use
System.load("Absolute path to your jni library");
This works fine on my project.
I am doing JAVA project based on the SSJ (Stochastic Simulation in Java) libraries. In my project I have followed the required steps to import the SSJ library files and also the source files.
When looking at the SSJ library files in the project's Libraries branch, only class files appear shown, but when I open them I can view the source code (which I couldn't before, as I hadn't imported the sources) so I assume everything was imported correctly.
When I run the examples provided with the library everything runs fine and as expected. But if I try to change the source code for the libraries for instance, changing the text on an output, the changes I make to the source files aren't reflected in the output from the java programs.
Any hint on what I might be doing wrong?
I understand this might be hard to understand or test as I can't quite create and SSCCE for this matter. The library files are hosted here (http://www.iro.umontreal.ca/~simardr/ssj/ssj-20120216.zip) and the source code here (http://www.iro.umontreal.ca/~simardr/ssj/ssj-source.html).
Thanks in advance for all your help!
Adding source to libraries it's for debugging and documentation purposes. It does not mean that Netbeans will detect changes and recompile classes automatically.
You should create a project for the library itself to recreate it from source. Changes are that if you have the source there will be a build.xmlor a pom.xml that contains the steps to compile the library and will allow to create a Java Free Form Project or a Project with existing POM.
You'd then remove the reference to library itself and add a dependency to the project.
Other option would be to add the library source to the same project you are using (and, again, removing reference to the library) but I would not recommend this unless it's a very small simple library.
EDIT. Particular ssj information.
ssj is a library where the original source code is in .tex files and it requires several programs available to generate .java files and then compile them. All this information can be found in the COMPILE.txt original file.
Since the source distribution includes .java files it is possible to generate a .jar just using Netbeans but it requires to modify the build.xml to just compile Java source files. Also when creating the Java Free Form Project is necessary to select lib as the target to Build Project.
Here it's a minimal build xml for Netbeans 7.1 to compile from Java sources.
In order to create the new *.java file from the *.tex file, you need the tcode.jar which is available in the distribution. Have you added tcode.jar as an external jar to your project?
umontreal.iro.lecuyer.tcode.Texjava is in tcode.jar
I recently downloaded the Java JNA from https://github.com/twall/jna/servlets/ProjectDocumentList?folderID=7408&expandFolder=7408&folderID=0 and tried using it. However, as exemplified by the screenshot below, the demo source code provided in https://github.com/twall/jna/ won't compile, no matter where I place the files. Mind you, I've tried placing them both in the default package an every other combination of package mix-ups, and this is only the latest version. Does anybody know why the compiler can't find what is contained within the JAR file? (Also, as you can see, the class files are all in the jar file to be found)
Basically, the JAR should go in the classpath. In Netbeans, you can do it by adding it as library to the build path of the project through the project properties.
I am just starting to learn JNI. I have been following a simple example, and I have created a Java app that calls a Hello World method in a native library. I'd like to target Win32 and Linux x86.
My library resides in a DLL, and I can call it just fine using LoadLibrary when the DLL is added to the root of my Eclipse project.
However, I can't figure out how to get Eclipse to export a runnable JAR that includes the DLL and the .SO file for Linux.
So my question is basically; how would you go about creating a project in Eclipse and include several versions of the same native library?
Thank you,
Martin
For runnable JARs, what you need to do is extract to the temporary directory (maybe in a static { } block) and then load the DLL from that directory (using System.loadLibrary() in the same block). To do this, you need to subclass ClassLoader and override the findLibrary() method to allow libraries to be found in that directory. You can do whatever logic you need to here to load the particular platform libraries. To be honest, the naming for the libraries on the different platforms should be similar anyway -- I believe that you omit the 'lib' part when loading, and the extension. That's the gist of it. Probably easier to use One-JAR as the other poster mentioned :)
You might want to check out the One-JAR project. It lets you package your application and its dependencies (including native libraries) to a single jar file.
I'm trying to use an open source java library to visualize nodes and edges in a graph, but I'm completely lost.
I have a bunch of jar files in a folder. Clicking on some of the jar files makes java swing windows pop open with graphs displayed. Clicking other jar files does nothing.
If I figured that out, would I just stick the jar files in there with the other ones, or would that still not work?
And if I ever figure out how to use these files, does that mean that I have to include them if I transfer my java project to another computer? How would I go about doing that?
I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. Figuring out the classpath can be confusing, but you can just set it when you start your jvm. Your IDE may have options for it, too.
Most java problems are classpath problems.
Have you included those libraries in your classpath?
If you are using eclipse, you could
Project - > properties -> Java build path ->addJar.
And the Jar file should be placed in a directory inside your workspace (lib/ for example)
If you have to take your project to another computer, you could take these steps
Before doing anything, export your project (as a Jar file, for example).
Save it into your favorite drive (cd / usb drive/ diskette/ tape).
On "the other" computer, you can import this project into your workspace
In Eclipse, you need to add libraries to the project build path.
In general, you need to provide dependencies via the classpath mechanisms at compile time and runtime. The precise mechanisms vary, but, for example, if you used the javac compiler, you would provide your libraries on the command line:
javac -classpath C:\dir\lib1.jar;C:\dir\lib2.jar foo/MyClass.java
These dependencies would also be required to invoke the app:
java -classpath C:\dir\lib1.jar;C:\dir\lib2.jar;. foo.MyClass
This page gives some good info, though googling for the term "classpath" should provide alternative sources.
You use it by including it in the classpath of your java application, that way you can reference it from your code. Here is a starter document. The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). It is definitely a little complicated, but it is very worth knowing.
You should have documentation for these Jars. Some sounds like examples, but one must be the core graph modelling and rendering Jar. Hopefully the examples have source included.
Just add that Jar to your project in Eclipse (e.g., in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. You can also use Eclipse to look inside the Jar file.
Unless there is no alternative, it probably isn't worth using a load of third party code that isn't documented at least on the API level, and without any source examples definitely not.