Link to home
Start Free TrialLog in
Avatar of bravotango
bravotango

asked on

need executable jar

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.
    }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>jar cvf HelloWorld.jar HelloWorldApp.java

Should be

jar cvf HelloWorld.jar HelloWorldApp.class
Avatar of bravotango
bravotango

ASKER

Thanks for quick reply CEHJ, I just tried that and same result
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The manifest file would be

Main-Class: HelloWordApp.class

with a couple of blank lines at end
Yes it still doesnt work and I can use java -cp HelloWorld.jar HelloWorldApp on the command line and it works perfectly there.
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.
Please show your last command
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?
>>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'
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
You need the file called 'manifest' containing the manifest
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Glad it's working
:-)