0

java1.8.0_261
F:\DMP\HelloWorld.java

package test;

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

F:\DMP>javac HelloWorld.java -d .

F:\DMP>java test.HelloWorld
Error: Could not find or load main class test.HelloWorld

this is the simplest java,but I can not find the reason

Venus
  • 1,184
  • 2
  • 13
  • 32
  • The HelloWorld class should be in a folder called test given your package declaration. Try it with this layout and it will work. – Paul Whelan Jul 08 '21 at 09:00
  • For the above to work the "HelloWorld.class" file must be in a directory called "test", AND you have to be in the directory >above< "test" – Stephen C Jul 08 '21 at 09:08
  • I've just tried it *exactly* as stated, and can't reproduce the problem. While it's certainly better to put the source files in the appropriate package structure, it's not *required* (so long as you specify all the source files you want to compile explicitly). – Jon Skeet Jul 08 '21 at 09:09
  • @StephenC: But that's exactly what the OP has done - with `javac HelloWorld.java -d .` a directory called `test` will be created, and they *are* running it still from the same directory (DMP). As I say, I've tried exactly the OP's steps, and they worked fine for me. – Jon Skeet Jul 08 '21 at 09:09
  • 1
    Hmmm. Then perhaps the OP has set CLASS_PATH to something that is incompatible with the file location. @Venus add `-cp .` to the `java` command line. That explicitly tell `java` to only look for stuff in / under the current directory. – Stephen C Jul 08 '21 at 09:12

1 Answers1

2

As @JonSkeet pointed out, and as I checked myself as well, your code should work fine.

Perhaps it is something wrong with your classpath; as indicated by @StephenC and @g00se in their comments, you can try running your program by explicitly including the current directory as your class path by providing the -cp flag:

F:\DMP>java -cp . test.HelloWorld

As an alternative, you can try a different approach and try defining your file structure according to your java package - it's always good practice to do so and it can help you avoid some mistakes.

I mean, if you placed your class in the test package:

package test;

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

Your folder structure can look like:

test/
  HelloWorld.java

Equivalently:

F:\DMP\test\HelloWorld.java

Then, compile your code like this:

F:\DMP>javac test\HelloWorld.java

You should be able to run your program like this:

F:\DMP>java test.HelloWorld
jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • "The problem is that your file structure has to be according to your java package." No it doesn't. The OP is specifying the Java file to compile *and* specifying `-d`, which means the class files should be automatically created in the right place. I've just tried the steps they've shown, and they worked fine. – Jon Skeet Jul 08 '21 at 09:08
  • Hi @JonSkeet, it is an honor. My bad, I totally agree with you, even though I edited the answer later, I misunderstood the use of the `-d` flag here. Thank you very much for pointed me my error. – jccampanero Jul 08 '21 at 09:24
  • 1
    Absolutely nothing wrong with any of that but for one invisible thing - the classpath. `java -cp . test.HelloWorld` should fix the problem. You need possibly to find out why/how the wrong one was set – g00se Jul 08 '21 at 09:42
  • Much better- thanks :) – Jon Skeet Jul 08 '21 at 09:46
  • So it works now after the `-cp` thing? – g00se Jul 08 '21 at 09:53
  • @g00se,yes. F:\DMP\test>javac -d . HelloWorld.java , F:\DMP\test>java -cp . test.HelloWorld works – Venus Jul 08 '21 at 09:59
  • Good. The answer needs to be changed. – g00se Jul 08 '21 at 10:03
  • @g00se , I can not edit the answer, it says: Suggested edit queue is full – Venus Jul 08 '21 at 10:09
  • @jccampanero thanks but >You should be able to run your program like this:< wouldn't work, so needs to be edited. The classpath thng is not a 'consideration' but crucial – g00se Jul 08 '21 at 10:28
  • @g00se I agree and disagree with you ;) I think it made sense according to the answer redaction, but I edited the answer again. Please, see the edit. – jccampanero Jul 08 '21 at 10:41
  • @jccampanero OK I'll leave it there but in closing, note that your `javac` command won't run. You need a backslash not a forward slash – g00se Jul 08 '21 at 10:52
  • Thank again @g00se. I actually tested the solution, and I use unix, not windows. This is why I used a forward slash. – jccampanero Jul 08 '21 at 11:01