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
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.
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.
The documentation is extremely helpful with this question. Start with
the tutorial:
<http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html>
--
Lew
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
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>
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
>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.
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
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/
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)
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