Link to home
Start Free TrialLog in
Avatar of 91mustang
91mustang

asked on

executable jar files

Hello I am trying to make a  sample executable jar file. For some reason it will not work..

This is the .class file i am putting in the jar:

import javax.swing.*;
public class Class {
    public static void main (String args[]) {
      System.out.println("Hello World " );
                String jon = JOptionPane.showInputDialog("jon");
    }
}


The .mf file is :
Manifest-Version: 1.0
Main-Class: Class

When i click on the created jar file, nothin happens. I have tried to run the jar in java.exe and javaw.exe
Avatar of jimmack
jimmack

I'm surprised that even compiled.  "Class" is already a class name in java.lang.

Change the name of that first to prevent confusion.  Just "MyClass" (or similar) will do.

Have you tried running it with:

java -classpath yourjar.jar Class

?
actually "Class" is ok since it is capatalized, but it is asking for some confusion.
First I must say that there is no stop condition.  The Option dialog only hides when one of the buttons is selected (it is still there) and the program should run forever then (with nothing seen.) try this
import javax.swing.*;
public class Class {
    public static void main (String args[]) {
     System.out.println("Hello World " );
     String jon = JOptionPane.showInputDialog("jon");
     System.out.println(jon);
     System.exit(0);
    }
}
The other question would be: How did you build the jar file?

You should have had a file (eg. called manifest) that contains the following line:

Main-Class: Class

Then you should have created the jar using:

jar cmvf manifest MyJar.jar Class.class

I've just tested it with Class.java and MyClass.java and it works OK (except for the exit condition pointed out by tomboshell ;-))
Avatar of krakatoa
To create an executable .jar :

jar cmf mainClass yourCompiledClass.jar yourCompiledClass*.class

To run it:

java -jar yourCompiledClass.jar [args]

Where mainClass is a file containing :

Main-Class: yourCompiledClass

k.
Avatar of 91mustang

ASKER

I created the jar using netbeans 3.5.1.
What happens if you type the following at the command line:

java -classpath MyJar.jar Class

?
When i type in java -classpath MyJar.jar Class
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: Class

I get the same error when tyring it with the demo file i downloaded from :
http://www.cs.princeton.edu/introcs/80systems/jar/jar.html
java -classpath Hello.jar Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
BTW, when i double click the jar icons of either file, the JVM launcher loads and gives me error message:
Could not find the main class. Program will exit.
Ensure that the path to your files is in the Classpath environment variable.
I'm sorry, I'm rather new to java, could you explain how I ensure that the path to the files is in the Classpath environment variable?
You can also call the jar with the -cp switch, and then supply the directory.
so ...

java -jar yourCompiledClass.jar -cp "....." [args]

or

java -jar yourCompiledClass.jar -cp. [args]

if you are in the currect directory.
OK that worked, thanks krakatoa
Now how can I make it work by just double clicking the jar file.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
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
Is there any restrictions on what kind of applications that you can create executable jars with. I tried to create an executable jar out of a application i wrote. when i attemp to run it from command line I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/netbeans/lib/AbsoluteLayout
       at Application.initComponents<Application.java:136>
       at Application.<init><Application.java:27>
       at Application.main<Application.main<Application.java:2899>

I wrote another app without Absolute layout, and it worked, then changed the layout to absolute, and got the same error. Will executable jar files work with Absolute Layout?
Never mind, I found the answer myself. Absolute layout can not be used in Jar files as Absolute layout is a Forte class and not part of the standard java libray.  Null layout works the same.

http://ruby.fgcu.edu/courses/mpenderg/spring2004%5Cism3230/JAR_and_JAVADOC.html.
thanks to everone that helped, i really appreciate your patience
Ah .. was away eating ! ;)

Thanks 91mustang; glad you got it sorted.

BTW, I'm not sure, but (maybe jimmack will drop in on this again),but there may be a way that you can get a .jar to run with some kind of association, or webstart or something - not quite sure. ;)
krakatoa

actually you can (with windows, havn't tested linux), when you double click on the icon, it will ask you what program you want to open with, browse to javaw.exe and it will run (click check box, and it will always accociate jar files with javaw.exe) thanks again!
Yes, it is possible to get the jar file running with an external .jar (in this case the Forte jar file).  In addition to the Main-Class: attribute, you need the Class-Path: attribute.  The only drawback is that you need to know where the extra jar file will be located.

I used this in my latest release, but it requires my jar file plus two others (JDOM and JavaHelp) to be packaged together in a .zip.  When the file is unzipped, all three jar files appear in the same directory, so the Class-Path attribute is set as follows:

Class-Path: jdom.jar jh.jar

Also, there's no equivalent to javaw.exe in Linux.

:-)