?
Solved

Enmbedd external library in compiled JAR file with jar?

Posted on 2011-03-07
28
Medium Priority
?
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 6
  • 4
  • +2
28 Comments
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 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