Solved

need executable jar

Posted on 2007-03-21
16
538 Views
Last Modified: 2013-11-23
Hello experts,
I am trying to create an executable jar file using latest jdk1.6.0 on Windows XP box.
I see this problem has come up before but I just cant follow the solutions given. It must be somjething very simple I am missing.
I have started with very simple HelloWorldApp.java file and using a batch file on cmd.exe to build.
My batch file is:
cd C:\MyHello
javac HelloWorldApp.java
java HelloWorldApp
jar cvf HelloWorld.jar HelloWorldApp.java

This works perfectly and the second line produces HelloWorldApp.class then the third line runs the file at least in cmd.exe.
The fourth line creates a jar file
When I try to run the jar file by double clicking on it I get error, "Failed to load Main-Class manifest attribute from HelloWorld.jar"
When I look at the contents of HelloWorld it shows that a default manifest was added.
I tried making a separate manifest file with
Main-Class: HelloWorldApp and typed jar umf manifest helloworld.jar into cmd.exe but got io error system cannot find the file specified.
My question is, what more do I have to do to be able to run the jar file without this error.
Here is the HelloWorldApp.java:
/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}
0
Comment
Question by:bravotango
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18763363
>>jar cvf HelloWorld.jar HelloWorldApp.java

Should be

jar cvf HelloWorld.jar HelloWorldApp.class
0
 

Author Comment

by:bravotango
ID: 18763421
Thanks for quick reply CEHJ, I just tried that and same result
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 18763479
You need to make a manifest file then

jar cvfm HelloWorld.jar yourmf.mf HelloWorldApp.class

In the meantime you should be able to run

java -cp HelloWorld.jar HelloWorldApp
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 86

Expert Comment

by:CEHJ
ID: 18763487
The manifest file would be

Main-Class: HelloWordApp.class

with a couple of blank lines at end
0
 

Author Comment

by:bravotango
ID: 18763664
Yes it still doesnt work and I can use java -cp HelloWorld.jar HelloWorldApp on the command line and it works perfectly there.
0
 

Author Comment

by:bravotango
ID: 18763689
It is such a tiny project which starts with just one file all in one folder. Would be nice if someone could just run it with a similar batch file and get the same result to see whats wrong.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18763770
Please show your last command
0
 

Author Comment

by:bravotango
ID: 18763860
This is everything in my batch file:

javac HelloWorldApp.java
java HelloWorldApp
jar cvf HelloWorld.jar HelloWorldApp.java
jar umf manifest HelloWorld.jar

btw how do you make comments in a batch file?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18764029
>>btw how do you make comments in a batch file?

::update the jar with the custom manifest file
jar umf manifest HelloWorld.jar
:: of course the above mean that the manfest file must be called 'manifest'
0
 

Author Comment

by:bravotango
ID: 18764267
Tried that again and still get java.io.FileNotFoundException: manifest (The system cannot find the file specified)
Have to give it away for now as late here. Thanks for help CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18764402
You need the file called 'manifest' containing the manifest
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 18767433
0
 

Assisted Solution

by:iXian_
iXian_ earned 100 total points
ID: 18769031
Here are the steps to make an executable JAR file:

Create a file called "manifest.txt" (not including the quotes).  
The contents of the file should be:

    Main-Class: HelloWordApp

There _must_ be a newline at the end of the file!  
Your .BAT should look like this:

    javac HelloWorldApp.java
    jar cmvf manifest.txt helloworld.jar HelloWorldApp.class

Then you execute with:

    java -jar helloworld.jar
0
 

Author Comment

by:bravotango
ID: 18769086
Had a look at that small Divelog example Objects and had a few minor problems, namely
java.io.FileNotFoundException: mymanifest (The system cannot find the file specified)
Finally stumbled on the omission of the file type suffix
jar cvfm DiveLog.jar mymanifest DiveLog.class DiveLog$1.class DiveLog$2.class Welcome.class
should be
jar cvfm DiveLog.jar mymanifest.mf DiveLog.class DiveLog$1.class DiveLog$2.class Welcome.class

Corrected to mymanifest.mf and it Divelog finally worked.

I then tried this with my helloworldapp and it compiled OK but still have error "Could not find main class".

I then added first line to the manifest.mf for helloworldapp "Manifest-Version: 1.2", similar to the Divelog example but got a duplicate line error.  Real reason for errors here is I was building the jar with
jar cvf HelloWorld.jar HelloWorldApp.java and then updating the manifest with
jar umf mymanifest.mf HelloWorld.jar This doesnt seem to work. It seems better to use
jar cvfm HelloWorld.jar mymanifest.mf HelloWorldApp.class
I did this and it finally worked. I now have a clickable exeutable jar file. It does absolutely nothing but at least it works.

This is really a difficult question to answer (or ask) and as it is now solved I propose dividing the points between CEHJ and Objects if that is OK. Thanks for all the comments.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18769788
Glad it's working
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18770014
:-)
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question