Solved

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

Posted on 2008-10-06
5
1,266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

738 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