Nested jar files - java

a question on building executable jar files. Suppose we have a jar file a.jar that contains UI.class. I have used a metafile in archiving a.jar that says
Main-Class: UI
Class-Path: . b.jar c.jar
Now, if I want to ship this executable a.jar to somebody, I have to ship 3 files - a.jar, b.jar and c.jar. The user has to put all 3 in the same folder, and then double clicking on a.jar would work. It would be simpler if I could ship a single file (a.jar), and the JVM could figure out to extract b.jar and c.jar from in there to use the classes. These 2 are external libraries and I dont want to expand them and re-jar the class files in a.jar.
Can this be done? If so, what is the jar command and what should go in the metafile? Currently the command is
jar cmf metafile a.jar UI.class
Thank you.

Have you looked at this tool OneJar? Also this is a good article to read on OneJar.
If you decide to not use an external tool then your alternative is to put the library/helper jars in the classpath in the manifest, and then copy the jars themselves in a directory path relative to your main jar.
EDIT: OP asked for an example MANIFEST.MF. I coped this from the example One-Jar example jar file.
Manifest-Version: 1.0
Main-Class: com.simontuffs.onejar.Boot
One-Jar-Expand: expand,doc

There is OneJar as people have mentioned but also (especially if you are using eclipse) you can consider fatjar.
ALternatively, you can also achieve this by using an ANT Task here's an example:
<target name="dist" depends="compile,static" description="Compiles and builds jar files">
<mkdir dir="${dist}"/>
<jar destfile="${dist}/MYAPP.jar">
<zipfileset src="${dist}/MY_OTHER_APP.jar"/>
<zipfileset src="${lib}/commons-io-1.4/commons-io-1.4.jar"/>
<zipfileset src="${lib}/commons-math-2.1/commons-math-2.1.jar"/>
<fileset dir="${res}" includes="*"/>
<manifest>
<attribute name="Main-Class" value=<where your main class is>/>
</manifest>
</jar>
</target>

OneJar is good for just bundling a few things together, but it's generally quite slow and inflexible. This isn't a dig at OneJar, it's great for what it does but unless I wanted a quick and easy distributable for a tiny program I wouldn't use it.
I did use it for a while but then switched to izpack. It provides an installer as a single jar which I've found to generally work very well, and supports things such as windows specific shortcuts as well. It's also really easy to integrate it with ant.

Another way can be use one of several available tools to create a setup deliverable and your setup pack will unpack your application when installed.

Maven users can also use the assembly plugin for this:
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

Related

Ant jar task - Include files not underneath basedir

I am trying to write an ant target that will create a jar based on a bunch of java files and some properties files, but am having trouble including the properties files the way the project is set up currently.
These are the ant targets I have:
1 - Compile the java souce files from the "myjar.src" folder and put the resulting classes into a "myjar.classes" folder. Once this is done copy all non .java files from "myjar.src" to "myjar.classes".
2 - Create the jar using the "jar" command using basedir = ${myjar.classes} and tell it to include everything.
These are the ant targets I want:
1 - Compile the java souce files and put the resulting classes into a "myjar.classes" folder. Only .java files are included
2 - Create the jars using the "jar" command using basedir = ${myjar.classes.location} but also include the .properties and .xml files from "myjar.src."
The key difference is I want the properties and xml files from "myjar.src" to be included when I package up the classes in "myjar.classes" using basedir = ${myjar.classes} - How do I include these fies when they are not underneath "myjar.classes"?
This is the ant target I want to modify:
<CreateManifest title="myjar classes etc"/>
<jar
destfile="${myProject.build.jars.dir}\ta_test_driver.jar"
basedir="${myjar.classes}"
manifest="${manifest}">
<include name="**"/>
</jar>
The reason I am not just using a directory one level up for basedir is that none of the other jar creation calls in the project do that, and I am hesitant to change that for just this one. I did try to do that, but had trouble specifying the right directory. (There are other jars that use a similar directory structure, and I don't want to interfere with them either now or in the future.) I was just wondering if these is a better way to do this? Thanks very much.
I managed to figure this out from here: How to include file in Jar through Ant at specific location
This was my modified ant call in the end:
<CreateManifest title="myjar classes etc"/>
<jar
destfile="${myProject.build.jars.dir}\ta_test_driver.jar"
basedir="${myProject.classes}\ta_test_driver"
manifest="${manifest}">
<include name="**"/>
<zipfileset dir="${myjar.src}"
includes="**/*.xml, **/*.properties, **/*.gif"
/>
</jar>
Notice the zipfileset tag.

Launch4J - how to attach dependent jars to generated exe

I have a simple java project, which requires external jars.
I build this with netbeans and after Clean and Build command, I can find in dist directory the following structure:
-myApp.jar
-lib/
library1.jar
library2.jar
typical, I would say.
Now, I'd like to distribute myApp.jar with dependent libraries as one exe.
Is this possible? I am trying to use Launch4J. In the GUI I create the config file, there are some options in cp section
<cp>lib/swing-layout-1.0.4.jar</cp>
but it seems to be classpath, and it is the only place I can refer to my extra jars.
After exe file is created, I can't find dependend libs in the exe (exe can be opened with winrar) and thus my application crashes.
How can I make the exe file properly then?
Thanks for your help.
As it often happens being unable to solve the problem I published it on StackOverflow ... and pretty soon after publishing the question I got an idea.
So the answer to my question is:
Put all the dependent jars into one main jar.
It took me some time to find info how can I do that.
To help people I decided to publish detailed instruction here - they are based on Netbeans 7.4.
Following article from http://mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/ I created the ant script that build one-jar-app for me.
I could then manually create exe via Launch4J
I then decided that I want more automated task, and I did that, Ant builds exe for me (via Launch4J)
Then I realized that I must do "clean and build" before my automated task (in point 2)/ I decided that I want clean and build to be done automatically before the exe build
Putting all together I am attaching my ant build script consisting of points 1,2,3:
It is required to edit build.xml and put the content found below before "project" end tag
<target name="package-for-launch4j" depends="clean,compile,jar">
<property name="launch4jexe.dir" location="C:\Program Files (x86)\Launch4j" />
<taskdef name="launch4j"
classname="net.sf.launch4j.ant.Launch4jTask"
classpath="${launch4jexe.dir}/launch4j.jar
:${launch4jexe.dir}/lib/xstream.jar" />
<property name="launch4j.jar.name" value="MyAppJarName"/>
<property name="launch4j.dir" value="exe"/>
<property name="launch4j.jar" value="${launch4j.dir}/${launch4j.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${launch4j.jar}"/>
<delete dir="${launch4j.dir}"/>
<mkdir dir="${launch4j.dir}"/>
<jar destfile="${launch4j.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<zip destfile="${launch4j.jar}">
<zipfileset src="${launch4j.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${launch4j.dir}/temp_final.jar"/>
<launch4j configFile="misc/l4j-myapp.xml" />
</target>
then in Netbeans rightclick on the build.xml and choose:
Run Target / Other Targets / package-for-launch4j
exe file is ready in exe folder :-)
When you are converting your .jar file
Go to classpath tab
Check custom classpath
On main class select your .jar from your dist folder after building the project
On the classpath textarea add your libraries, you add them right below that
textarea writting the full path to the lib (of course including the
lib, ie "C:\folder\lib\file.jar")
I have spent hours on this issue. So here is my contribution.
The problem here: how to sucessfully put your external jars that your .JAR program needs INSIDE the .exe that you generate.
We assume that you already, and correctly, configured the external jars on eclipse/netbeans and ALL WORK FINE with the command: java -jar yourprogram.jar.
So the real problem is how to ensure that this .EXE file will contain the external jars, otherwise it will not work properly.
1) First of all, you need to forget Launch4J and anyother program.
2) Install JSmooth, I recommend that you use the windows version.
3) On the left menu there is a button "Application". Click on it.
4) You will see a tab "Classpath" panel. Click on the plus (+) and add your external .jar's files. And that is it!!
Don't forget to put your .jar application marking checkbox "use am embedded jar" and choose the main class properly. It will work.
I also faced the same issue while migrating my .jar to exe. I also had many dependent libraries as well. So These were the steps I performed :
Download and Install launch4j.
Open your project in netbeans. Clean and build the project.
Make sure you have a folder named 'dist' in the project directory. It will have your jar files with lib folder(containing the dependent libraries).
Open launch 4j.
Create output file in the dist folder. For example : OutputFile : D:******\My_App\dist\my_application.exe
Browse your jar file in the next row. For example : Jar : D:******\My_App\dist\my_application.jar
Go to classpath tab. Tick CustomClasspath. Press browse icon, and browse to your jar file which is located in the dist folder.
Specify Min Jre version in the JRE tab.
Save the configration.
Build the wrapper(by clicking the settings icon)
Your exe file will be generated in the dist folder.
Thats it :)
Putting different links of places which had helped me
How to include all dependent Jars within a single non-executable jar?
How can I create an executable JAR with dependencies using Maven?
http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
and most importantly
http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
quick tut
http://www.mkyong.com/java/how-to-add-your-manifest-into-a-jar-file/
To include external libraries with Launch4j you have to have the ".jar" files of the external libraries near your .exe (mine are just in the same folder) then in "Classpath" you put in the path to the .jar files into "Edit Item: "
In launch4j go to the classpath tab. Tick the custom classpath field. In the edit field, enter the full path of each jar you want included and press accept. When finished type just the name of the main class into the separate field (ie MyProg). All the jars will now be included in the exe.
PS I have all the jars in the same directory. I'm using version 3.12

Setting Class-Path in Ant build script

I'm trying to build my console application and I'm using Ant to build it. I can run my application in Eclipse, but when I try to run it from jar that I get - the ClassNotFoundException: is thrown. is in one of jars, that I use for my application. Here is a part of build.xml where I create manifest:
<manifest>
<attribute name="Main-Class" value="com.package.Reporter" />
<attribute name="Class-Path" value="lib/commons-httpclient-3.1.jar
lib/commons-logging-api.jar
...lot of jars...
lib/stax-api-1.0.1.jar" />
</manifest>
The required class is in commons-httpclient-3.1.jar
And here is how I set up classpath for compiling, that is fine:
<path id="libs.dir">
<fileset dir="lib" includes="**/*.jar"/>
</path>
UPD: Should I put jars with libs to my jar? Now I'm putting them to "lib" directory of my jar. So myjar.jar contains package with my classes, META-INF directory and lib directory.
Max, you can't insert jar libs into jar, assuming normal usage. Either you don't have to specify them manually at runtime as Romski suggested. When invoking java -jar myjar.jar it should locate all your jars provided that they are located in the lib directory. lib directory must be in the same directory that jar resides in. Doesn't matter if you call java executable directly or through ant java task.
Note that the current directory doesn't matter only the relation between the jar and the lib.
Now being overly explicit. Perform sanity test as follows: create a new tmp directory and copy files to it:
tmp/myjar.jar
tmp/lib/commons-httpclient-3.1.jar
Run java -jar tmp/myjar.jar
Edit: now I see I just wrote the same what is in Oracle jar tutorial. But I also made tests myself with a relative directory. I also see dozens of stackoverflow questions searching for jar in jar so please first search SO, then ask.
try to change the path like this.
<path id="libs.dir">
<fileset dir="./lib" includes="**/*.jar"/>
</path>
You need the manifestclasspath task.
Example:
Ant - how to get all files' name in a specific folder

Java: How to compile a runnable jar from packages?

My Java application has got a package structure similar to this:
src/com/name/app
src/com/name/app/do
src/com/name/utils/db
How would I go about compiling Java files in these directories in to a runnable jar? I need to package required libraries into the generated JAR (jdbc).
I've always done these things in Eclipse but now I need to supply a couple of people with a way to compile the repository without the use of eclipse and I was thinking of making a makefile or a script that invokes the necessary javac pattern.
Take a look at Ant. It's a relatively simple build tool to understand, and provides everything that meets your requirements. Here's a quick skeleton build.xml to get you started:
<project name="my_app_name" default="jar">
<target name="compile">
<javac srcdir="src" destdir="bin">
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="jar">
<jar manifest="manifest_file" destfile="dist/my_app_name.jar">
<fileset dir="bin" />
<fileset dir="lib" />
</jar>
</target>
You need to create a manifest file that will tell the java process which class holds the "main" method. Here is a good place to start learning about manifests.
As an alternate that produces really cluttered Ant build files, you can right click on your Eclipse project and choose "Export...", then choose "General > Ant Buildfiles".
Anyway, that should get you started. You can ask more specific questions as you run into them.
First of all, consider using Ant for such a task.
But since you asked for a manual process, you need to first create a manifest file, like so:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Class-Path: lib/jdbc.jar lib/otherlib.jar
Main-Class: com.name.app.MainClass
Replace the contents of Class-Path with your libs, and Main-Class with the fully qualified name of your main class.
Then, you need to generate the actual .jar, using the following command:
jar cfm app.jar MANIFEST.MF src/com/name/app/*.class src/com/name/app/do/*.class
Where MANIFEST.MF is the previously mentioned manifest file, and the rest is the folders where your .java classes lie in.
Finally, to run your app, you simply execute: java -jar app.jar.
Consider using Ant to do this. http://ant.apache.org/
I recommend that you use Apache Ant to implement your build scripts.
If implemented correctly, Ant is easy to use and the build scripts can be run on any platform that you can install a JDK on. Indeed, with a little bit of work, you can even set up your project so that users don't even need to download / install Ant. (Hint: add the Ant JAR files and a wrapper script to your project distro)

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, especially as the order of dependencies may be important and needs to be preserved from the order in the ivy config.
Has anyone done this before? I also need to generate relative paths in the classpath so I can't use absolute paths as this will only work for the machine on which the build is done.
EDIT: Based on feedback if we cut Ivy out the equation (do the resolve to a directory of my choice) I can then probably resolve the list of libs ok. But how would I generate a classpath suitable for a start script, especially with the relative paths (relative to my bin directory)?
e.g.
install
/bin <-- scripts here
/lib <-- jars here
So in my bin/start.sh I need to have ../lib/ in front of every jar reference rather than a full absolute path.
Thanks.
Since many years (2000?), we had this small script in path ("make_cp")
#!/usr/bin/perl
my $CLASSPATH="";
my $DIR=shift;
$DIR||="lib";
opendir(LIBDIR, $DIR);
while ($file = readdir(LIBDIR)) {
$CLASSPATH.=":$DIR/$file" if ($file =~ /\.jar$|\.zip$/);
}
closedir(LIBDIR);
$CLASSPATH=~ s/^://g;
print "$CLASSPATH";
Used like this:
export CLASSPATH=`make_cp lib`:`make_cp external-lib`
Since Ivy evicts overlapping dependencies and tries to find the best common dependency for all the projects I don't really understand how the order of dependencies would matter at all.
However you should make a standard JAR/WAR/other with Ant for your project and include Ivy dependencies inside that JAR. Basically all you should need to do is to make Ivy's Ant task to resolve the dependencies to a folder, then build tha classes using those dependencies and then consruct the JAR so that you include the library JAR:s to newly created JAR's /lib/ folder.
Like Esko said, you should create a JAR including all required JAR archives:
<zip destfile="abc.jar">
<zipgroupfileset dir="lib/distributed" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="com.acme.MyClass"/>
</manifest>
</zip>
After that, your start script is simply:
java -jar abc.jar
If you're using java 1.6 you can use file globs (i.e. java -cp "../lib/*"). If you're using an earlier version of java and you don't want to use Vladimir's solution, you'll need to write a script that figures out what the classpath should be.
So launch.sh looks something like:
cd dirname %0 # change to the bin directory, use %0/.. instead and you can replace ../lib with just /lib
sh set_classpath.sh # set the classpath
java -cp $CLASSPATH some.package.Main
and set_classpath.sh will have some linux magic that sets CLASSPATH equal to something like "../lib/abc.jar:../lib/def.jar"
export CLASSPATH=`ls *.jar | sed 's/[^.jar].jar/..\/lib\/\0:/'`

Categories