当前位置: 技术问答>java相关
why I can't run jar successfully?
来源: 互联网 发布时间:2015-01-19
本文导语: I'm reading . I create a file named SimpleBean.java : --------------------------------------------------------------- package sunw.demo.simple; import java.awt.*; import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable{ pr...
I'm reading .
I create a file named SimpleBean.java :
---------------------------------------------------------------
package sunw.demo.simple;
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas implements Serializable{
private Color color = Color.green;
public Color getColor(){
return color;
}
public void setColor(Color newColor){
color = newColor;
repaint();
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(20, 5, 20, 30);
}
public SimpleBean(){
setSize(60,40);
setBackground(Color.red);
}
}
--------------------------------------------------------
and a file named manifest.tmp :
--------------------------------------------------------
Name:SimpleBean.class
Java-Bean:True
--------------------------------------------------------
and javac SimpleBean.java successfully.
but when i run the command:
jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
i get an error like this:
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:353)
at java.util.jar.Manifest.read(Manifest.java:159)
at java.util.jar.Manifest.(Manifest.java:59)
at sun.tools.jar.Main.run(Main.java:124)
at sun.tools.jar.Main.main(Main.java:778)
sos,please help me
I create a file named SimpleBean.java :
---------------------------------------------------------------
package sunw.demo.simple;
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas implements Serializable{
private Color color = Color.green;
public Color getColor(){
return color;
}
public void setColor(Color newColor){
color = newColor;
repaint();
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(20, 5, 20, 30);
}
public SimpleBean(){
setSize(60,40);
setBackground(Color.red);
}
}
--------------------------------------------------------
and a file named manifest.tmp :
--------------------------------------------------------
Name:SimpleBean.class
Java-Bean:True
--------------------------------------------------------
and javac SimpleBean.java successfully.
but when i run the command:
jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
i get an error like this:
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:353)
at java.util.jar.Manifest.read(Manifest.java:159)
at java.util.jar.Manifest.(Manifest.java:59)
at sun.tools.jar.Main.run(Main.java:124)
at sun.tools.jar.Main.main(Main.java:778)
sos,please help me
|
In the manifest file of a JAR, it is possible to specify the class to be used when the JVM is lauched with the JAR as parameter. The class must have a main().
Try with this simple class import java.awt.*;
import java.awt.event.*;
public class MyClass {
public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
f.add(new Label("Hello world"));
f.setSize(200,200);
f.setVisible(true);
}
}
Then create this manifest file (manifest.mft) with any text editor. Manifest-Version: 1.0
Main-Class: MyClass
Classpath: .MyJar.jar
Next, you include the manifest file in the JAR (MyJar.jar) with the MyClass class. jar cvfm MyJar.jar manifest.mft MyClass.class
Then you are able to start the MyClass.class by double-clicking on the MyJar.jar file (if the JRE is correctly installed) or by typing java -jar MyJar.jar
On NT, you can also make JARs run from the command-line by setting the PATHEXT environment variable, for example set PATHEXT=.EXE;.BAT;.CMD;.JAR
Then if you have the jar file MyJar.jar accessible via the PATH environment variable, typing "MyJar" on the DOS command line will invoke "java -jar MyJar.jar" .
Try with this simple class import java.awt.*;
import java.awt.event.*;
public class MyClass {
public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener
(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
f.add(new Label("Hello world"));
f.setSize(200,200);
f.setVisible(true);
}
}
Then create this manifest file (manifest.mft) with any text editor. Manifest-Version: 1.0
Main-Class: MyClass
Classpath: .MyJar.jar
Next, you include the manifest file in the JAR (MyJar.jar) with the MyClass class. jar cvfm MyJar.jar manifest.mft MyClass.class
Then you are able to start the MyClass.class by double-clicking on the MyJar.jar file (if the JRE is correctly installed) or by typing java -jar MyJar.jar
On NT, you can also make JARs run from the command-line by setting the PATHEXT environment variable, for example set PATHEXT=.EXE;.BAT;.CMD;.JAR
Then if you have the jar file MyJar.jar accessible via the PATH environment variable, typing "MyJar" on the DOS command line will invoke "java -jar MyJar.jar" .