1

Can't seem to get my head around what is causing this error. I have set the CLASSPATH in Environment Variables to C:\Program Files\Java\jdk-10.0.2\bin.

I can compile the code into a .class file using javac HelloWorld.java. However when trying to run the .class file using java HelloWorld, I am getting the below error:

I am running the code from C:\Java which is the directory of both my .java and .class file.

 c:\Java>java HelloWorld
 Error: Could not find or load main class HelloWorld
 Caused by: java.lang.ClassNotFoundException: HelloWorld

CODE:

public class HelloWorld {

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

Any tips would be greatly appreciated.

LockTheTaskBar
  • 99
  • 4
  • 13

2 Answers2

2

if your classpath,

CLASSPATH=C:\Program Files\Java\jdk-10.0.2\bin

Your class loader will look .class files from there,

include your current directory into your CLASSPATH, where as in your case your .class files are at C:\Java, so the java could not find your .class file, try this one

CLASSPATH=C:\Java

CLASSPATH variable is where java looks for .class and jar file paths

PATH and CLASSPATH

The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
2

The CLASSPATH environment variable is not supposed to point the location of your java installation (you don't really need any environment variable to point at that. A few outdated tools MIGHT need you to set JAVA_HOME, but not to the 'bin' dir but to its parent).

It is supposed to point to the location(s) of your class files.

If your HelloWorld.class file has no package declaration and is located at C:\java\HelloWorld.class, then C:\java needs to be your classpath.

You can use CLASSPATH for this, but... don't. You can have multiple projects on a machine so the notion of 'one machine, one classpath' is silly. Use command line params:

java -cp c:\java HelloWorld

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72