Solved

need executable jar

Posted on 2007-03-21
16
543 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

728 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