[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-10-06
5
Medium Priority
?
1,274 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 1500 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

Technology Partners: 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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

873 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