2012-05-10 36 views
1

好吧,這裏有什麼讓我感到困惑:這個jar版本有什麼缺失?

我一直在使用Netbeans編譯和運行一些Java程序,現在我已經得到了一個掛它,我試圖建立一個jar文件打開它在NetBeans外部,但我被這個錯誤在臉上砸到:

Error: Could not find or load main class C:\Users\n\Desktop\DirectoryLi 
st_1\DirectoryList_1\build\classes\directorylist\DirectoryList_1.jar 

什麼我不能確定的是如何/爲什麼發生這種情況,因爲我可以運行程序,它會產生一個文本文件與一些信息,當我在Netbeans上運行它時是完全正常的,什麼被排除?什麼地方出了錯?

下面是編譯jar的日誌:

感謝您的幫助!

init: 
deps-clean: 
Updating property file: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\built-clean.properties 
Deleting directory C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build 
clean: 
init: 
deps-jar: 
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build 
Updating property file: C:\Users\Maxunknown\Desktop\DirectoryList_1\DirectoryList_1\build\built-jar.properties 
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\classes 
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\empty 
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\generated-sources\ap-source-output 
Compiling 1 source file to C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\classes 
compile: 
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\dist 
Copying 1 file to C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build 
Nothing to copy. 
Building jar: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\dist\DirectoryList_1.jar 
To run this application from the command line without Ant, try: 
java -jar "C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\dist\DirectoryList_1.jar" 
jar: 
BUILD SUCCESSFUL (total time: 0 seconds) 

非常亂,也許應該更好地組織代碼:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package directorylist; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.PrintWriter; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* This program lists the files in a directory specified by the user. The user 
* is asked to type in a directory name. If the name entered by the user is not 
* a directory, a message is printed and the program ends. 
*/ 
public class DirectoryList { 

    public static void main(String[] args) { 
     try { 
//   String directoryName;    // Directory name entered by the user. 
//   Scanner scanner;     // scanner reads from standard input. 
//   scanner = new Scanner(System.in); // scanner reads from standard input. 
// 
//   System.out.print("Enter a directory name: "); 
//   directoryName = scanner.nextLine().trim(); 

      String indent = ""; 
      String userHome = System.getProperty("user.home"); 
      String fileSeparator = System.getProperty("file.separator"); 

      File log = new File(userHome + fileSeparator + "log.txt"); 
      FileOutputStream stream; 
      stream = new FileOutputStream(log); 
      PrintWriter out = new PrintWriter(stream); 

//   System.out.println(userHome); 
//   System.out.println(fileSeparator); 

      for (int i = 0; i < userHome.length(); i++) { 
       if (userHome.charAt(i) == fileSeparator.charAt(0)) { 
        userHome = userHome.substring(0, i + 1); 
       } 
      } 

      System.out.println("Files in directory \"" + userHome + "\""); 
      out.println("Files in directory \"" + userHome + "\""); 
      directoryListV2(userHome, 1, out); 
     } // end main() 
     catch (FileNotFoundException ex) { 
      Logger.getLogger(DirectoryList.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } // end main() 

    public static void directoryListV2(String directoryName, int directoryLevel, PrintWriter out) { 

     File directory;  // File object referring to the directory. 
     String[] files;  // Array of file names in the directory. 
     directory = new File(directoryName); 
     String fileSeparator = System.getProperty("file.separator"); 
     String indent = ""; 

     for (int i = 0; i < directoryLevel; i++) { 
      indent += " "; 
     } 

     if (directory.isDirectory() == false) { 
      if (directory.exists() == false) { 
       System.out.println("There is no such directory!"); 
       out.println("There is no such directory!"); 
      } else { 
       System.out.println("That file is not a directory."); 
       out.println("That file is not a directory."); 
      } 
     } else { 
      files = directory.list(); 
//   System.out.println("Files in directory \"" + directory + "\":"); 
//   out.println("Files in directory \"" + directory + "\":"); 

      if (files != null) { 
       for (int i = 0; i < files.length; i++) { 
        if (isDirectory(directoryName + fileSeparator + files[i] + fileSeparator)) { 
         System.out.println(indent + directoryName + fileSeparator + files[i] + fileSeparator); 
         out.println(indent + directoryName + fileSeparator + files[i] + fileSeparator); 

         directoryListV2(directoryName + fileSeparator + files[i], directoryLevel + 1, out); 
        } else { 
         System.out.println(indent + files[i]); 
         out.println(indent + files[i]); 
        } 
       } 
      } 
     } 
     out.flush(); 
    } 

    private static boolean isDirectory(String directoryName) { 
     File directory; 

     directory = new File(directoryName); 
     if (directory.isDirectory() == true) { 
      if (directory.exists() == true) { 
       return true; 
      } 
     } 
     return false; 
    } 
} // end class DirectoryList 
+0

可能是清單中的主類聲明。 –

+0

已經有人問這裏http://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class – Juliano

+0

你是怎麼試着運行jar的? MANIFEST.MF是什麼樣的? – Thomas

回答

0

的問題是,在你的jar的主類沒有加載。

爲此,您將不得不爲jar中的Main Class製作一個清單條目。

你可以找到最好的教程創建可執行的JAR here

其他資源也可以發現here這是Sun公司創建可執行罐子主要的指導方針。

希望這會爲你工作。

享受!

+0

啊,你說的話聽起來很合乎邏輯,但我仍然因爲這個問題而受到瘟疫!我已經嘗試了幾件事情: 將主要類:directorylist.DirectoryList添加到清單 使用完整路徑名代替 但發生錯誤! – Hobbyist

+0

然後你的編碼可能有問題。 –

+0

但在IDE中按預期運行,這是可能的嗎? – Hobbyist

相關問題