0

Why does this work in executing my code:

java -cp . Main

but not this:

java Main

I have always used just java <classname> for executing my code, however almost randomly, this command just provides the error message Error: Could not find or load main class Main.

Here's my Java file (Main.java):

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

Here's the output from my Command Prompt including file names

  • Isn't that the usual way to execute class files that are in the same directory? I have always used that method and it has weirdly never given me a problem before. Is there some specific reason as to why `java Main` would not work? – Justin Vos Sep 27 '16 at 13:17
  • I just tried the java Main in command line and it worked well. I am not sure why it didn't work with you. – Wael Sep 27 '16 at 13:20
  • 4
    Check your `CLASSPATH` environment variable. If that's specified, then it's used; and if it doesn't contain `.`, then you have to explicitly specify on the command line. – Marko Topolnik Sep 27 '16 at 13:21
  • I've just checked for the `CLASSPATH` environment variable, and I didn't already have it included. However, it results in the same `Error: Could not find or load main class Main` with just `java Main`. Thanks for the help anyway :) – Justin Vos Sep 27 '16 at 13:28
  • Did you change the value of CLASSPATH? What is it now? – RealSkeptic Sep 27 '16 at 13:30
  • The full text is now `C:\libraries\junit-4.12.jar;C:\libraries\hamcrest-core-1.3.jar;.` I tried it also with an extra `;` at the end, stil the same output. – Justin Vos Sep 27 '16 at 13:32

0 Answers0