0

Unable to execute this code from windows command line

  1. I wrote the code as :
package sample;

public class PrivateMain{
    public static void main(String[] args){
        System.out.println("This is from  main");
    }

}
  1. I have set the classpath using below commands, but still receiving error as "Could not find or load main class". Am i missing any steps?
G:\javaPract>javac PrivateMain.java  

G:\javaPract>java PrivateMain      
Error: Could not find or load main class PrivateMain

G:\javaPract>java sample.PrivateMain 
Error: Could not find or load main class sample.PrivateMain

G:\javaPract>set CLASSPATH=%CLASSPATH%;.

G:\javaPract>echo %CLASSPATH%
G:\javaPract;.

G:\javaPract>javac PrivateMain.java 

G:\javaPract>java PrivateMain      
Error: Could not find or load main class PrivateMain

G:\javaPract>java sample.PrivateMain 
Error: Could not find or load main class sample.PrivateMain

G:\javaPract>
  1. file contents on G:\javaPract directory:
G:\javaPract>dir
 Volume in drive G is Learning
 Volume Serial Number is 008F-C257

 Directory of G:\javaPract

03/21/2023  08:04 AM    <DIR>          .
03/21/2023  08:04 AM    <DIR>          ..
05/17/2023  04:35 PM               280 PrivateMain.java
05/17/2023  04:36 PM               688 PrivateMain.class
sampath
  • 33
  • 8
  • 1
    Where is your `PrivateMain.class` file located and why is your `PrivateMain.java` file not located inside a directory `sample/`? – Progman May 17 '23 at 21:40
  • `javac -d . PrivateMain.java;java sample.PrivateMain` – g00se May 17 '23 at 21:53

1 Answers1

2

java PrivateMain does not work

That's because your class isn't called that. It's called sample.PrivateMain. If you use java in a 'I shall give you the name of my class' mode (i.e. not via the -jar or -jmod launch options), it has to be fully qualified.

java sample.PrivateMain does not work

class names are turned into paths by turning dots into slashes and appending .class. In other words, if you run java sample.PrivateMain, java starts, and checks every dir in the classpath by appending sample/PrivateMain.class to it. This doesn't find your class because your class isn't in sample/PrivateMain - it's in ./PrivateMain which isn't where java is going to look at all.

How to fix it

For throwaway projects like this, get rid of the package declaration. While you're at it, you can just run java MyMain.java - no need to involve javac. Once you graduate to more serious projects with multiple source files, and especially, multiple packages, ditch this command line javac and java stuff and use a build system such as maven or gradle.

If you must, for some strange reason, use a package statement, set up your project like so:

cd G:\javaPract
mkdir sample
cd sample
# put your PrivateMain.java file here
javac PrivateMain.java
cd ..
java -cp . sample.PrivateMain

key point is, file has to be in G:\javaPract\sample\PrivateMain.class, and G:\javaPract (not G:\javaPract\sample!) has to be on the classpath. Then run java sample.PrivateMain.

Generally, do not use CLASSPATH, just specify with the -cp parameter. CLASSPATH is a global variable and as a general rule it's bizarre to limit your computer to only run one java application ever.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72