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

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.

Related

Completing My Project

I have finished writing a project in the IntelliJ IDEA using Java. I can not figure out how to get the project from a saved file in the IDEA to an executable on my computer (I am on Windows 10 if that matters). Then, I want to be able to put the executable on my Girlfriend's computer (mac) because it's a gift for our anniversary. She's not very tech-savvy (apparently neither am I...) so I want to be able to just drag and drop something onto her desktop and have her be able to double click it and have it work without having to install anything to her computer. (I was looking at Jar files, but something I read said that mac's do not run Jar files unless I install some things).
So what I'm really asking is:
1)Using IntelliJ, how do I finalize my project (make a JAR file)? I have been unable to find directions for this that have actually worked.
2)What file do I need to make a batch file call to get it to run my program once it is a JAR? Does it just call the JAR as a whole, or a specific .class? (Someone suggested a bash file, but I have not heard of this before, so I am open to other suggestions besides batch files. I am currently looking into what bash files are)
3)Can I make the batch file call the file indicated in question 2 via relative path, thus allowing me to keep it in the JAR or in a folder with the JAR and making a shortcut to it on a desktop (for example)?
Building a JAR in IntelliJ
Option 1 - Long way round, getting IntelliJ to build your JAR
Hit Ctrl+Shift+Alt+S to open your Project Structure settings menu.
Go to Artifacts
Click the green +
Select JAR > Empty.
Name your JAR, for example Gift
Right click on all the files in the Available Elements that you want to put into your JAR (often everything) and select Put into Output Root
Click Apply. Click OK.
Go to Build > Build Artifacts... > Build.
You'll find your JAR in out/artifacts/{JAR_NAME}/{JAR_NAME}.jar
Option 2 - Short way (Maven only)
If you're using a dependency manager like Maven and you've adhered to the Maven project structure, just use the command:
> mvn clean package
and you'll find your JAR in your target/ directory.
In either option, you may have to define your .class file that contains your main() method.
Running a JAR
The normal way to run a JAR file would be to go into cmd (Windows) or a terminal (Mac & Linux) and use the command:
> java -jar {JAR_NAME}.jar {args}
where {JAR_NAME} is the file name of your JAR and {args} are any arguments that need to be defined up front.
If you want to not use a command line you could write a script that runs the JAR for you. Then you can double-click/run the script and it runs the JAR for you.
For Windows
I'd recommend writing a simple .bat or "batch" script that will run the file for you.
Keep the batch script in the same directory as your JAR file and just have it contain:
java -jar ./{JAR_NAME}.jar {args}
You could use a Powershell script or something similar though.
For Mac/Linux
As Mac (and Linux) are Unix systems we can use a .sh or "bash" script to run the JAR. Again keep the bash script in the same directory as your JAR file and use:
#!/bin/bash
java -jar ./{JAR_NAME}.jar {args}
.bat script is exclusive to Windows systems, .sh scripts are exclusive to Unix systems.
Because Windows and Unix are two very different systems, you're not going to find an executable file that works for all systems.
Converting your JAR into an executable
You could convert your JAR into a standard Mac "app" application bundle by following some instructions here.
However, I wouldn't recommend this as it isn't really necessary when you have a simple script/command option and for this method, you'd most likely need access to a MacOS machine with development tools.

adding path to java build

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.

Exporting eclipse project to jar

I did check the already answeared questions which have almost the same topic as this question but none of the answeared onces were able to solve my problem.
I have been searching now online for about 4 hours and tried soo many different things to solve my problem..
Im trying to make a Pong game in Java and now i wanted to export my project so i can send it to some friend so he can try it.
Im using LWJGL for this project so i do have some jars added to the referenced libraries, And the LWJGL.jar has the windows natives added which is included in the projects lib folder.
Here is an image of the project viewer:
Reason im posting this image is so you get an idea of that i have all the libraries inside the project aswell as the natives needed for the project to run.
While reading about why the jar didnt work i also read that you need a Manifest.mf file so i created one and inside this manifest file i have the following text:
Manifest-Version: 1.0
Main-Class: jGame.Main
Class-Path: lib/jars/asm-debug-all.jar lib/jars/jinput.jar lib/jars/lwjgl.jar lib/jars/izma.jar lib/jars/slick.jar
Also 2 empty lines underneath Class-Path since i heard u have to have that.
This project runs fine aslong as i run it in eclipse but when i export the project with either runnable jar or jar wizard in eclipse it doesn't start when i double click the .jar file so i went into the cmd and used this command:
java.exe -jar JGame.jar
And the following message was shown:
I have checked atleast 10 times that i provided the correct path for the main class.. And i dont know what to do anymore. In the eclipse wizard i have been trying both to export runnable jar and exporting normal jar but none of the two is working. Also in the wizards i have selected to export a normal jar not runnable since when i choose runnable the libraries get messed up because the lib folder gets replaced.
I tried opening the jar file as an archive and got the following:
In an attempt of using JarSplice i suceeded to solve the problem.
After reading this:
It's not going to work the way you're trying to currently do it, since you need to have the native files along side the jar and point
to them via the '-Djava.library.path' parameter.
If you just want a single jar and want to avoid the hassle of the
command line and native files use the JarSplice tool. JarSplice is
easy to use and will automatically handle the native file stuff for
you.
1) Simply export your project (class and resources) to a jar (easier
just to do it through your IDE).
2) Then run JarSplice, add all the jars you need to the jars tab (your
app jar, lwjgl.jar, and any other external jar you're using).
3)Then on the natives tab add all the natives files (windows *.dll,
linux *.so, mac *.dylib & *.jnilib).
4)On the class tab add your main class. Then create your jar.
You can then run this jar just by double clicking it (or if you wish
via command line using 'java -jar yourapp.jar').
I found this solution from this link: Can't start .jar file (using LWJGL)
Reason why it didnt work the first time were i didn't include the actual .jar file that was exported from eclipse while using JarSplice.

run java console program in unix

I have some code in Java using Eclipse and I would like to deploy it to unix envirnment. The program is simple console program that just takes some arguments at the run time, read a file and print out some results.
My question that what is best approach to deploy and run it in unix envirnment. I was just thinking to copy all the classes file to the unix envirnment and create a batch file to run the main class file. Does this sound okay? Or, should I create a runnable jar file?
Also, where should i put the jar files that the prgram is referencing (in classpath)?
Thanks
I think an executable jar file will solve your purpose here.
You should be able to execute it as
java -jar <jarfilename> <arguement1> <arguement2> .... <arguementN>
You can execute the jar file from the current directory itself, just make sure your jar file has executable permissions.
chmod +x <jarfilename>
ls -la
Designing for easy deployment is important in my opinion.
In our case, there are some components:
store project in the source code management system (git). we break down source code as
the developing source code to dev branch
the stable source code to release branch
use build tool, such as ant or maven, and provide a deploy script in the project. (we will talk deploy script in 3.).
provide deploy script to:
fetch the latest stable source code in the build server
build to executable files in the build server (whatever you do)
send the package to the target server
launcher (close the old app and run the new app) in the target server (via remote ssh command)
Currently, you think how to package the java, but it is a simple thing just about building and runing. When you talk about deployment, make it as easy as possible. Each time we deploy just to invoke the release script.
PS. I don't like the executable jar. Using un-packaging jars and compiled class can be sending by rsync very efficiently.
It sounds Ok and will work for you. Just one fix: you are going to write shell script for unix, not batch file.
But you can do better. Typically java classes are packaged into jar file. Jar file is just a zip file with optional META-INF, directory, MANIFEST.MF and other stuff. So it is better to package your application into jar and then run it as: java -cp yourjar.jar YourMainClass.
To create jar file you can use any tool that can create zip or utility jar that is a part of your JDK. You can also create automatic build using ant, maven, gradle, bildr etc that will help you to package your application.
I would do the following:
create a dedicated directory for this program. Copy the dependent .jar files to that directory
write a (short) script that sets the classpath to point to these jars and then executes the main class
Given the above, it's largely a matter of style as to whether you create a runnable .jar or not. It'll be hidden from the user.
I'm suggesting a script because:
you can set regularly used JVM parameters easily (memory options etc.)
it's a pain (and hardly intuitive) to type java -jar {pathtojar} etc.
By copying the jars to a dedicated directory, you can then use different versions of jars for different scripts (e.g. you may have 2 programs that use two different versions of commons-lang)
You should also (probably) use this script to explicitly determine which version of Java you use to run the program with. As you install/upgrade you don't want to break your programs and the scripts can be configured to explicitly tie down this info.
I find this quickest of all:
First, create a jar, copy to unix server and change file permission just as dopplesoldner mentioned below.
You can put your library classes and or jar dependencies in a lib folder
Then execute the jar
java -Djava.ext.dirs=lib/ -classpath yourJar.jar com.yourPackage.yourClass
yourClass will be the class having main(String args[]) method you wanted to execute.

Distributing a Java console program

I have created a Java console application using Netbeans. In the Netbeans dist directory I have the class file of the project. Now I need to give the executable files to someone else
who will run them on another PC.
Which file I should send? How can he run them on his PC? Is there any way to create an exe type file?
Both PCs have the JDK installed.
Build a jar file with a main class specified in it.
If he has Java installed and .jar is associated with that, he should be able to just double-click on it.
Alternatively from a command line he'd be able to run:
java -jar program.jar
There are programs around to create executable wrappers around this, but a jar file is a simpler solution in terms of packaging - it's worth trying that to start with.
In additon to Jons answer:
If you have a runnable jar to start with, it is frequently much easier to package it up in an EXE file. If you have the need search Stackoverflow for JSmooth and one-jar.
NetBeans actually answers your question. If I do a Clean/Build, the output says:
To run this application from the command line without Ant, try:
java -jar "insert_your_project_name_here.jar"
theres a handy page about this here:
java tools tutorial
It describes creating jar files a little further down the page
You can go to run menu and select clean and build project or shift+f11 after this go to dist folder in project folder and use .jar file and run that by hint say in over .

Categories