尝试通过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);
}
}
}
答案 0 :(得分:2)
这是因为包comp2402a1
。 Java期望Part1
类文件位于
comp2402a1
文件夹。
创建该文件夹并放置类文件(像$ java comp2402a1/Part1
一样运行)或从源代码中删除包。