Why don't we use the .class extension with "java" command?

java

Solution

Because you are not describing a file to run. You are telling Java which class contains the main method - and the class' name is (in your case) `filename`, not `filename.class`.

The fact that the bytecode is almost always contained in files on the filesystem is an implementation detail. The classpath you pass to the `java` command tells it where to look for classes, and then the main class argument tells it which class to use.

(It's different for `javac`, because this program specifically does take source files and compiles them into bytecode.)

Problem

Why don't we give the `filename.class` file after `java` command, instead of only `filename`? Suppose we want to compile the `test.java` program, then we run `javac test.java`. It is ok! After that it will produce `test.class` file but to run the program we run `java test` instead of `java test.class`. What is the reason for this?

Original source