正常にコンパイルされますが、コンソールから実行しようとすると ClassNotFoundException エラーが発生しました。しかし、Eclipse から実行すると、問題なく動作します。なんで?
「javac FileIO.java」を使用してコンパイルし、「java FileIO」を使用して実行します。
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileOutputStream;
public class FileIO {
public static void main(String[] args){
PrintWriter pw = null;
BufferedReader bfr = null;
String linea = null;
try{
bfr = new BufferedReader(new FileReader("Records"));
linea = bfr.readLine();
} catch(FileNotFoundException fnfex){
System.out.println("Check you have reading/writing access.");
} catch(IOException ioex){
ioex.printStackTrace();
}
try{
pw = new PrintWriter(new FileOutputStream("Copy Records"));
} catch(FileNotFoundException fnfex){
System.out.println("Check you have reading/writing access.");
}
while(linea != null){
pw.println(linea);
try{
linea = bfr.readLine();
} catch(IOException ioex){
ioex.printStackTrace();
}
}
try{
bfr.close();
} catch(IOException ioex){
ioex.printStackTrace();
}
pw.close();
}
}`
完全なスタック トレース:
`Exception in thread "main" java.lang.NoClassDefFoundError: FileIO/java
Caused by: java.lang.ClassNotFoundException: FileIO.java
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: FileIO.java. Program will exit.
`