Search code examples
javacloud9-idecloud9

Error class not Found fixed but I don't understand why


The file "HelloDemo.java" path is "/test/hello/HelloDemo.java"

package test.hello;

public class HelloDemo {
  public static void main(String[] args) {
      System.out.print("Hello!!");
  }
}

when I "run" it, an error occurred.

Building HelloDemo.java and running HelloDemo
Error: Could not find or load main class HelloDemo

Then, I changed the code.

//package test.hello;

public class HelloDemo {
  public static void main(String[] args) {
      System.out.print("Hello!!");
  }
}

when I "Run" it, code success output.

Building HelloDemo.java and running HelloDemo
Hello!!

This is the screenshot about the "Run". I fixed an error, but I don't konw why, I need help, Thank you!

If I want to keep the package uncomment, How to fix it?


Solution

  • That's because you probably changed the location of your file after running it once already. Hence, the running configuration should change to look for the new test.hello.HelloDemo class inside the built jar and not for HelloDemo anymore (which was probably in the default package, initially). What is your IDE?

    Remark: This is not because you changed the location of your file that the classpath changed, and vice-versa.

    On IntelliJ, you should do this: https://www.jetbrains.com/help/idea/creating-and-editing-run-debug-configurations.html