2013-10-30 163 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