Jar, manifest file, main question

L

laredotornado

Hi,

I'm using Java 1.6 with Ant 1.7. I have a JAR file in which there are
two classes that define main (public static void main) methods.
However, my manifest file was sloppily constructed and did not include
any "Main-Class: MyPackage.MyClass" lines. Yet, I'm able to execute
command line invocations of each through Ant, similar to

<java classname="CMStarteamIncBuild" classpath="${cp}:$
{ant.home}/lib/CmStarTeamUtils.jar:${ant.home}/lib/starteam110.jar:/
Users/davea/.ant/lib/xercesImpl.jar" failonerror="true">
...
<arg value="${lvcva.checkout.dir}"/>
</java>

So my question is, why is the "Main-Class" directive necessary? -
Dave
 
L

Lew

laredotornado said:
I'm using Java 1.6 with Ant 1.7.  I have a JAR file in which there are
two classes that define main (public static void main) methods.
However, my manifest file was sloppily constructed and did not include
any "Main-Class: MyPackage.MyClass" lines.  Yet, I'm able to execute
command line invocations of each through Ant, similar to

        <java classname="CMStarteamIncBuild" classpath="${cp}:$
{ant.home}/lib/CmStarTeamUtils.jar:${ant.home}/lib/starteam110.jar:/
Users/davea/.ant/lib/xercesImpl.jar" failonerror="true">
                ...
                <arg value="${lvcva.checkout.dir}"/>
        </java>

So my question is, why is the "Main-Class" directive necessary?  -

Or more precisely, when is it necessary?

You provide little information about your scenario, really; missing
are the name of the JAR file, the name of the other "main" class, the
contents of the "cp" environment variable, and so on. None of that
affects your answer, though.

The manifest drives how the JAR executes with the "java -jar" option.
That's not how you executed the class "CMStarteamIncBuild". You
started "java" without the "-jar" option (implicitly via Ant). Had
you not specified the class to run, you would have had trouble.

I don't know the Ant magic to run "java -jar" but the Ant targets all
boil down to such commands in the end.

To summarize: JARs serve two purposes: as standalone deployment
vehicles and as library packagers. The manifest is necessary to run
the JAR as a standalone deployment vehicle rather than just another
element in the classpath.
 
J

Jean-Baptiste Nizet

Le 23/11/2010 17:50, laredotornado a écrit :
Hi,

I'm using Java 1.6 with Ant 1.7. I have a JAR file in which there are
two classes that define main (public static void main) methods.
However, my manifest file was sloppily constructed and did not include
any "Main-Class: MyPackage.MyClass" lines. Yet, I'm able to execute
command line invocations of each through Ant, similar to

<java classname="CMStarteamIncBuild" classpath="${cp}:$
{ant.home}/lib/CmStarTeamUtils.jar:${ant.home}/lib/starteam110.jar:/
Users/davea/.ant/lib/xercesImpl.jar" failonerror="true">
...
<arg value="${lvcva.checkout.dir}"/>
</java>

So my question is, why is the "Main-Class" directive necessary? -
Dave

The Main-Class directive is only necessary if you want to launch your
application by just double clicking on the jar file, or by executing the
command

java -jar myJarFile.jar

Read
http://download.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#Main Attributes
for more details.

If you launch your application by giving a whole classpath and a class
name, the directive isn't necessary.

JB.
 
A

Arne Vajhøj

I'm using Java 1.6 with Ant 1.7. I have a JAR file in which there are
two classes that define main (public static void main) methods.
However, my manifest file was sloppily constructed and did not include
any "Main-Class: MyPackage.MyClass" lines. Yet, I'm able to execute
command line invocations of each through Ant, similar to

<java classname="CMStarteamIncBuild" classpath="${cp}:$
{ant.home}/lib/CmStarTeamUtils.jar:${ant.home}/lib/starteam110.jar:/
Users/davea/.ant/lib/xercesImpl.jar" failonerror="true">
...
<arg value="${lvcva.checkout.dir}"/>
</java>

So my question is, why is the "Main-Class" directive necessary? -

When you don't specify the main class explicit.

Here you specify:
classname="CMStarteamIncBuild"

Arne
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top