我正在編寫一個文本編輯器項目,現在正在編寫我的程序課程,而且我遇到了一個我從未見過的錯誤,當我嘗試運行它時。這是一個相當長的解釋,但基本上,我使用一個編輯器類,使用其他幾個類創建一個鏈表,存儲一個文本文件,然後允許修改文件。我應該在linux環境中運行它,並且該文件應該作爲'命令行'參數輸入。不過,我嘗試運行它每一次,我碰到下面的錯誤ClassNotFoundException問題; linux相關問題(學校項目)
Exception in thread "main" java.lang.NoClassDefFoundError: myEditor
Caused by: java.lang.ClassNotFoundException: myEditor
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: myEditor. Program will exit.
至於節目本身:
import java.util.Scanner;
import java.util.Iterator;
import java.io.*;
public class myEditor {
public static void saveToFile(String text, String filename) throws IOException{
PrintWriter out = new PrintWriter(new File(filename));
out.println(text);
out.close();
}
public static void main(String args[]) {
boolean quit = false;
try {
if(args.length!=1) {
throw new IllegalArgumentException();
}
String filename = args[0];
Scanner input = new Scanner(new File(filename));
//Add exception
UnorderedList<String> list = new UnorderedList<String>();
while(input.hasNextLine()) {
if(list.head==null) {
list.addToFront(input.nextLine());
}
list.addToRear(input.nextLine());
}
System.out.println(">");
do {
Scanner command = new Scanner(System.in);
String comm = command.next();
String[] comm1 = comm.split(" ");
if(comm1[0].equalsIgnoreCase("I")&&comm1[1].equals("")) {
System.out.println("Type a line of text >");
comm = command.next();
list.addToRear(comm);
}
else if(comm1[0].equalsIgnoreCase("I")&&!comm1[1].equals("")) {
int linNum = Integer.parseInt(comm1[1]);
Iterator<String> itr = list.iterator();
String current = "";
for(int count=0;count<linNum;count++) {
current = itr.next();
}
list.addAfter(comm, current);
}
else if(comm1[0].equalsIgnoreCase("D")&&!comm1[1].equals("")) {
int linNum = Integer.parseInt(comm1[1]);
if(linNum<=list.count&&linNum>0) {
Iterator<String> itr = list.iterator();
String current = "";
for(int count=0;count<linNum;count++) {
current = itr.next();
}
list.remove(current);
}
}
else if(comm1[0].equalsIgnoreCase("L")) {
list.toString();
}
else if(comm1[0].equalsIgnoreCase("E")&&!comm1[1].equals("")) {
saveToFile(list.toString(), filename);
quit = true;
break;
}
}
while(!quit);
}
catch(IllegalArgumentException e) {
System.err.print(e.getMessage());
}
catch(FileNotFoundException e) {
System.err.print(e.getMessage());
}
catch(IOException e) {
System.err.print(e.getMessage());
}
}
}
顯然,有我這個人用其他類的負載,但它在我看來,錯誤並不在於他們。有沒有人有這種錯誤的經驗?
編輯:我幾乎忘記提及,通過命令行參數,我的意思是這個文件應該與之合作應該已經放在它放在它的linux目錄中了。它顯然應該佔用參數[0]
你在什麼樣的系統?你如何嘗試運行這個課程?您熟悉系統上的「終端窗口」嗎? – arcy