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.showInputDialo g("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
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.showInputDialo
}
}
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
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.showInputDialo g("jon");
System.out.println(jon);
System.exit(0);
}
}
import javax.swing.*;
public class Class {
public static void main (String args[]) {
System.out.println("Hello World " );
String jon = JOptionPane.showInputDialo
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 ;-))
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 ;-))
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.
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.
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
?
java -classpath MyJar.jar Class
?
ASKER
When i type in java -classpath MyJar.jar Class
I get:
Exception in thread "main" java.lang.NoClassDefFoundE rror: 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.NoClassDefFoundE rror: Hello
I get:
Exception in thread "main" java.lang.NoClassDefFoundE
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.NoClassDefFoundE
ASKER
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.
Could not find the main class. Program will exit.
Ensure that the path to your files is in the Classpath environment variable.
ASKER
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.
java -jar yourCompiledClass.jar -cp "....." [args]
or
java -jar yourCompiledClass.jar -cp. [args]
if you are in the currect directory.
ASKER
OK that worked, thanks krakatoa
Now how can I make it work by just double clicking the jar file.
Now how can I make it work by just double clicking the jar file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.NoClassDefFoundE rror: org/netbeans/lib/AbsoluteL ayout
at Application.initComponents <Applicati on.java:13 6>
at Application.<init><Applica tion.java: 27>
at Application.main<Applicati on.main<Ap plication. java:2899>
Exception in thread "main" java.lang.NoClassDefFoundE
at Application.initComponents
at Application.<init><Applica
at Application.main<Applicati
ASKER
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?
ASKER
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
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. ;)
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. ;)
ASKER
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!
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.
:-)
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.
:-)
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
?