这个jar构建中缺少什么?

时间:2012-05-10 16:44:04

标签: java netbeans-7

好吧,这就是令我困惑的事情:

我一直在使用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

1 个答案:

答案 0 :(得分:0)

问题是你jar中的主类没有被加载。

为此,你必须为jar中的Main Class制作一个清单。

您可以找到有关创建可执行jar here

的最佳教程

其他资源也可以找到here,这是Sun创建可执行jar的主要指南。

希望这对你有用。

享受!!