命令行错误:无法找到或加载主类Part1

时间:2014-09-08 21:33:10

标签: java

尝试通过Powershell运行java程序。我转到我的目录并使用javac Part1.java,然后运行我使用java Part1的类,但它给了我这个错误Error: Could not find or load main class Part1这是Part1.java中的代码,它带有一个基本的.in文本文件,并在.in文档中的每一行做一些很酷的东西:

package comp2402a1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.*;

public class Part1 {

    /**
     * Your code goes here - see Part0 for an example
     * @param r the reader to read from
     * @param w the writer to write to
     * @throws IOException
     */
    public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
        // Your code goes here - see Part0 for an example
        SortedSet<String> s = new TreeSet<String>();

        for (String line = r.readLine(); line != null; line = r.readLine()) {
            s.add(line);
        }

        for (String text : s) {
            w.println(text);
        }
    }

    /**
     * The driver.  Open a BufferedReader and a PrintWriter, either from System.in
     * and System.out or from filenames specified on the command line, then call doIt.
     * @param args
     */
    public static void main(String[] args) {
        try {
            BufferedReader r;
            PrintWriter w;
            if (args.length == 0) {
                r = new BufferedReader(new InputStreamReader(System.in));
                w = new PrintWriter(System.out);
            } else if (args.length == 1) {
                r = new BufferedReader(new FileReader(args[0]));
                w = new PrintWriter(System.out);                
            } else {
                r = new BufferedReader(new FileReader(args[0]));
                w = new PrintWriter(new FileWriter(args[1]));
            }
            long start = System.nanoTime();
            doIt(r, w);
            w.flush();
            long stop = System.nanoTime();
            System.out.println("Execution time: " + 10e-9 * (stop-start));
        } catch (IOException e) {
            System.err.println(e);
            System.exit(-1);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这是因为包comp2402a1。 Java期望Part1类文件位于 comp2402a1文件夹。

创建该文件夹并放置类文件(像$ java comp2402a1/Part1一样运行)或从源代码中删除包。