Error: Can not find or load the main class (Command Prompt). How the package name affects

advertisements

My code like this

//package com.web_tomorrow;

public class RunThis1 {

    public static void main(String[] args) {
        System.out.println("Run ....RunThis1");

    }
}

When I run javac command with "package com.web_tomorrow" package name commented or not commented in both cases it can compile well. But When I run java command with "package com.web_tomorrow" package name, I mean not commented, it shows the Error: Could not find or load main class RunThis1 And if I comment/ remove the package name and then run the java command it executes.

Could anyone plz explain why java cant find RunThis1.class file while javac can find RunThis1.java from the same folder which is web_tomorrow ?

C:\JavaPractices\CPTest\src\com\web_tomorrow>java RunThis1 Error: Could not find or load main class RunThis1

C:\JavaPractices\CPTest\src\com\web_tomorrow>javac RunThis1.java

C:\JavaPractices\CPTest\src\com\web_tomorrow>java RunThis1 Run ....RunThis1

How java command is related to package name ? How package name is affecting java to find main class ?

My Envr variable classpath value is .


Java packages are like directory. So if you have your class file MyClass.java in package like com.myfirm.pack then this should be in src directory like:

src
   com
      myfirm
          pack
              MyClass.java

if you wish to compile your java code (assuming you are at src folder) you need to do the following:

c:\src> javac com\myfirm\pack\MyClass.java

While if you want to run it, you need to do:

c:\src> java -cp . com.myfirm.pack.MyClass