Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

832 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