Solved

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

Posted on 2008-10-06
5
1,260 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now