Solved

need executable jar

Posted on 2007-03-21
16
535 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now