Solved

How do I compile a JAR file that is truly self-contained?

Posted on 2008-10-06
5
1,262 Views
Last Modified: 2013-12-14
I am developing a piece of Java code that will be run as a scheduled task on another machine.  The code currently uses functionality out of two precompiled .jars: "mail.jar" and "activation.jar".  When I compile the code, I'm able to run the resulting .jar file, but only if it's in a directory that contains "mail.jar" and "activation.jar" in a "lib" directory (as in the "dist" directory).  What I'm looking for is a way to modify the build.xml file or change some settings in the IDE so that it will build all of the classes in any .jar file I add to the compile-time libraries list into the final .jar, allowing it to be completely self-contained.

Any suggestions?
0
Comment
Question by:FerrousHombre
  • 3
5 Comments
 
LVL 1

Expert Comment

by:gbglass
ID: 22654709
Extract mail.jar and activation.jar with the following command jar -xf file.jar

Then build a new jar file that includes classes from mail, activation and your own code.
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 22656524
You can use the following to create a single jar out of 2 jars

      
            
            
                  <format property="current" unit="millisecond" pattern="yyyyMMddhhmmssSSS" />
            
            
            
            
            
            
      
0
 

Author Comment

by:FerrousHombre
ID: 22659669
I'm creating the code as part of a NetBeans project, which auto-generates a large portion of the build.xml.  There are points where you can essentially insert custom code, but there are limits to what I can change.  NetBeans creates a build-impl.xml file, which has the "real" Ant script in it, and then a build.xml file, which is available to the user and lets them add targets for before and after the compile, build, run, etc. operations.
I'll see if I can get Ant to recognize the extra source files.
0
 

Author Comment

by:FerrousHombre
ID: 22872199
What I ended up having to do in the build.xml file, because of the way NetBeans generates most of the Ant script, is modify the "-pre-jar" target as shown in the code snippet.  That made the class files within each jar available to the target that actually builds the jar.  The final jar seems to be working entirely on it's own, now, which is what I was going for.
 

<target name="-pre-jar">
  <unjar src="${file.reference.mail.jar}" dest="${build.classes.dir}"/>
  <unjar src="${file.reference.activation.jar}"     dest="${build.classes.dir}"/>
</target>

Open in new window

0
 

Author Closing Comment

by:FerrousHombre
ID: 31503629
The unjar step was part of the solution, but NetBeans' generated ANT script wouldn't allow me to describe how to build the final jar as you specified, I had to unjar the libraries into the "build/classes" directory to get the build to work properly.
That's why I rated the solution as partially complete and gave it a B.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 38
Java exception bubble up 2 18
jboss wildfly 10.1 10 96
Which non-HTML GUI front end to use with Java? 3 22
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question