2013-10-30 272 views
0

这些正是创建可执行jar文件的步骤,以便我可以通过双击运行我的swing程序,但它给了我一个错误,说“无效或损坏的jar文件” 也试图通过命令提示符执行它给了我相同的错误无法打开可执行jar文件

Step 1:create a .java file which contains my swing code 
Step 2:compile the .java file to .class file 
Step 3:create jar file using the command jar cf name.jar classname.class 
Step 4:open jar file using winrar archiever and modify manifest file by adding Main-class:classname 
Step 5:try to execute the jar file 

但上述步骤不工作,我不明白为什么?

我想另一件事太

Step 1:create a .java file which contains my swing code 
Step 2:compile the .java file to .class file 
Step 3:create jar file using the command jar cf name classname.class 
Step 4:open jar file using winrar archiever and modify manifest file by adding Main-class:classname 
Step 5:try to execute the jar file.In this case i can open my file through command prompt but not by double clicking. I tried to select javaw option through Open with option but it doesn't give any ouput(not even error) 
+0

试着用'java -jar file.jar'运行它。 –

+0

我试过,但它给出了同样的错误“无效或损坏的jar文件” – Bhushan

回答

2

你可以试试:

java -cp name.jar Classname 

,并检查在命令提示符下的错误。

如果您的程序需要一些外部库,您可以在清单文件中提及它们。

Class-Path: lib/file1.jar lib/file2.jar 

其中lib位于jar文件所在的同一目录中。

+0

这个工程。但你能解释为什么吗? – Bhushan

+0

'-cp'表示您将jar包括在classpath中。 –

+0

我很抱歉,但我不明白这是什么意思。你能再解释一次吗? – Bhushan