How do I combine multiple Jar files into one deliverable Jar? - java

I have been researching trying to find a way to combine multiple Jar files into on Jar as a deliverable.
In a directory I have an ant file named Jar_gen.xml that consists of the following code in its entirety
<target name="combine-jars">
<jar destfile="out.jar">
<zipgroupfileset dir="lib" includes="*.jar"/>
</jar>
</target>
In that same directory I have another directory named lib which contains all of the Jar files I would like to flatten.
I have been running the ant script with
ant -buildfile Jar_gen.xml
making sure that I am running it from the same directory that the Jar_gen.xml file is in.
I am getting no output from my ant script and I have not idea why. Can someone please help me fix my script so I may flatten all of my jars and continue constructing my deliverable package.
NOTE
I have no main class so the Eclipse runnable Jar will not work for me
I have very little experience running Ant scripts so complete answers would be very helpful.

Is your directory structure set up correctly?
I created a quick test using your script and it appears to work, given that you have everything set up in a directory structure as
project_root_dir
- build.xml
- lib
- a.jar
- b.jar
and build.xml looks like:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="test">
<target name="combine-jars">
<jar destfile="out.jar">
<zipgroupfileset dir="lib" includes="*.jar"/>
</jar>
</target>
</project>
when I go to the project_root_dir and run "ant combine-jars", I get an out.jar that contains the contents of both a.jar and b.jar.

Related

Taskdef class com.temp.install.common.action.UserInstallDirRule cannot be found using the classloader AntClassLoader

I have created a jar with the .class files and the dependency libraries which are required for executing the class files using the below jar code
<target name="jar" depends="clean">
<jar destfile="${basedir}/lib/HelloWorld.jar">
<zipgroupfileset dir="${basedir}/lib" includes="*.jar" />
<fileset dir="${basedir}" includes="/com/temp/**" />
<fileset dir="${basedir}" includes="build.properties"/>
<manifest>
<attribute name="Class-Path" value="./HelloWorld.jar"/>
</manifest>
</jar>
</target>
Now i have written other build.xml to run taskdef actions using this jar but the following error occurs when i try to invoke the class files using the taskdef actions even though the class files and their dependencies are present in the same jar.
BUILD FAILED
C:\Users\kh2139\Desktop\New folder\build.xml:4: taskdef class com.temp.install.common.action.UserInstallDirRule cannot be found
using the classloader AntClassLoader[C:\Users\kh2139\Desktop\New folder\HelloWorld.jar]
Attaching my build.xml code below which is used to run taskdef actions on the HelloWorld.jar
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MyTask" basedir="." default="use">
<target name="use" description="Use the Task" >
<taskdef name="helloworld1" classname="com.temp.install.common.action.UserInstallDirRule" classpath="HelloWorld.jar"/>
<helloworld1/>
<taskdef name="helloworld" classname="com.temp.install.common.action.EncryptionGUID" classpath="HelloWorld.jar"/>
<helloworld/>
</target>
</project>
PS: I could able to run the build.xml file successfully without errors when i specify the lib folder in the location where i place HelloWorld.jar and give the classpath to the lib folder in the taskdef actions.
But my issue is i want to use the same jar to contain the dependencies that are used while executing the classes.
The error indicates that Java cannot find UserInstallDirRule.class in HelloWorld.jar. To determine if HelloWorld.jar contains the class, try running the jar.exe program included with the JDK.
Here's an example of running jar.exe in a Windows Command Prompt:
C:\>jar.exe tf "C:\Users\kh2139\Desktop\New folder\HelloWorld.jar"
The output will show whether UserInstallDirRule.class is in the JAR file.

Reference jars outside of a jar [duplicate]

I am trying to build an executable jar program which depends on external jar downloaded. In my project, I included them in the build path and can be run and debug within eclipse.
When I tried to export it to a jar, I can run the program but I can't when I try to press a button which includes function calls and classes from the external jar. I have edited the environment variables (Windows XP) CLASSPATH to include paths of all the external jar, but it doesn't work.
A point to note is that I got compile warnings while exporting my executable jar, but it doesn't show up any description about the warnings.
Would someone kindly provide a thorough guide on how to include an external jar program using eclipse?
Eclipse 3.5 has an option to package required libraries into the runnable jar.
File -> Export...
Choose runnable jar and click next.
The runnable jar export window has a radio button where you can choose to package the required libraries into the jar.
You can do this by writing a manifest for your jar. Have a look at the Class-Path header. Eclipse has an option for choosing your own manifest on export.
The alternative is to add the dependency to the classpath at the time you invoke the application:
win32: java.exe -cp app.jar;dependency.jar foo.MyMainClass
*nix: java -cp app.jar:dependency.jar foo.MyMainClass
How to include the jars of your project into your runnable jar:
I'm using Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.
Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.
Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.
Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.
As a good practice you can use an Ant Script (Eclipse comes with it) to generate your JAR file. Inside this JAR you can have all dependent libs.
You can even set the MANIFEST's Class-path header to point to files in your filesystem, it's not a good practice though.
Ant build.xml script example:
<project name="jar with libs" default="compile and build" basedir=".">
<!-- this is used at compile time -->
<path id="example-classpath">
<pathelement location="${root-dir}" />
<fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
</path>
<target name="compile and build">
<!-- deletes previously created jar -->
<delete file="test.jar" />
<!-- compile your code and drop .class into "bin" directory -->
<javac srcdir="${basedir}" destdir="bin" debug="true" deprecation="on">
<!-- this is telling the compiler where are the dependencies -->
<classpath refid="example-classpath" />
</javac>
<!-- copy the JARs that you need to "bin" directory -->
<copy todir="bin">
<fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
</copy>
<!-- creates your jar with the contents inside "bin" (now with your .class and .jar dependencies) -->
<jar destfile="test.jar" basedir="bin" duplicate="preserve">
<manifest>
<!-- Who is building this jar? -->
<attribute name="Built-By" value="${user.name}" />
<!-- Information about the program itself -->
<attribute name="Implementation-Vendor" value="ACME inc." />
<attribute name="Implementation-Title" value="GreatProduct" />
<attribute name="Implementation-Version" value="1.0.0beta2" />
<!-- this tells which class should run when executing your jar -->
<attribute name="Main-class" value="ApplyXPath" />
</manifest>
</jar>
</target>
Try the fat-jar extension. It will include all external jars inside the jar.
Update url: http://kurucz-grafika.de/fatjar
Homepage: http://fjep.sourceforge.net/
look #
java-jar-ignores-classpath-Workaround

Can not export folder from project to jar using Eclipse

I've got the following project structure:
-Project name
--src
--web-content
If I export my project as "Runnable JAR file", no 'web-content' folder will be in exported archive. How can I fix that?
You can check the Save as ANT script in export wizard.
Editing the ANT script, then run it to produce the archive that you want.
<project default="create_run_jar">
<target name="create_run_jar">
<jar destfile="my_run.jar">
<fileset dir="x:/workspace/Project name/bin"/><!-- Default output folder of your Java project -->
<fileset dir="x:/workspace/Project name/web-content"/><!-- add extra folder-->
</jar>
</target>
</project>

Log4j not being added to classpath in Ant

I'm attempting to add Log4j to my project's classpath in Ant which creates an executable JAR, but it appears that it's not being added properly.
Here is the path component of my Ant build script:
<path id="classpath.compile">
<fileset dir="${dir.myLibs}">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${dir.webContent}/WEB-INF/lib/log4j.jar" />
</path>
The compile target looks like this:
<target name="-compile">
<javac destdir="${dir.binaries}" source="1.6" target="1.6" debug="true" includeantruntime="false">
<src path="${dir.source}"/>
<classpath refid="classpath.compile"/>
</javac>
</target>
Tthe target that creates the JAR:
<target name="-createJar" >
<jar jarfile="${path.jarFile}"
manifest="${dir.source}\META-INF\MANIFEST.MF">
<fileset dir="${dir.binaries}" casesensitive="yes">
<exclude name="**/*.java"/>
</fileset>
</jar>
</target>
Lastly, the MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: ../../../WebContent/WEB-INF/lib/log4j.jar (what is this pathing relative to?)
Main-Class: foo.Bar
The JAR is created, but when I execute it, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger...
Any thoughts as to what I'm doing wrong?
It looks from the classpath in your MANIFEST that you are trying to reference a jar inside your jar. The only two ways to make that work AFAIK are 1) a special classloader, like #infosec812 mentions, or 2) by exploding the jar dependencies directly into the root of your jar. Either is workable, but I don't see either of them happening in your ant script.
If you're trying to reference a jar outside of your jar, your relative classpath is relative to the location of the jar you are executing. Make sure the referenced jar exists in that location.
I'm guessing that you're running the Java program as follows
java -jar myapp.jar
In this case you'll need to specify the Class-Path attribute in the manifest. I suggest you also check out the manifestclasspath task
Creating the jar does not include the linked libraries in the jar. You would have to have the required jars in your execution classpath in order to run it that way. Or, you could use the solution I use, which is to create a one-jar archive. It adds a specialized class loader for your application into the resulting jar and also packages your required jars in to the final executable jar. It works really well for deploying neat, simple to use packages.

Eclipse: How to build an executable jar with external jar?

I am trying to build an executable jar program which depends on external jar downloaded. In my project, I included them in the build path and can be run and debug within eclipse.
When I tried to export it to a jar, I can run the program but I can't when I try to press a button which includes function calls and classes from the external jar. I have edited the environment variables (Windows XP) CLASSPATH to include paths of all the external jar, but it doesn't work.
A point to note is that I got compile warnings while exporting my executable jar, but it doesn't show up any description about the warnings.
Would someone kindly provide a thorough guide on how to include an external jar program using eclipse?
Eclipse 3.5 has an option to package required libraries into the runnable jar.
File -> Export...
Choose runnable jar and click next.
The runnable jar export window has a radio button where you can choose to package the required libraries into the jar.
You can do this by writing a manifest for your jar. Have a look at the Class-Path header. Eclipse has an option for choosing your own manifest on export.
The alternative is to add the dependency to the classpath at the time you invoke the application:
win32: java.exe -cp app.jar;dependency.jar foo.MyMainClass
*nix: java -cp app.jar:dependency.jar foo.MyMainClass
How to include the jars of your project into your runnable jar:
I'm using Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.
Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.
Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.
Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.
As a good practice you can use an Ant Script (Eclipse comes with it) to generate your JAR file. Inside this JAR you can have all dependent libs.
You can even set the MANIFEST's Class-path header to point to files in your filesystem, it's not a good practice though.
Ant build.xml script example:
<project name="jar with libs" default="compile and build" basedir=".">
<!-- this is used at compile time -->
<path id="example-classpath">
<pathelement location="${root-dir}" />
<fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
</path>
<target name="compile and build">
<!-- deletes previously created jar -->
<delete file="test.jar" />
<!-- compile your code and drop .class into "bin" directory -->
<javac srcdir="${basedir}" destdir="bin" debug="true" deprecation="on">
<!-- this is telling the compiler where are the dependencies -->
<classpath refid="example-classpath" />
</javac>
<!-- copy the JARs that you need to "bin" directory -->
<copy todir="bin">
<fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
</copy>
<!-- creates your jar with the contents inside "bin" (now with your .class and .jar dependencies) -->
<jar destfile="test.jar" basedir="bin" duplicate="preserve">
<manifest>
<!-- Who is building this jar? -->
<attribute name="Built-By" value="${user.name}" />
<!-- Information about the program itself -->
<attribute name="Implementation-Vendor" value="ACME inc." />
<attribute name="Implementation-Title" value="GreatProduct" />
<attribute name="Implementation-Version" value="1.0.0beta2" />
<!-- this tells which class should run when executing your jar -->
<attribute name="Main-class" value="ApplyXPath" />
</manifest>
</jar>
</target>
Try the fat-jar extension. It will include all external jars inside the jar.
Update url: http://kurucz-grafika.de/fatjar
Homepage: http://fjep.sourceforge.net/
look #
java-jar-ignores-classpath-Workaround

Categories

Resources