IO程序不是从控制台运行,而是从Eclipse(Java)运行

Aik*_*áro 1 java console writing file

它编译得很好,但是当我尝试从控制台运行它时,我得到了一个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();
}
}`
Run Code Online (Sandbox Code Playgroud)

完成StackTrace:

`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.
Run Code Online (Sandbox Code Playgroud)

`

Jen*_*der 6

错误消息看起来像您正在调用

java FileIO.java
Run Code Online (Sandbox Code Playgroud)

代替

java FileIO
Run Code Online (Sandbox Code Playgroud)