好吧,这让我感到困惑:
我一直在使用 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