Search code examples
javawindowscmdcompiler-errorsjavac

Compiling Java from CMD/Powershell fails


I have installed JDK already on my Windows10

Now, I am trying to run the code from cmd or PowerShell.

I have created two java files, respectively TestClass.java, and Test1.java

both of them have the same code inside:

public class TestClass{
    public static void main(String[] args){
        System.out.println("Hello");
     }
}

When I navigate to the containing folder and run javac TestClass.java; it compiles; when I run `java TestClass, I got error message saying "Error: Could not find or load the main class TestClass.class"

Can anyone help me point out where the problem maybe?


Solution

  • I got the problem fixed.

    a. (after removed anything Java related software (from Oracle) but one STL Java version) b. I went ahead and re-map the path to Java folder in a proper way (both JAVA_HOME and under environment path) c. rebooted the computer. (in my case it didn't work out the first time, but it may work for you at once, or who knows even without rebooting)

    then when I ran <java -version> and <javac -version>, it worked fine.

    Thank you for everyone who's kindly provided some tips how to resolve the issue!