-2

Any java code I compile and try to execute from CMD line results in the error:
Error: Could not find or load main class

I have tried setting environment variables:
PATH=C:\Program Files\Java\jdk1.8.0_202\bin
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_202

and with and with out:
CLASSPATH=C:\Program Files\Java\jdk1.8.0_202\jre\lib or
CLASSPATH=C:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar or
CLASSPATH=(directory of code)

The following is an example of a simple test prog that will not run:

/**
 * Experiment with println() and print()
 */
public class HelloGoodbye 
{   
    public static void main(String[] args) 
     { 
        System.out.println("My First Hello!");
        System.out.println("Hello again!");
        System.out.print("Hello finally!");
        System.out.print("Goodbye!");
    }
}

This produces: Error: Could not find or load main class

1 Answers1

0

First you have to compile your code:

javac HelloGoodbye

A class file will be created (HelloGoodbye.class). Then, you have to run the following command for running the program:

java HelloGoodbye
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Eduardo
  • 319
  • 2
  • 14