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

Why don't we give filename.class after the java command instead of filename ?

Suppose we want to compile test.java , then we run javac test.java . This is normal!

After that, he will create a test.class file, but to run the program we run java test instead of java test.class . What is the reason for this?

+7
java
source share
6 answers

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

The fact that bytecode is almost always contained in files in the file system is an implementation detail. The path to the class that you pass to the java command indicates where to look for classes, and then the main class argument indicates which class to use.

(It is different for javac because this program specifically takes the source files and compiles them into bytecode.)

+20
source share

You also do not pass the file name to the java command. You pass it the full class name. Something like com.yourcompany.yourapp.Main . Then Java finds the .class file for that class name, looking through all the directories and jar files in the classpath.

+6
source share

This is an implementation detail. The Java classloader system can be expanded with custom code to make it behave differently. For example, some companies write encrypted class loaders that can decrypt and download encrypted class files on the fly. You could hypothetically create a similar system that combines a bunch of classes into something similar to a .NET assembly, rather than a Jar file (which is actually just a zip file).

+1
source share

when you execute " java test.class "

You get either

Could not find or load the main class test.class

or

Exception in thread "main" java.lang.NoClassDefFoundError: test / class

This is because java in java test.class is jvm . He is looking for the main method in a class called " class " instead of " test ". The point in " java test.class " is significant. So, as jvm looks at " java test.class ", in a package called " test " it looks for a java class called " class ".

 *test.class* *test* - package name *class* - java filename 

Hope this helps!

0
source share

Just summarize everything to the smallest detail,

when someone does

 java filename.java 

He / she actually runs the Java compiler and converts the code into instructions that the JVM understands.

Now that one is running

 javac main_file 

he / she calls the JVM to run the entire project, whose main method is in the main_file class

This main_file is actually a fully qualified class name, i.e. if I have a ProjectX project and this main class is in the src.java.hello.main package,
You must run the command

 java src.java.hello.main.main_file 

Hence it . is actually a reserved thing at the end of the JVM, and we cannot provide .class as part of the java command argument.

0
source share

The Javac compiler creates a file called Xyz.class (here Xyz is FileName) that contains the byte version of the Java Bytecode program - it is nothing more than an intermediate representation of your program that contains the instruction that the Java interpreter will execute.
thus javac output is not code that can be executed directly

In short, the javac keyword is used to compile a Java program; if we use .class with javac (an already compiled .class File), then how to compile an already compiled file

so the valid syntax is: Javac Xyz.java (compile Java program) java Xyz (Run Java program)

-one
source share

All Articles