Go Back   Coding Forum > Coding World > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2012, 01:30 AM   #1 (permalink)
Nasser M. Abbasi
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar that includeseverything?


Hello;

Its been few years since I've used Java, since jdk 1.1 I think,
and I'd just like to ask a simple question:

Is it possible to package my Java application,
including all and any other Java code, that I might have
downloaded from the web and used, into one jar file,
such that one can just download this one jar file and
double click on it on their PC or Linux or Mac, and it
will run (ofcourse one needs to have the JRE installed
on their end).

I've done something like this before, and I remember using
Makefiles and the jar command

jar cvf0 myjar.jar .../*.class

I assume only *.class files can go into a Jar. Can other
jar files go into a Jar file also?

For example, If I use 3rd party Jar file myself, do I
need to extract the content of that Jar file out first,
and then jar the resulting tree into my jar file?

Just wanted to check if things has changed, and if
there might be now better tools or ways to do this,
as I have not kept up with Java.

If someone has a good link I can read on this whole topic
of building one Jar for an application, and what to
watch for, that will help. I am using latest JDK.

thanks,
--Nasser
  Reply With Quote
Old 02-20-2012, 02:30 AM   #2 (permalink)
Arne Vajhøj
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar thatincludes everything?

On 2/19/2012 9:25 PM, Nasser M. Abbasi wrote:
> Its been few years since I've used Java, since jdk 1.1 I think,
> and I'd just like to ask a simple question:
>
> Is it possible to package my Java application,
> including all and any other Java code, that I might have
> downloaded from the web and used, into one jar file,
> such that one can just download this one jar file and
> double click on it on their PC or Linux or Mac, and it
> will run (ofcourse one needs to have the JRE installed
> on their end).
>
> I've done something like this before, and I remember using
> Makefiles and the jar command
>
> jar cvf0 myjar.jar .../*.class
>
> I assume only *.class files can go into a Jar. Can other
> jar files go into a Jar file also?
>
> For example, If I use 3rd party Jar file myself, do I
> need to extract the content of that Jar file out first,
> and then jar the resulting tree into my jar file?
>
> Just wanted to check if things has changed, and if
> there might be now better tools or ways to do this,
> as I have not kept up with Java.
>
> If someone has a good link I can read on this whole topic
> of building one Jar for an application, and what to
> watch for, that will help. I am using latest JDK.


Java does not standard read jars in jars (you can get special
classloaders that can do it though).

You can obviously package all the class files you need
from various external jar files into you jar file.

But I will suggest that you keep your stuff in your jar
file and the external stuff in their jar files.

That enables independent updates of your stuff and the
external stuff.

And if it is simplicity of distribution you need, then
take a look at Java Web Start.

Arne



  Reply With Quote
Old 02-20-2012, 02:30 AM   #3 (permalink)
Roedy Green
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar that includes everything?

On Sun, 19 Feb 2012 20:25:11 -0600, "Nasser M. Abbasi" <nma@12000.org>
wrote, quoted or indirectly quoted someone who said :

>
>If someone has a good link I can read on this whole topic
>of building one Jar for an application, and what to
>watch for, that will help. I am using latest JDK.

There are reasons not to do that, but all you would have to do is
unpack all the jars, and rebuild them and sign the whole thing.
See http://mindprod.com/jgloss/genjar.html
http://mindprod.com/jgloss/jarexe.html
http://mindprod.com/jgloss/jarsignerexe.html

--
Roedy Green Canadian Mind Products
http://mindprod.com
One of the most useful comments you can put in a program is
"If you change this, remember to change ?XXX? too".

  Reply With Quote
Old 02-20-2012, 05:50 AM   #4 (permalink)
Lew
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar thatincludes everything?

Arne Vajhøj wrote:
> Nasser M. Abbasi wrote:
>> Its been few years since I've used Java, since jdk 1.1 I think,
>> and I'd just like to ask a simple question:


Life has somewhat changed in Java universe since then.

>> Is it possible to package my Java application,
>> including all and any other Java code, that I might have
>> downloaded from the web and used, into one jar file,
>> such that one can just download this one jar file and
>> double click on it on their PC or Linux or Mac, and it
>> will run (ofcourse one needs to have the JRE installed
>> on their end).


Yes and no. The "jar" and "java -jar" commands /per se/ do not carry that
capability, but you can package things such that they will work.

Besides external packaging products, and Arne's excellent suggestion of Web
Start, the JAR format is intended as the vehicle to distribute Java
applications, but it does not pretend to package third-party works for you.
Nor should you be tempted to unpackage third-party work and package their
classes into your own JAR. That way lies madness.

If you study the JAR documentation you find the manifest file "MANIFEST.MF".
One of its elements is "Class-Path:", which specifies where third-party
libraries go *relative to the JAR* in its final resting place.

So the trick is to package third-party JARs with your JAR in parallel, or
conventionally, in a "lib/" subdirectory relative to your JAR's deployment.

The rest is packaging. I like ZIP. You put your JAR and all the library JARs
in a ZIP. It looks like this (from a project I just did as an exercise):

$ cd ~/projects/someproject/
$ ls configurator.* lib/
configurator.jar configurator.zip

lib/:
log4j-1.2.16.jar
$ unzip -l configurator.zip
Archive: configurator.zip
Unzip into desired directory, run 'java -jar configurator.jar &' from there.
Length Date Time Name
--------- ---------- ----- ----
23197 2012-02-11 19:25 configurator.jar
481534 2012-02-11 15:40 lib/log4j-1.2.16.jar
--------- -------
504731 2 files
$

Now that ZIP file is a complete distribution package for the 'configurator'
program.

It is one step away from your request. You have to unpack the ZIP on the
target machine, say, into "/var/opt/configurator/".

$ ls /var/opt/configurator/ /var/opt/configurator/lib/
/var/opt/configurator/:
configurator.jar configurator.zip lib

/var/opt/configurator/lib/:
log4j-1.2.16.jar
$

Now a GUI can run "configurator.jar" with a double click if you associate JAR
files with the "java -jar" command.

Here's the manifest:
$ unzip -p configurator.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Class-Path: lib/log4j-1.2.16.jar
Created-By: Lew Bloch
Main-Class: exercise.ui.ConfiguratorScreen
$

>> I've done something like this before, and I remember using
>> Makefiles and the jar command
>>
>> jar cvf0 myjar.jar .../*.class
>>
>> I assume only *.class files can go into a Jar. Can other
>> jar files go into a Jar file also?


Yes, but it won't help you. In fact, your assumption is wrong. You can put any
kind of file whatsoever into a JAR file. Why would you assume when you can
look it up?

>> For example, If I use 3rd party Jar file myself, do I
>> need to extract the content of that Jar file out first,
>> and then jar the resulting tree into my jar file?


NO!

>> Just wanted to check if things has changed, and if
>> there might be now better tools or ways to do this,
>> as I have not kept up with Java.


Things have changed, and there are other tools for other use cases, but really
nothing better than the JAR, which is magnificent for the purpose.

>> If someone has a good link I can read on this whole topic
>> of building one Jar for an application, and what to
>> watch for, that will help. I am using latest JDK.


Have you considered Googling for that?

Have you considered reading the Oracle Java site? You know, they really do
have some excellent documentation there. In fact, from the technologies page
<http://www.oracle.com/technetwork/java/javase/tech/index.html>
there's a link right to a whole bunch of stuff about JARs, ion the column
labeled "Tools and Utilities"
<http://docs.oracle.com/javase/7/docs/technotes/guides/jar/index.html>
Though sometimes (but not always) somewhat superficial, the tutorials are a
great introduction to all things Java
<http://www.oracle.com/technetwork/java/javase/documentation/tutorials-jsp-138802.html>

I am somewhat surprised you aren't using these resources, as they come from
the same site whence you get the downloads.

> Java does not standard read jars in jars (you can get special
> classloaders that can do it though).
>
> You can obviously package all the class files you need
> from various external jar files into you jar file.
>
> But I will suggest that you keep your stuff in your jar
> file and the external stuff in their jar files.


Absolutely.

Quite aside from the compelling technical reasons to do things correctly, you
can have interesting licensing issues if you repackage other people's code
into your own.

> That enables independent updates of your stuff and the
> external stuff.
>
> And if it is simplicity of distribution you need, then
> take a look at Java Web Start.


JARs in ZIPs aren't all that complicated, and can boldly go where Web Start
sometimes can't. OTOH, Web Start handles dependencies for you when you can
rely on a suitable server for distribution.
<http://docs.oracle.com/javase/7/docs/technotes/guides/javaws/index.html>

But OP, all this is pretty accessible on the Java site, even if you aren't too
terribly familiar with it. In fact, there's a lot more there I haven't even
had time to suggest, so I suggest that you explore it fully. You'll get much
better answers more quickly from the material at your fingertips than trying
to piece it together piecemeal from Usenet posts, and you won't waste
everyone's time with stuff that is trivial for you to find out.

After that, IBM Developerworks has a ton of useful Java articles.

And remember, GIYF.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
  Reply With Quote
Old 02-20-2012, 08:30 PM   #5 (permalink)
Fredrik Jonson
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar thatincludes everything?

In <jhsau8$8oo$1@speranza.aioe.org> Nasser M. Abbasi wrote:

> Is it possible to package my Java application, including all and any
> other Java code, that I might have downloaded from the web and used, into
> one jar file.


You can. If you use maven for build management you can use the shade plugin
to build such "uberjars". I believe there are similar utilities for ant.

https://maven.apache.org/plugins/maven-shade-plugin/

> I've done something like this before, and I remember using Makefiles and
> the jar.


Yes, jar files are just a zip container for class files so that is
possible. You can put whatever files (resources) you like in the jar, not
only class files.

If you have more than one project I'd strongly recommend avoiding Makefiles
nowdays and using maven, ant, ivy or some other special purpose build
management tool for java.

https://maven.apache.org/guides/gett...e-minutes.html

--
Fredrik Jonson
  Reply With Quote
Old 02-20-2012, 09:30 PM   #6 (permalink)
Lew
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar thatincludes everything?

On 02/20/2012 12:48 PM, Fredrik Jonson wrote:
> In<jhsau8$8oo$1@speranza.aioe.org> Nasser M. Abbasi wrote:
>
>> Is it possible to package my Java application, including all and any
>> other Java code, that I might have downloaded from the web and used, into
>> one jar file.

>
> You can. If you use maven for build management you can use the shade plugin
> to build such "uberjars". I believe there are similar utilities for ant.


You can, but you shouldn't.

> https://maven.apache.org/plugins/maven-shade-plugin/
>
>> I've done something like this before, and I remember using Makefiles and
>> the jar.

>
> Yes, jar files are just a zip container for class files so that is
> possible. You can put whatever files (resources) you like in the jar, not
> only class files.
>
> If you have more than one project I'd strongly recommend avoiding Makefiles
> nowdays and using maven, ant, ivy or some other special purpose build
> management tool for java.
>
> https://maven.apache.org/guides/gett...e-minutes.html


I am not a big fan of Maven. It's too clever by half. If you aren't really,
really careful it can create a mess from incompatible dependencies on
third-party JARs, and often quietly updates things without notification so you
don't have good control over your dependency versions without a lot of effort.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
  Reply With Quote
Old 02-20-2012, 11:30 PM   #7 (permalink)
Arne Vajhøj
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar thatincludes everything?

On 2/20/2012 5:28 PM, Lew wrote:
> On 02/20/2012 12:48 PM, Fredrik Jonson wrote:
>> In<jhsau8$8oo$1@speranza.aioe.org> Nasser M. Abbasi wrote:
>>> I've done something like this before, and I remember using Makefiles and
>>> the jar.

>>
>> Yes, jar files are just a zip container for class files so that is
>> possible. You can put whatever files (resources) you like in the jar, not
>> only class files.
>>
>> If you have more than one project I'd strongly recommend avoiding
>> Makefiles
>> nowdays and using maven, ant, ivy or some other special purpose build
>> management tool for java.
>>
>> https://maven.apache.org/guides/gett...e-minutes.html
>>

>
> I am not a big fan of Maven. It's too clever by half. If you aren't
> really, really careful it can create a mess from incompatible
> dependencies on third-party JARs, and often quietly updates things
> without notification so you don't have good control over your dependency
> versions without a lot of effort.


I am not a big fan of Maven either.

But if one really want to do things the Maven way it is OK.

It is just my experience that I usually want to do something
differently.

So I like the more programmatic approach in ant and I accept
having to write some build.xml's.

But I do know people that are happy about Maven.

Arne





  Reply With Quote
Old 02-20-2012, 11:30 PM   #8 (permalink)
Arne Vajhøj
Guest
 
Posts: n/a
Default is it possible to package a Java application in one jar thatincludes everything?

On 2/20/2012 1:41 AM, Lew wrote:
> Arne Vajhøj wrote:
>> Nasser M. Abbasi wrote:
>>> For example, If I use 3rd party Jar file myself, do I
>>> need to extract the content of that Jar file out first,
>>> and then jar the resulting tree into my jar file?

>> And if it is simplicity of distribution you need, then
>> take a look at Java Web Start.

>
> JARs in ZIPs aren't all that complicated, and can boldly go where Web
> Start sometimes can't. OTOH, Web Start handles dependencies for you when
> you can rely on a suitable server for distribution.
> <http://docs.oracle.com/javase/7/docs/technotes/guides/javaws/index.html>


And if unzipping is a problem (it actually is for some end users), then
install kit generators exists that can help with the unpacking process.

Arne
  Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT. The time now is 01:38 AM.


Powered by vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Copyright ©2010, CodingForum.Org