I am really curious about this question.
When we compile .java file using javac ie. javac JavaFileName.java, a JavaFileName.class file gets created. But when we run this java program by using java command on command prompt, we write java JavaFileName.
My question is which file is passed to java command when we run java program through command prompt?
If it uses .class file then why it gives error on java JavaFileName.class
It’s not a file that you’re nominating. Instead you’re nominating a class, and Java will use the entries in the
classpathto find this.Your
classpathwill contain directories or .jar/.zips, and Java will search these in order.Obviously for a directory, there’s a mapping between the class nominated and the file. That applies too for the .jar/.zip. Java will look for the corresponding entry.
e.g.
will result in a search for the file or .jar entry
a/b/c/D.class