Ashwin Hegde
Ashwin Hegde

Reputation: 1771

How to create Jar and execute it correctly

I have following directory structure and files:

Folder
    +
    |--/org
    |--Online.java
    |--Online.class

When I execute following code:

C:\>cd Folder
C:\Folder>java Online http://www.google.com google.html

My files execute and give me correct output as per my requirement.

Now, I want to add the package 'org' and Online.class file in one Jar file say E.g. Online.jar

I did that by executing following code:

jar cvf Online.jar *

Now, when i am trying to execute following code:

java -jar Online.jar http://www.google.com google.html

It's shows me error:

Failed to load Main-Class manifest attribute from Online.jar

Can any one help me out here? I want to know What is wrong here ?

Upvotes: 2

Views: 219

Answers (5)

iKlsR
iKlsR

Reputation: 2672

I normally use the command jar -cmf MANIFEST_FILE jarfile.jar *.class and specify a custom Manifest file as I get the same errors as you do.

This is the simple Manifest File I use, (src)

Manifest-Version: 1.0
Class-Path: .
Main-Class: MainClassName

Warning: The manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

When running, I use java -jar jarfile.jar or just double click the .jar on Windows.

Upvotes: 0

devconsole
devconsole

Reputation: 7925

Assuming your FQCN is org.Online (which is a bit awkward) the structure should be:

Folder
    +
    |--org
    |    +
    |    |--Online.class
    |
    |--META-INF
         +
         |--MANIFEST.MF

That is:

  • You don't have to include Online.java.
  • Place the class file Online.class under the package directory org.
  • Include META-INF/MANIFEST.MF

The contents of META-INF/MANIFEST.MF should be:

Manifest-Version: 1.0
Main-Class: org.Online

That is, specify the fully-qualified class name.

Upvotes: 1

Nida Sahar
Nida Sahar

Reputation: 749

It depends on how you build your jar. You will need a Manifest file with the main class name

I usually use a ant to build, so my build task looks like this:

    <target name="Build_myjar">
        <jar destfile="my.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Main-Class" value="MyMainClass"/>
                <attribute name="Class-Path" value="."/>
            </manifest>
            <fileset dir="bin"/>
        </jar>
    </target>

Notice the Manifest already has the MainClass Name, hence that takes care of invoking the MainClass

Upvotes: 2

Bob Flannigon
Bob Flannigon

Reputation: 1294

I think the default manifest may need to be modified to include the main-class...

  Main-Class: Online.class

Upvotes: 1

qwazer
qwazer

Reputation: 7531

Look java tutorial page: Setting an Application's Entry Point

Upvotes: 2

Related Questions