简体   繁体   中英

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

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?

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.)

You don't pass a file name to the java command either. You pass it a fully qualified class name. Something like com.yourcompany.yourapp.Main . Java then finds the .class file for this class name, by looking into all the directories and jar files in the classpath.

It's an implementation detail. The Java class loader system can extended by custom code to make it behave differently. For example, some companies have written encrypted class loaders that are capable of decrypting and loading encrypted class files on the fly. You could hypothetically create a similar system that bundles a bunch of classes together into something resembling a .NET assembly instead of a Jar file (which is really just a zip file).

when you execute " java test.class "

You get either

Could not find or load main class test.class

or

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

This is because " java " in " java test.class " is jvm . It looks for main method in class called " class " instead of " test ". Dot in " java test.class " has a significant meaning. So how jvm looks at " java test.class ", in a package called " test " it looks for a java class named " class ".

*test.class*

*test* - package name

*class* - java filename

Hope this helps !!

Just to summarise everything to the details,

when one does

java filename.java

s/he actually runs the java compiler and converts the code to instructions that a JVM understands.

Now when one runs

javac main_file

s/he invokes the JVM to run the entire project whose main method is found in the class main_file

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

java src.java.hello.main.main_file

Hence this . is actually a reserved thing at JVM end and we cannot give .class as part of the argument to the java command.

The Javac compiler creates file called Xyz.class (Here Xyz is FileName) that contains the byte version of the program Java Bytecode is nothing but intermediate Representation of Your Program that contains instruction the Java interpreter will execute.
thus, the output of javac is not code that can be directly executed

in short, javac keyword is used for the Compile the Java program if we Use .class with javac (Already compiled File .class File) then How can compile already compile file

so Valid Syntax is: Javac Xyz.java (Compile the Java Program) java Xyz (Run the Java Program)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM