Running several main-classes from the same JAR file

E

eli.hen

Hi,

I want to run 2 different main-classes from the same JAR. Is it
possible? How?

The point is that both main-classes use the same common classes, so it
seems better that all classes will reside in the same 1 JAR file.

-thanks, Eli
 
T

Thomas Kellerer

java -cp yourjar.jar package.Main1

java -cp yourjar.jar package.Main2

Read the docs for the java command, it's all explained there :)

Thomas
 
T

Thomas Hawtin

I want to run 2 different main-classes from the same JAR. Is it
possible? How?

The point is that both main-classes use the same common classes, so it
seems better that all classes will reside in the same 1 JAR file.

How are you expecting to select between the two? If you can set the
command line then, as Thomas Kellere suggested, put the jars on the
class-path and use the execute a class form of the java command.
Alternatively, add a new class as the main-class that determines which
of the other classes to run.

However, I'd suggest it is not normally worthwhile. You might as well
just have two smaller jar files. If you don't want duplication, you can
add to the class-path through the manifest.

http://java.sun.com/docs/books/tutorial/jar/manifest/downman.html

Tom Hawtin
 
A

Andrew Thompson

I want to run 2 different main-classes from the same JAR. Is it
possible?

Sure, you can do that, but[1]

One way is as described ny Thomsa Kellerer, to
launch two different mains from the one Jar by
invoking the class from the command line.

Another, more user-friendly way to deploy the
applications is using Webstart/JWS, which would simply
require you to specify a different main in each JNLP
descriptor.

*Note the jar file/s for JWS would need to be signed if
you intend the application/s to gain the 'full priviliges'
usually given to an app. launched from the command line.*
The point is that both main-classes use the same common classes,

[1] ..but, as Thomas Hawtin suggested, it may
not make sense to deploy the mains and core classes
in the same jar. Instead consider breaking it into
the 'common library' jar, with each main class and
it's own unique classes in a second (third, fourth..)
jar.

Webstart/JWS can handle that as well, and will only
deliver the library application once, even if it is
used for 10 different applications (or Webstarted
applets).
..so it
seems better that all classes will reside in the same 1 JAR file.

Not with the JWS option..
 
A

Andrew Thompson

Andrew said:
One way is as described ny Thomsa Kellerer, ..

Thomas, ..Thomas. (silly me)

<weak defence>
If only I could have 'replied' to both posts at once.
</weak defence>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top