Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to specify the main() class in "jar -cfe ..." ?

637 views
Skip to first unread message

Raymond Schanks

unread,
Mar 31, 2010, 4:14:10 AM3/31/10
to
Assume I want to create a jar archive with a command

jar -cfe myjar.jar <MainApp> *.class

for some classes. The entrypoint should be the main() method in class "MyTestclass123".

What do I have to specify for <MainApp> in the command above?

Do I have to write:

jar -cfe myjar.jar main *.class

or

jar -cfe myjar.jar MyTestclass123 *.class

What if the main() class is the only main() method in all classes. Can I omit it then and write simply

jar -cf myjar.jar *.class

?

Raymond

Jean-Baptiste Nizet

unread,
Mar 31, 2010, 9:20:04 AM3/31/10
to
On 31 mar, 10:14, ray....@caltech.org (Raymond Schanks) wrote:
> Assume I want to create a jar archive with a command
>
> jar -cfe myjar.jar <MainApp> *.class
>
> for some classes. The entrypoint should be the main() method in class "MyTestclass123".
>
> What do I have to specify for <MainApp> in the command above?
>
> Do I have to write:
>
> jar -cfe myjar.jar main *.class
>
> or
>
> jar -cfe myjar.jar MyTestclass123 *.class
>

See http://java.sun.com/javase/6/docs/technotes/tools/windows/jar.html#options
You have to specify the name of the class. Since the class is
MyTestclass123, your second one is the right one (jar -cfe myjar.jar
MyTestclass123 *.class)

> What if the main() class is the only main() method in all classes. Can I omit it then and write simply
>
> jar -cf myjar.jar *.class
>
> ?

You seem to be confused between methods and classes. main is a method.
It's contained in a class which, in your example, is named
MyTestclass123. Jar oesn't analyze the class files. It won't
automagically discover the only class in your set of classes which has
a main method. Note that if it did, it would be very fragile, since it
wouldn't work anymore as soon as you add another class containing a
main method.

PS : why don't you simply test it?
PS2 : It's a good practice to put all your classes in a package
structure, and to avoid using the default package. The command line
should thus be something like
jar -cfe myjar.jar your.package.name.MyTestclass123 your/*

JB.

markspace

unread,
Mar 31, 2010, 10:33:55 AM3/31/10
to
Raymond Schanks wrote:
>
> jar -cfe myjar.jar MyTestclass123 *.class


I think it's this one. Don't forget to specify a package name if it exists:

jar -cfe myjar.jar com.foo.MyTestclass123 *.class


http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html

>
> What if the main() class is the only main() method in all classes.
> Can I omit it


No.

Lew

unread,
Mar 31, 2010, 12:41:08 PM3/31/10
to

The documentation is extremely helpful with this question. Start with
the tutorial:
<http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html>

--
Lew

Lew

unread,
Mar 31, 2010, 12:46:59 PM3/31/10
to
Raymond Schanks wrote:
>> What if the main() class is the only main() method in all classes.
>> Can I omit it
>

markspace wrote:
> No.
>

Not entirely correct. He can omit the "e" parameter and main class
specification from the command line if he uses the manifest to specify
the entry point.

--
Lew

John B. Matthews

unread,
Mar 31, 2010, 2:04:43 PM3/31/10
to
In article <4bb30451$0$6879$9b4e...@newsspool2.arcor-online.net>,
ray...@caltech.org (Raymond Schanks) wrote:

Amplifying on others' helpful answers, Apache Ant offers a convenient
way to construct a JAR file with a manifest:

<http://ant.apache.org/manual/CoreTasks/jar.html>
<http://ant.apache.org/manual/CoreTasks/manifest.html>

This small project, which allows one to examine the manifest in place,
includes a corresponding build.xml file:

<http://sites.google.com/site/drjohnbmatthews/manifesto>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

jaap

unread,
Mar 31, 2010, 3:14:17 PM3/31/10
to
Op 31-03-10 10:14, schreef Raymond Schanks:
Use
jar -cf myjar.jar *
Then you can start your program with:
java -cp myjar.jar MyTestclass123

If you want to start your program with:
java -jar myjar.jar

you have to add the following option in the META-INF/MANIFEST file:
Main-Class: Mytestclass123

according to the jar --help information you can provide your own
manifestfile with the option -m FILE

bye,
Jaap

Roedy Green

unread,
Mar 31, 2010, 5:45:19 PM3/31/10
to
On Wed, 31 Mar 2010 06:20:04 -0700 (PDT), Jean-Baptiste Nizet
<jni...@gmail.com> wrote, quoted or indirectly quoted someone who said
:

>On 31 mar, 10:14, ray....@caltech.org (Raymond Schanks) wrote:
>> Assume I want to create a jar archive with a command
>>
>> jar -cfe myjar.jar <MainApp> *.class
>>

A jar must contain a manifest with a Main-Class statement. That says
which class to run. It always runs the static method main.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you tell a computer the same fact in more than one place, unless you have an automated mechanism to ensure they stay in sync, the versions of the fact will eventually get out of sync.

Arne Vajhøj

unread,
Mar 31, 2010, 7:34:17 PM3/31/10
to

If you want your users to specify the main class themselves,
then you don't need to do anything.

If you want Java to find the main class for your users,
then you should put a Main-Class directive in the manifest file.

Start here:
http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html

Arne

Knute Johnson

unread,
Mar 31, 2010, 8:36:13 PM3/31/10
to

I like Jaap's answer the best with the addition that you may have many
classes with a main() method in them in the same .jar file and you can
specify which to run with the method above. I use the same technique
with some demo applets on my website. One jar contains several demo
applets.

--

Knute Johnson
email s/nospam/knute2010/

Alan Malloy

unread,
Apr 3, 2010, 8:23:01 PM4/3/10
to

This is not done with the jar CLI. Instead, you need to include a
special file in the jar, called a manifest. It can specify lots of
things, but of particular value for you is that it specifies the main()
class. Relevant links:

Manifest basics
http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html

Setting main() class
http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html

--
Cheers,
Alan (San Jose, California, USA)

Lew

unread,
Apr 3, 2010, 10:53:37 PM4/3/10
to
Raymond Schanks wrote:
>> Assume I want to create a jar archive with a command
>>
>> jar -cfe myjar.jar <MainApp> *.class
>>
>> for some classes. The entrypoint should be the main() method in class
>> "MyTestclass123".
>>
>> What do I have to specify for <MainApp> in the command above?
>>
>> Do I have to write:
>>
>> jar -cfe myjar.jar main *.class
>>
>> or
>>
>> jar -cfe myjar.jar MyTestclass123 *.class
>>
>> What if the main() class is the only main() method in all classes. Can
>> I omit it then and write simply
>>
>> jar -cf myjar.jar *.class
>>
>> ?

Alan Malloy wrote:
> This is not done with the jar CLI.

Sure it is, sometimes.
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/jar.html#options>
"e Sets entrypoint as the application entry point for stand-alone
applications bundled into executable jar file. The use of this option creates
or overrides the Main-Class attribute value ..."

> Instead, you need to

Instead you may

> include a special file in the jar, called a manifest.
> It can specify lots of things, but of particular value for you is
> that it specifies the main() class. Relevant links:
>
> Manifest basics
> http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
>
> Setting main() class
> http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html

This latter link comes highly, or at least frequently recommended.

--
Lew

0 new messages