theasianmamba24 theasianmamba24 - 1 month ago 6
Java Question

"Could not find or load main class [Class name]"

I pasted this code from a tutorial site and compiled the code in CMD as well as attempting to run the code:

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


Since the PATH variable already existed, I added a path to the 'bin' file within the java file. I also added a new variable (CLASSPATH) and set the variable value to where I keep the
.class
file.

PS:


  • The
    .java
    and the
    .class
    file are in the same folder

  • No spelling or capitalization errors are made

  • I did not add
    .class
    at the end where I try to run the code


Answer

Assuming that you have you java path set up done coorectly, run the following commands in order : JavaProg is the name of the java file and your public class.

1.Suppose my .java file is in Desktop, then run the following command

cd Desktop

2. Compile it

javac JavaProg.java

3. Run it

java JavaProg