I am trying to build an installer for a couple of Java Maven projects (using Eclipse). I would like to use maven assembly to produce a jar file (henceforth big-fat jar) which contains all the installation files (JARs + docs + others) as well as the installer JAR (the result of building the installer project). I want the installer to unpack the big-fat jar and therefore I want the main class to be included in the installer jar. The user will get the big-fat jar and I want java -jar big-fat.jar to execute the main inside installer.jar. To recap, here is the structure of the big-fat jar:
big-fat-jar:
--installer.jar
--application.jar
--readme.doc,
--META-INF:
----MANIFEST.MF
where installer.jar is the Java program which unpacks big-fat-jar and contains main, application.jar is the application being installed (there may be multiple jars or wars instead of just one application.jar) and readme.doc stands for any docs. My question is: what do I do to make the main inside installer.jar be called when the user executes java -jar big-fat-jar? I read on other posts that OneJar may be helpful but I am not sure how to indicate that main resides inside the inner jar.
I have never tried this with a jar inside another jar as you have described but I think it could work if you specify this in your Manifest.txt file. You could do this like so:
Manifest-Version: 1.0
Created-By: StackOverflow
Main-Class: installer.MyClass
In the example above the MyClass file will have the main method that will be called when running java -jar <jarname.jar>
Please see the top of this page for more information: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Head First Java by Sierra and Bates also goes over how to do this with a class but not a Java jar inside another Java jar.
I hope this helps.
Thank-you for reading this.
Related
I'm currently a student at a high school, and I found a game programming competition for a club called FBLA. I had a few questions about the competition, and so I emailed them.
I made a post earlier on Computer Science Stack Exchange, where you can view here.
Now after receiving feedback from the question, someone commented that I should post my next question on this website.
My question is that how would I be able to package a Java JAR file with the native libraries, and then create an EXE out of it, so it will be a stand alone?
In my theory, I would have to create the jar, and put the required native files when a person installs java on their machine, into that JAR file, and then I can use a program such as FatJar or JSmooth.
If I'm wrong, or how I could understand a bit more on how jars are ran, it would help considerably.
Update Nov. 8, 2014
I finally got back around to messing with it, and I downloaded Launch4j. I set up and did everything with it, and I make an executable, which contains all of java's core native files. I uninstalled all of my programs dealing with java, and when I went to run it, it still required java.
Once again to state what I need to happen:
What needs to happen is the executable will be able to run even if there's not any version of Java installed on the judge's computer.
I would reccomend you use launch4j. http://launch4j.sourceforge.net
I've used it for some larger projects and it worked very well.
It can encapsulate your jar, dependencies and even a JRE into a windows in executable. It has a GUI or you can run from the command line.
Checkout Packr, created by libGDX team: https://github.com/libgdx/packr
To make your jar executable, you specify the main class of your application, any extra jar files it may require and so on in the jar's manifest file:
Main-Class: MyAppMain
Class-Path: MyLib.jar
Then you use the jar utility from the Java SDK to package your classes and resource files, specifying the m option and the name of your manifest file:
jar cvfm MyApp.jar MyApp.mf *.class *.gif
This will result in the creation of MyApp.jar. Now, if you type
java -jar MyApp.jar
the Java launcher will read the manifest from MyApp.jar, place that file and MyLib.jar on classpath and invoke the main method from the class MyAppMain.
Now, if your Java app needs native libraries, you also need to specify the java.library.path property pointing to the directory(ies) holding those:
java -Djava.library.path=.\bin -jar MyApp.jar
For more info and alternative solutions, refer to my article.
I have written a java application in eclipse and then I have made its runnable JAR file, but the problem is that the application is not opening when I click its JAR file to open it, but the application is opening and running perfectly when I open it in the eclipse, I have made the JAR file number of times again and again but I am facing the same issue every time, no error is shown when I click the JAR but it is not opening.
So please tell what can be the problem with the JAR file.
Any number of things can be wrong with a jar file that prevents you from being able to run it after you create it. Some things to check:
Do you have java associated with your jar file?
Did you specify a main class when you built your manifest?
Are there dependent library jars your application depends on that did not get added to the classpath? The manifest file in your jar file should tell you any additional classpath entries that were generated.
Do you get an exception when you run it from command line?
You can run it two ways:
java -cp [path to your jar] your.package.MainClass
java -jar [path to your jar]
See if any of these are your issue and it should point you to the solution to your problem.
When you generate a JAR file, it doesn't mean something is going to happen, maybe it is just a library you generated, a reusable module of the system, etc.
"JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform." - Wikipedia
So, Make sure you have a main class in your manifest, for example:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 16.3-b01 (Sun Microsystems Inc.)
Main-Class: com.example.MainClass
Class-Path: lib/lib1.jar lib/lib2.jar
Jar available at http://dl.dropbox.com/u/10919879/roller.jar
After writing and testing my program with Eclipse's built-in run Utilities, I decided it was time to export and distribute. So I go to File>Export>Runnable JAR and designate the class whose main method I want to invoke. Everything seems to be going off without a hitch, until I try to actually run the JAR.
Error: Could not find or load main class C:\Users\Matt\Documents\My Dropbox\Publ
ic\roller.jar
Contents of roller.jar
dice/Roller$RollData.class
dice/Roller.class
META-INF/MANIFEST.MF
contents of META-INF\MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: .
Main-Class: dice.Roller
What's going on, and how do I fix it?
It's not a problem with your jar. It's a problem with OS setting that launches jar files incorrectly. I don't know if currently on Windows after installing Java it will be correctly configured automatically and why it isn't on your system. It may require editing registry to set jar files to be launched with java -jar command.
You can use application that converts JARs to EXEs if you're going to distribute it only to users with Windows.
I have developed an application in eclipse that requires java3d. I have the jars for java3d, but I'm not sure how to put them into my final jar such that my application can reference the required classes. I'm on a mac and just making the jars with the command line, this is my script:
cd /Users/landonsilla/Sites/3dviewer/bin
jar -cvf CyarkCloudViewerPro.jar pointCloudTest/*.class pointCloudTest/icons/*.png
It's pretty simple, it just takes some class files and some images and puts them in the jar. How do I put the java3d jars in there?
The end goal is to deliver this on my website via jnlp. My application works, and the jnlp deliver mechanism I implemented works fine. However, my app crashes when trying to do java3d stuff.
This seems like a simple and common request, I just can't figure out the answer.
You just add the jar files into the main jar, and then announce the libraries on your MANIFEST.MF file. Example of the MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: fi.ropecon.contv.client.ConTVClient
Created-By: "Jari Juslin <zds#iki.fi>"
Class-Path: lib/j2ee.jar lib/jbossall-client.jar lib/log4j.jar lib/jnp-client.jar
"Main-Class" is the class that's called to start the program when you start program with java -jar command. Class-path is, well, normal Java classpath, but relative to the root of the main jar archive.
Probably a very usual problem with exec jar. I am trying to create and run an executable jar and its driving me crazy.
I have couple of classes(with package stmts), one of which has a main method, a simple empty constructor and ofcourse few biz methods, these form part of a small eclipse project.
I am bundling both these and a manifest file into a jar(arc.jar) using a build.xml. My program uses jdom library and also references from one of my other eclipse projects, so I am including both the jdom libray and other biz library while building my arc.jar.
This is the Manifest.MF I wrote, there is a new lineafter Main-Class
Manifest-Version: 1.2
Class-Path: jdom.jar other.jar
Main-class: uk.co.Art
When I unzip this arc.jar, it contains jdom.jar, other.jar, META-INF/Manifest.mf and the package with my classes.
Funnily, the Manifest file in jar looks like this -
Manifest-Version: 1.2
Created-By: 14.2-b01 (Sun Microsystems Inc.)
Main-class: uk.co.Art
Class-Path: jdom.jar other.jar
I got the ClassNotFoundException ClassNotFoundException: uk.co.Art when the value of Main class was without the package. With package the error changed to ClassNotFoundException: org.jdom.JDOMException.
So just for testing purpose, I tried giving the fullpaths with c:\ for both my executing class and the jars but still it is not able to locate the main class, errors out ClassNotFoundException: uk.co.Art.
I cant seem to understand why.
The command line I am using is - java -jar Arc.jar
The version on cmd prompt is -
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode)
while the eclipse installed jre is jre6
What can you folks suggest ?
Alex' solution did solve the problem for local execution.
I am also thinking of another way(as an extra option) of execution, to deploy the Arc.jar on server and run a batch on that machine which basically sets the java classpath and then calls either the jar or directly the class whichever way it works. But its only an option, not a preferred one though.
Or, also, set that classpath in my manifest and build jar locally and deploy on the server.
Would like to try out a few things though. But now I understand that the basic problem was jars into jar and hence classpath issue.
Thanks a lot folks.
You cannot bundle other JAR's in a JAR. They have to go in the same folder or at least in a folder relative to the Arc.jar. You specify then its relative path in the Class-Path entry.
Eclipse however has an useful export tool which allows you to embed the contents of other JAR's in your JAR so that you don't end up with multiple loose JAR's which are required to be placed in a specific location. Check the 2nd Library Handling option when you choose Export > Runnable JAR file.
When choosing Package required libraries into generated JAR, then Eclipse will add a special classloader which will load those JAR's transparently for you before executing the main class. It's doing that with help of JarRsrcLoader.
You do not have to pack your additional jars into your jar. These jars should be in the same directory as your application jar. By other words: put all jars into one directory and run. Everything will work.
Reasons: think what does executable jar mean. If you jar is not "executable" you run it as following:
java -cp art.jar;ldom.jar;other.jar uk.co.Art
when your jar is executable you use the following command line:
java -jar art.jar
No magic: jvm opens manifest and takes name of main class and other jars from there and virtually creates command line like first one. Now you understand that all jars in your case must be in the same directory.
Maybe the .jar is corrupt, like sometimes happened to me. Take a look at this.
Just couple of things to be noted when exporting.
JRE associated to the project is consistent with the JRE that would be used to run it.
Please export your project into a local drive, then move it into any shared drive (this fixed my issue).
Thank you.