Search code examples
javacommand-lineprogram-entry-pointclassnotfoundexception

Getting ClassNotFoundException from the same folder where class file is present


I have written a very simple java program and compiled using java 11. Here is the code :

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

}

It compiles fine from Windows command line (javac Simple.java) but when I try to run using java Simple (from same folder) I see this error:-

C:\work_related>java Simple
Error: Could not find or load main class Simple
Caused by: java.lang.ClassNotFoundException: Simple

I tried setting path to current folder as well by doing set path=%path%;. but doesn't make any difference.

Thanks in advance.


Solution

  • Setting classpath env variable did the trick as mentioned by g00se in comments. As g00se has suggested it is better to refer to classpath from command line while running the application as:

    java -cp . Simple