Enmbedd external library in compiled JAR file with jar?

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.
endasilAsked:
Who is Participating?
 
Dejan PažinHead of SW DevelopmentCommented:

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
 
endasilAuthor Commented:
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
 
Dejan PažinHead of SW DevelopmentCommented:

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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
endasilAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
0
 
Dejan PažinHead of SW DevelopmentCommented:

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
 
objectsCommented:
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
 
CEHJCommented:
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
 
CEHJCommented:
Unacceptable. At least one of the offered solutions required no tools external to the jdk at all
0
 
objectsCommented:
theres actually lots wrong with the fatjar approach, ask anyone who used it for a real application.
0
 
Dejan PažinHead of SW DevelopmentCommented:

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
 
CEHJCommented:
>> ask anyone who used it for a real application.

e.g. you can ask me ;)
0
 
objectsCommented:
accept 35058748
question clearly asks how to do it without any external tools
0
 
endasilAuthor Commented:

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
 
CEHJCommented:
>>At least one of the offered solutions required no tools external to the jdk at all

Accept http:#35058748
0
 
CEHJCommented:
>> 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
 
CEHJCommented:
>>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
 
objectsCommented:
>  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
 
CEHJCommented:
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
 
CEHJCommented:
>>yes, use ant

Ant is already being used
0
 
objectsCommented:
> Ant is already being used

thats right, and it can handle rejarring *without* any need for 3rd party tools
0
 
CEHJCommented:
>>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
 
objectsCommented:
> Not reliably.

rubbish

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

for developers that don't fully understand how jars work

0
 
CEHJCommented:
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
 
Robert MarleySinger / SongwriterCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.