Java run from command line — Error: Could not find or load main class / Error: cannot find symbol
1 min readApr 7, 2024
If you would like to test out the simple Java program that span over multiple file.
Hello.java
MyClass.java
MyAnotherClass.java
You might see the problem like this
Error: Could not find or load main class Hello.java
Caused by: java.lang.ClassNotFoundException: Hello.java
Or if you can run it might throw
error: cannot find symbol
symbol: class MyClass
location: class Hello
Why single file run is working ?
because it compile Hello.java
and store Hello.class
in memory then run.
public class Hello {
public static void main (String args[]) {
System.out.println("hello");
}
}
java Hello.java
// hello
Solution : Multiple file need compilation
The problem is the java
does not know what to compile so you need to specify by yourself
I compile everything.
javac *.java
Then you can see the class
file
Hello.java
Hello.class
MyClass.java
MyClass.class
MyAnotherClass.java
MyAnotherClass.class
Then you run where your public static void main
is
java Hello.java
// Or just
java Hello
Another way is using Java package and run it with MVN or other tools but it will well suit for larger project not for some quick script or setup like this.
Hope this help and see you next time !