adding path to java build - java

In my project I have an rxtx library that I use to manage Serial comms. When I run the application in netbeans ide I have no problems. I have put the rxtx files in my java folders and added the path in the project libraries.
The problems start when I build the jar file for the project. It creates the jar file and adds the folder for costum libraries at /lib folder. However I am not able to run the jar file by double clicking on it. When I run it from command line I get:
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
What should I do in order for the program be able to find the rxtx library in the /lib folder?

You must either:
Provide the classpath containing your /lib when you run java from the command line. Use the -cp option to do this.
Properly create an executable jar that bundles your /lib. I use maven and this plugin to create that: https://code.google.com/p/onejar-maven-plugin/

When you execute the program from the command line, you will need to specify the java.library.path to make sure that your native libraries are accessible by the JVM.
Without knowing how you actually run your program, I am going to take a guess at what the command should look something like:
java -cp yourjar.jar -Djava.library.path=/yourProjectPath/lib gnu.io.RXTXCommDriver

You can extract the required libraries into the jar file (when you don't want to use command line). Checkout this site to make this happen at netbeans.

Related

How to use Eclipse generated .classpath file to specify the external libraries?

I need to start a Java application using a .bat file . Here is what I have so far, and it works fine
java -cp ".;C:\someLibrary.jar;C:\someLibrary.jar;..." Main
The problem is there are too many external libraries, and Eclipse already generates a .classpath file referring to all those libraries. Is there any way I could use that Eclipse generated .classpath file in my batch, so that I don't have to list all the libraries in the java command above ?
Can I use something like this
java -cp ".;C:\ ..\pathToEclipseFolder.classpath" Main
The reason I am asking this is because I will eventually end up updating some of those external libraries. And I want to still be able to use the original .bat file
If your Java application needs the libraries, then you must specify them in the classpath of your .bat file.
A couple of considerations:
1) Maybe you have more libraries listed in your Eclipse classpath than you actually need
2) Java6 and higher allows you to specify a directory, instead of requiring you to specify each individual library in that directory:
java -classpath ".;c:\mylib\*" MyApp

Building Java Project can't access external jar referenced libraries?

My program runs exactly as it should when I run it out of eclipse, but when I try to build it into a runnable jar I get problems.
When I set Library Handling to Extract required libraries into generated JAR, or Copy required libraries into a sub-folder next to the generated JAR, the program runs but doesn't do anything that involves the external libraries. When I set it to Package required libraries into generated JAR, absolutely nothing happens when I try to run the JAR.
At this point I have no idea what to do after spending the past hour looking online for solutions, library handling seems to work fine for everyone else?
I'm using eclipse 4.4.1 if it makes a difference
First check to make sure you have the libraries. To do so open up the jar file with win rar or something similar. If the libraries are missing you can just copy and paste them into the jar file.
If they are there then you can create a batch file (If using windows) to run your jar file with the java command. Doing this will give you a console in command prompt to view output and stack-traces. This can help you find where the problem is.
Command for .bat file:
java -jar jarfile.jar
If you don't want to create a batch file and run it from command prompt, remember to make the cmd path the same as the jar file location.

JFreeChart Java JAR not running on a different machine

I have created an application using the JFreeChart library for use on another machine.
Previously I have developed applications with JFreeChart (using the same libraries) which has worked fine on other machines. The only difference is this machine is running Vista.
Please see below for the run-time exception I am getting:
The class that cannot be found, however, is located in the highlighted jar in the below image showing my imported libraries for the JAR. I have also established that this JAR is included in the manifest for the application. See below image:
So I very much need this to work and have no idea where to look next - or what is causing this problem!
Development machine Java version:
1.7.0_45
Target machine Java version:
1.7.0_45
Thanks in advance.
Check the Class-Path attribute in your JAR's manifest, which should contain entries like this:
Class-Path: lib/jfreechart-1.0.17.jar lib/jcommon-1.0.21.jar …
Also, examine dist/README.TXT in your NetBeans project folder, which should say something like this regarding libraries required by your project:
To run the project from the command line, go to the dist folder and
type the following:
java -jar "CISOnlineMonitor.jar"
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
This has nothing to do with os i belive.You dont have all the necessery libs within your jar.Try to open a jar and see if you have them in.Fact that you are able to run it on your maschine only proves that.Make executable jar with eclipse or whathever you use.And when it ask you for libs check -Extract required libraries into generated Jar.
if you dont know how to get to that point
File>Export>Java>Runnable Jar File> Runnable JAR File Specification.
Also right click on your project and check Your build path.
RightClick project>Properties>Java Build Path>Libraries
Make sure you have everything correct
EDIT-
As i see you use NetBeans im not sure exactly how to find all this there.Bud it will be very similiar.

How to run a custom jar file copy to classpath command using java?

I have a jar file which I'm using to run as an application. However, every time I run my application I need to add -cp "jar name" to compile it and then run the java application.
How can I make a permanent change so that I don't have to use the command line commands for this task? Can I put that in certain lib location?
Note: I don't have to use any IDE. It has to be from a terminal.
If you are on Linux/ Mac, add the following to your ~/.bash_profile
export CLASSPATH=$CLASSPATH:/path/to/jar
If you are on windows, use this..
set CLASSPATH=%CLASSPATH%;\path\to\jar
You can read more about it here and here
Add your custom JAR in the CLASSPATH. Steps to setup and verify are available here: JAVA PATH & CLASSPATH
you may copy your lib or additional jars to JAVA_HOME/lib/ext
or any directory that is in system variable CLASSPATH
One more solution: extension libraries, put your jar into $JAVA_HOME/lib/ext
One needs to be careful - every java application will pick up this jar

Exporting to Runnable jar with extra native code libraries in eclipse

I am having trouble exporting my java project from eclipse as a jar executable file. My java project uses an external library (its called jri). I have exported the jri.jar file and set the library path for its native library in eclipse, and it works great in development in eclipse. However, when I export it as an executable jar file I get the following error:
Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.
I have placed a folder called lib in the same directory as my project's jar; this lib folder contains jri's native library. jri's native library is not in one file but in a folder. This is the same setup I have in eclipse.
The way I am exporting my project in eclipse is
Export...
Java > Runnable JAR file
Copy required libraries into a sub folder next to the generated Jar
Finish
And my folder is organized like this
folder project
project.jar
project_lib
jri.jar
jri native library folder
The MANIFEST.MF of my project.jar is:
Manifest-Version: 1.0
Class-Path: . project_lib/jri.jar
Main-Class: index
What I want to achieve is to give another person a folder including project.jar and anything else needed so she/he can run it without needing to install anything else.
Thanks so much
Add a script containing something like that:
#!/bin/bash
java -Djava.library.path=project_lib/native/ -jar project_lib/jri.jar
I export some java projects that way.
This is relatively hard to implement. The solutions I have seen involve extracting the native libraries in the JAR to an OS temp directory and then loading it. I would go for an integrated solution for that. One Jar and Java Class Loader support it, and on the second page you will find links to similar tools.
You can put the libraries inside your jar:
Export...
Java > Runnable JAR file
Package required libraries into generated Jar
Finish
I always export this way.
I don't know if it will work in your case, but worth a try.
Edit:
See these links:
Instalation, setup and setting environment properly
What to do when getting Cannot find JRI native library!
My guess is that this have something to do with LD_LIBRARY_PATH not correctly been set. Or the file wich it is searching for isn't in the path listed.
You know I had the similar problems
Could not extract native JNI library.
all above proposes can't help me. I couldn't stop and start gradle deamon by using follow command:
gradle --stop
I saw that gradle deamon still not stopped in my processes. That's why I kill it in my process and all will be fine :)

Categories