Solved

Enmbedd external library in compiled JAR file with jar?

Posted on 2011-03-07
28
442 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 38
javap not working 8 46
throw exception 21 44
Is there a simpler dropbox system? 10 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

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