1

I'm having issues running java on the command line. I have checked lots of other questions and none have solved my problem.

I am using Windoze 10 x64.

Installed at C:\Program Files\Java, I have the following files:

jdk1.8.0_121 and jre1.8.0_121.

I already set the JAVA_HOME, PATH and CLASSPATH as follows:

  • JAVA_HOME as C:\Program Files\Java\jdk1.8.0_121

  • PATH as %JAVA_HOME%/bin

  • CLASSPATH as %JAVA_HOME%/lib

This is the current configuration of the system variables. I tried lots of variations for the configs but none seem to help. Such as adding to the PATH %JAVA_HOME%\jre1.8.0_121\bin as well. Nothing seems to help.

If I run java -version and javac -version they will go through, showing the current version installed.

I can also compile, for example: javac HelloWorld.java succesfully.

When I try java HelloWorld, I get Could not find or load main class HelloWorld.

Here is the HelloWorld.java file:

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

I spent +2 hr trying to figure this out. I'm sure it's a rookie mistake but honestly, I'm just trying to set this up so I can start learning Java.

I can run java using IntelliJ IDEA Community Edition's console but not directly on CMD.

I would really appreciate if you could walk me through like LI5, I really looked for the solution, watched videos on how to set the system variables but it yield nothing.

If you need any more information I'm happy to provide it, thank you.


EDIT: Okay, the issue was solved. Thanks to @azurefrog and @mirmdasif for helping me figure it out.

As you can read Hereand below this on azurefrog's comments and mirmdasif's reply, you need to add the current working directory to the CLASSPATH.

Add the current working directory to the CLASSPATH by adding a .(dot) to the CLASSPATH.

So CLASSPATH should look like this instead: .;%JAVA_HOME%/lib (Notice the . followed by the semi-colon ;.

10110
  • 2,353
  • 1
  • 21
  • 37
  • Can you share your HelloWorld.java file – mirmdasif Feb 15 '17 at 05:04
  • Sure, sorry for not adding it. Syntax seems good to me, though... Adding it right now. – 10110 Feb 15 '17 at 05:05
  • That's the entirety of your HelloWorld.java? No package statement? – azurefrog Feb 15 '17 at 05:11
  • I tried adding a package statement, it didn't work. If you provide me with a better code sample to test, I will be happy to try it. As I said, I am trying to set this up in order to start learning and practicing, so I have little to no idea what the package statement is for. Thank you! – 10110 Feb 15 '17 at 05:14
  • The second thing that pops into my head is that you don't have `.` (the current working directory) on your classpath. Try running `java -classpath . HelloWorld` – azurefrog Feb 15 '17 at 05:15
  • 2
    If you take a look at [How should I set CLASSPATH](http://stackoverflow.com/questions/2017833/how-should-i-set-classpath), you'll notice it's got `.` prepended to the lib entry, which I don't see in yours. – azurefrog Feb 15 '17 at 05:18
  • @azurefrog `java -classpath . HelloWorld` Works! I will try adding the `.` to the CLASSPATH! – 10110 Feb 15 '17 at 05:41
  • Thank you, azurefrog and mirmdasif for being so patient. I really appreciate your help. The issue has been solved. – 10110 Feb 15 '17 at 06:13

3 Answers3

1

The problem is with your CLASS_PATH variable.

The preferred way to specify the classpath is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications

Default value for ClassPath variable is '.' which means only the current directory is searched. If you specify the classpath variable the default will be overridden.

If want to set classpath as environment variable you may do as running the following cmd in cmd prompt(remember the dot in the beginning)

C:> set CLASSPATH=.;C:\Program Files\Java\jdk1.8.0_121\lib\*

For further information http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • The command would not solve the issue but I set the classpath manually to `.;C:\Program Files\Java\jdk1.8.0_121\lib\*` and it does the job! Thank you ! – 10110 Feb 15 '17 at 06:11
  • Actually I am doing the same just from the command prompt – mirmdasif Feb 15 '17 at 06:13
  • I tried it from the console but it would not change it. Maybe the System Variable window was open so it could not access it or something like that. Thanks again for your help! – 10110 Feb 15 '17 at 06:16
0

Sometimes it happens. You should try to find java.exe in C:\Windows\System32, if it there you need delete it.

Example: Java Windows7 System32 folder java.exe

Community
  • 1
  • 1
i.merkurev
  • 465
  • 2
  • 8
  • Theres no java.exe in System32! :( But `java -classpath . HelloWorld` works. Seems Im misconfigured the `CLASSPATH` – 10110 Feb 15 '17 at 05:39
0

Just keep updating your Enviroment Variables in classpath whenever you get: Error: Could not find or load main class XXX

Then browse to C:\Program Files\Java\jre1.8.0_181