To compile Foo.java: javac Foo.java
To run the program : java Foo
Why compiler needs .java suffix but interpreter doesn’t need .class suffix?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As a couple of other answers have explained, the Java compiler takes a file name as an argument, whereas the interpreter takes a class name. So you give the
.javaextension to the compiler because it’s part of the file name, but you don’t give it to the interpreter because it’s not part of the class name.But then, you might wonder, why didn’t they just design the Java interpreter differently so that it would take a file name? The answer to that is that classes are not always loaded from
.classfiles. Sometimes they come from JAR archives, sometimes they come from the internet, sometimes they are constructed on the fly by a program, and so on. A class could come from any source that can provide the binary data needed to define it. Perhaps the same class could have different implementations from different sources, for example a program might try to load the most up-to-date version of some class from a URL, but would fall back to a local file if that failed. The designers of Java thought it best that when you’re trying to run a program, you don’t have to worry about having to track down the source that defines the class you’re running. You just give the fully qualified class name and let Java (or rather, itsClassLoaders) do the hard work of finding it.