0

I set all the paths correctly through 'environment variables' and my code is fine. It does compile my file, but it cannot run it. I'm a mathematician and normally run java files at uni, but this time I downloaded jdk on my window computer to work from home. Please could anyone help me?

paths in environment variable C:\Program Files\Java\jdk1.8.0_231\bin

CLASSPATH in environment variable C:\Program Files\Java\jdk1.8.0_231\lib

Both java and javac versions look fine.

PS C:\Users\Administrator\Desktop\Algo> java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
PS C:\Users\Administrator\Desktop\Algo> javac -version
javac 1.8.0_231
PS C:\Users\Administrator\Desktop\Algo> javac HowAreYou.java
PS C:\Users\Administrator\Desktop\Algo> java HowAreYou
Error: Could not find or load main class HowAreYou

The following is my java code:

class HowAreYou {

    public static void main (String[] args) {
        System.out.print("Hello ");
        System.out.print(args[0]);
        System.out.println(", how are you today?");
    }

}
Progman
  • 13,123
  • 4
  • 28
  • 43
  • 1
    Delete the CLASSPATH environment variable. Use `java -cp . HowAreYou` (but `.` is the default if you stop defining CLASSPATH, so once you've deleted this env variable, then java HowAreYou should work) – JB Nizet Dec 17 '19 at 21:03
  • Thank you very much for your comment. However, it does not seem to work either. It gives this message: PS C:\Users\Administrator\Desktop\Algo> javac HowAreYou.java PS C:\Users\Administrator\Desktop\Algo> java HowAreYou Hello Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at HowAreYou.main(HowAreYou.java:5) –  Dec 17 '19 at 21:29
  • @Piemel The `ArrayIndexOutOfBoundsException` is a totally different problem, which is explained on https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it. In this specific case you have to provide an argument for the `java` call, like `java HowAreYou SomeNameHere`. – Progman Dec 17 '19 at 21:40
  • Of course, there must be an argument. Thank you again! –  Dec 17 '19 at 21:53
  • In the meantime, would it be possible to know why you told me to delete CLASSPATH? What's the logic behind it? –  Dec 17 '19 at 21:56

1 Answers1

0

You can do any of the following to make it work:

  1. Remove the CLASSPATH from the environment variables > Close the current cmd window > Open a new cmd window > Run javac HowAreYou.java > Run java HowAreYou Piemel
  2. Run set CLASSPATH=%CLASSPATH%;. > Run javac HowAreYou.java > Run java HowAreYou Piemel
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • Thank you for your reply! The first method gives the following error: Hello Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 whereas the second method gives PS C:\Users\Administrator\Desktop\Algo> set CLASSPATH=%CLASSPATH%;. At line:1 char:27 + set CLASSPATH=%CLASSPATH%;. + ~ Missing expression after '.' in pipeline element. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRec ordException + FullyQualifiedErrorId : MissingExpression –  Dec 17 '19 at 21:32
  • Thank you so much! –  Dec 17 '19 at 21:54
  • I've asked the same question above, but could you tell me why I should delete CLASSPATH? –  Dec 17 '19 at 21:56
  • thank you so much! –  Dec 17 '19 at 22:37