Solved

Enmbedd external library in compiled JAR file with jar?

Posted on 2011-03-07
28
436 Views
Last Modified: 2012-05-11
Is there a way of embedding external libs (jars) into my compiled jar file using only jdk tools such as jar, javac? I would prefer to have all my files in one package.
0
Comment
Question by:endasil
  • 12
  • 6
  • 4
  • +2
28 Comments
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 500 total points
ID: 35058748

You can always unpack the jars and pack them back together in one file.

You can do that using jar tool:

http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
0
 

Author Comment

by:endasil
ID: 35058818
There has to be a easier way. Does everyone extract all the external libs needed for the project and then
add them to their own jar file? Or otherwise have a lib folder with all the externals? Sounds crazy!
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35058989

No. I dont know of anyone extracting them. All projects I have seen or worked on simply used the jars as they are found.
0
 

Author Comment

by:endasil
ID: 35059147
Ok lets give an example here. My application is dependent on:
appserv-deployment-client.jar
appserv-rt.jar
javaee.jar
ejbmodule3.jar
appserv-admin.jar
appserv-ext.jar

I then have two options if i want to use my project on an external computer:
1. Copy all those glassfish jars and my ejbmodule3.jar to the target computer.
2. Extract all classes from those 6 jar files, and then package the extracted files into my own jar?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35059547
You'd probably be better off using OneJar or the Eclipse FatJar plugin (essentially the same thing). That's the easiest way (a on-click procedure)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35059563
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35060362

Obviously this application communicates with the server. If that is the case, the standard solution would be to use Java Web Start:

http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136112.html

That's how we do it. This way the user gets the application with one click and he is not aware of how many jars you have.
0
 
LVL 92

Expert Comment

by:objects
ID: 35061670
Why do you want to put them all in one jar?  Defeats one of the reasons for having jars in the first place.
All you need to do is specify there names in the manifext
or put them in a ext directory
http://mindprod.com/jgloss/ext.html

Putting everything in one jar always ends up being more trouble than its worth
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35068121
If you ARE using JWS, then as dejanpazin says, it's just another archive to be added and it'll be treated transparently.

Otherwise, there's nothing wrong with using FatJar techniques - that's why an Eclipse plugin exists
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35699631
Unacceptable. At least one of the offered solutions required no tools external to the jdk at all
0
 
LVL 92

Expert Comment

by:objects
ID: 35702423
theres actually lots wrong with the fatjar approach, ask anyone who used it for a real application.
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35708217

The very first answer I provided is the actual answer to the original question.

The CEHJ's answer with Fat Jar may be good too, I cant say, since I dont know that tool.

All the rest is debate on other solutions.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35709790
>> ask anyone who used it for a real application.

e.g. you can ask me ;)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 92

Expert Comment

by:objects
ID: 35710973
accept 35058748
question clearly asks how to do it without any external tools
0
 

Author Comment

by:endasil
ID: 35711630

If i unpack jars and then add the content of those files into my jar i have not added a jar file to my package, i have added the class files. And it sounds awfully inefficieint to first unpack files and then add the content. I know netbeans can add various xml files to a jar file, so there has to be a way to get a file of my choice, regardless of format to a jar package using jar, javac from the command line.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35711635
>>At least one of the offered solutions required no tools external to the jdk at all

Accept http:#35058748
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35711638
>> And it sounds awfully inefficieint to first unpack files and then add the content.

(which is why i suggested using something that is designed to do all that for you)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35711643
>>so there has to be a way to get a file of my choice, regardless of format to a jar package using jar, javac from the command line.

Only by getting them out of the jar they're already in before putting them in a new one (unless you use the method i mentioned or something similar)
0
 
LVL 92

Expert Comment

by:objects
ID: 35711653
>  And it sounds awfully inefficieint to first unpack files and then add the content

to put everything in one jar you going to *have* to do that, its unavoidable.
But its completely unnecessary, librarys are broken up into multiple jars for a very good reason.

> o there has to be a way to get a file of my choice, regardless of format to a jar package using jar, javac from the command line.

yes, use ant
you don't want to be building your project manually. Thats is terribly inefficient
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35711663
Personally, if i were using Netbeans, i'd use 'One-Jar' (similar to FatJar) in the Netbeans build files using:

http://one-jar.sourceforge.net/index.php?page=build-tools&file=ant-example

http://technojeeves.com/joomla/index.php/free/111-source-zip-in-netbeans (do a one-jar here maybe instead of packaging source)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35711671
>>yes, use ant

Ant is already being used
0
 
LVL 92

Expert Comment

by:objects
ID: 35711676
> Ant is already being used

thats right, and it can handle rejarring *without* any need for 3rd party tools
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35711687
>>and it can handle rejarring *without* any need for 3rd party tools

Not reliably. Why do you imagine a one-jar ant task exists?
0
 
LVL 92

Expert Comment

by:objects
ID: 35711690
> Not reliably.

rubbish

> Why do you imagine a one-jar ant task exists?

for developers that don't fully understand how jars work

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35711695
endasil - if for some odd reason you want to know how to do it unreliably (and in some cases possibly even illegally) using zipfileset (part of Ant) - feel free to ask
0
 

Expert Comment

by:Mod_MarlEE
ID: 35763184
Starting the auto-close procedure on behalf of the Question Author, to implement the recommendations from the participating Expert(s).


-----
Everyt'ing is everyt'ing,

Mod_MarlEE
Community Support Moderator
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 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

19 Experts available now in Live!

Get 1:1 Help Now