0

I faced a rather strange problem while executing a java program from the command line on Windows. I used this command to create a source.class file from a source.java file.

javac source.java

After this, I tried to execute the source.class file using:

java source.class

This gave me the following error:

Error: Could not find or load main class source.class

However, when I do this:

java source

I get the output as desired. I am curious to know why it failed when I mentioned .class explicitly. My professor told me that the java command is associated with .class files. Thus I want to know why this happened. Thank you.

EDIT: I don't believe this is a duplicate. I have already read the linked question. I am asking why the java command cannot execute a file when I mention .class explicitly.

Debanik Dawn
  • 797
  • 5
  • 28
  • Possible duplicate of [How do I run a Java program from the command line on Windows?](https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows) – pvg Aug 17 '17 at 15:59
  • just a thought, probably in java's point of view, the argument you passed in is not a filename, it is the class name. That is why the error message is "cannot find the main class", if it treat the argument as filename and append .class itself, the message would probably be "cannot find source.class.class file". – Surely Aug 17 '17 at 16:05
  • @Surely what do you mean? the filename and the classname has to be the same. so either way, it's the same. – Debanik Dawn Aug 17 '17 at 16:10
  • just looking around to find some document related. You can test this answer and if it really works in the way, you will know what I meant previously: https://stackoverflow.com/a/2324915/4298881 -- I think the java program would load all the .class file in current directory itself, the argument you provided is only to look for the corresponding class implementation. – Surely Aug 17 '17 at 16:45
  • @Surely in reply to your previous comment, I think the appending of '.class' could be it. And thanks, I understand what you mean now. – Debanik Dawn Aug 17 '17 at 16:59
  • 1
    @Surely You kind of in a way answered my question. It's not actually the '.class' append that's the thing. The javac makes .class files for each class in the .java file. Hence when I explicitly mention source.class, no such class exists with the name 'source.class' hence I get the error. Thanks. – Debanik Dawn Aug 17 '17 at 17:02
  • @DebanikDawn nice to know it.. don't have java in the machine currently so cannot test it myself :( – Surely Aug 17 '17 at 17:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152184/discussion-between-debanik-dawn-and-surely). – Debanik Dawn Aug 17 '17 at 17:14

0 Answers0