-2
import java.io.*;

public class WriteFile {
    public static void main(String[] args){
        try {
            FileWriter writer = new FileWriter("Test.txt");
            writer.write("this is a plain text file.\n");
            writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

错误: 找不到或无法加载主类 O:WriteFile 原因: java.lang.ClassNotFoundException: O:WriteFile

This code can run on both eclipse and vs Code but not on Coderunner. How to solve it.

  • If you are referring `coderunner` as a mac software then you can run your code in coderunner without saving by simply removing `public` keyword before the class name. – Aayush Shah Jun 28 '21 at 20:18

1 Answers1

0

Your classpath is broken.

Method #1

Try adding the classpath while running it. On windows:

java -classpath .;yourjar.jar YourMainFile

On Unix:

java -classpath .:yourjar.jar YourMainFile

Method#2

Configure the build path in your IDE and add an external JAR containing your class to the build path.

Method#3

Please read this to resolve the issue.

Note: You could also refer to this.

Madhuv Sharma
  • 21
  • 1
  • 1
  • 6
  • Thank you for your patient response. I fixed this bug by adding a compile flag "." which represents the current path. I used to not know I have to do this because it seemed that Eclipse and VSCode helped me finish this automatically. – YIMING ZHAO Jun 20 '21 at 08:52