2

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.

2
  • Is the class file located in the folder where you try to run it from (c:\Java)? Commented Sep 18, 2018 at 11:59
  • Set your classpath where is your .class file is present. Commented Sep 18, 2018 at 12:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, I was getting confused with the PATH and CLASSPATH variables, going to use parameters as advised, thanks
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

5 Comments

do we need the JDK path in there?
no we don't, i thought may he needs it.
well, no .class (or .jar) there anyway.,..
Thanks for this, I thought I had read somewhere the class path needs to be set to the JDK directory so it knows how to launch "javac" etc
ok, path and classpath are different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.