who create class file JVM or compiler in java? - java

The compilation and execution of a Java program is a two-step process. During the compilation phase the Java compiler compiles the source code and generates bytecode. My question is who is responsible for .class file?`

A Java class file (containing bytecode) is produced by a Java compiler from Java programming language source files (.java files) containing Java classes. If a source file has more than one class, each class is compiled into a separate class file.
Source : https://en.wikipedia.org/wiki/Java_class_file

The class file contains the bytecode - it's created by the compiler, and executed by the JVM: https://en.wikipedia.org/wiki/Java_class_file

Related

What does the architecture mean from java class file?

I just built Tomcat7, and used file command to check the generated class files, it prints as below
output/classes/org/apache/coyote/AbstractProcessor$1.class: compiled Java class data, version 52.0 [64-bit architecture=65593] [architecture=6382440]
output/classes/org/apache/coyote/AbstractProcessor.class: compiled Java class data, version 52.0 [architecture=14878464] [architecture=56073]
What does the architecture means in the output? Why compiler generates differently for the classes from a same file?
And why did the compiler generate AbstractProcessor$1.class for https://github.com/apache/tomcat/blob/7.0.x/java/org/apache/coyote/AbstractProcessor.java? There is no anonymous inner class in source code.
It looks like this is actually a bug in the file command. Looking at the file cafebabe in the file github mirror, which handles detecting both Java class files and Mach-O (i.e. Mac) binaries (because they both use the same magic strings), it seems that the "architecture" part is meant to only be used for the latter, but somehow gets applied to your class files.
Feel free to ignore that.
Regarding the $1 file, looking at it with javap shows that it is a synthetic class and contains a method named $SwitchMap$org$apache$tomcat$util$log$UserDataHelper$Mode (sic), suggesting that it is generated by the compiler to help with the switch statement in the code:
$ javap AbstractProcessor\$1.class
Compiled from "AbstractProcessor.java"
class org.apache.coyote.AbstractProcessor$1 {
static final int[] $SwitchMap$org$apache$tomcat$util$log$UserDataHelper$Mode;
static {};
}

Java source file -> List of required classes for compilation

For a given Java source code file I want to list all the (fully qualified names of) classes that are (directly) required for compilation. In other words: All classes that are directly used by the code in the source code file, coming from imports, fully qualified names in the source code and other compile time means, but not by reflection or other runtime means.
Is there a way to "ask" the java compiler for this list? Are there other ways to get it?
PS: By "directly" I mean the following: If my source code file requires class A for compilation which uses class B, then class B has to be present to compile the code, but it is not a direct use.
You can try this:
It can be eaily done with just javac. Delete your class files and then compile the main class. javac will recursively compile any classes it needs (providing you don't hide package private classes/interfaces in strangely named files).

Does Java need .class files to run even if defined

I just started Java today and I am getting a NoClassDefFoundError when I run my code:
class Example {
public static final String greeting = "Hi there";
public static void main(String[] args) {
System.out.format("%s", Example.greeting);
}
}
Above is the contents of my example.java code. This code compiles fine in the Eclipse execution, but when I try to run example.java by itself, it fails. Eclipse also created some .class file when I ran this program, but that doesn't make much sense as to why it would fail if I ran the .java by itself in a different directory as all the information of the class Example is found in the .java file itself.
Does Java need the .class file to compile and run despite having the class itself defined in the program and why? I also built the .java file through terminal and it worked fine
Compilation and execution of a Java program is two step process:
During compilation phase Java compiler compiles the source code and generates bytecode. This intermediate bytecode is saved in form of a .class file.
In second phase, Java virtual machine (JVM) also called Java interpreter takes the .class as input and generates output by executing the bytecode.
So yes .class file is essential for execution of the code. .java file is text that you write, but not what the machine can interpret.
Here is the flow diagram of what happens:
To look at it in more depth:
When your Java project builds, it translates the source code (contained in *.java source files) to Java bytecode (most often contained in *.class files). This takes your high-level code one step closer to machine code, but not quite there yet. This bytecode is a collection of compact instructions; easier for a machine to interpret, but less readable.
When you run a Java application on your computer, cellphone, or any other Java-enabled platform, you essentially pass this Java bytecode to the Java Virtual Machine. The interpreter in the Java Virtual Machine usually starts compiling the entire bytecode at runtime, following the principles of so-called just-in-time compilation. This makes for the typical, albeit often slight delay when opening a Java application, but generally enhances the program performance compared to interpreted compilation.
Does Java need the .class file to compile and run despite having the class itself defined in the program and why
The compiled output of a java file (class) is .class file. It contains the bytecode of the program which is run by the JVM. So .class files are required to run the program.
I also built the .java file through terminal and it worked fine
Even through the terminal, when you compile the .java file (class). It creates the .class for each class declared in the .java file. Then you execute the class which contains the main method. The main method is the entry point for the JVM.
For example:
File name: example.java
Compile code: javac example.java
Output is .class file: Example.class. Because name of the class is Example.
Run the program: java Example
Error in your code. Looks like this has been updated in the question.
System.out.format("%s", Student.greeting);
Name of the class is Example not Student. it should be:
System.out.format("%s", Example.greeting);

What is the use of Byte code in java

I am a little bit confused...
I know that classes are loaded by the class loader only when they are needed,that is when we are trying to use static variables or when we are creating instance of that class.Thus if we have for e.g. 3 classes in our program and we are going to use only one,then only that particular class will be loaded and rest are not,but when we run the java compiler,it will create 3 .class files,I know these 3 .class files are byte code files,but then what is this byte code and what is the difference between loading a class and generating bytecode of a class?Where is the use of this byte code?If we are not going to use a particular class,then what is the need of generating a bytecode for that class?
Java is a compiled language. The purpose of compiling into bytecode is to allow the code to run on the JVM on any platform. Platform independence is a feature built into java.
Furthermore, you don't have to compile all three class files unless they have inter-dependencies. You can specify which specific files to compile in the console javac command. If you are using an IDE, check your settings or exclude the undesired class from the project.
Loading a class happens at runtime, when you're preparing to invoke whatever properties the class has.
Generating the bytecode of a class happens at compile time. This allows the code to be run on the virtual machine.
Java is a compiled language, and it runs on top of the Java Virtual Machine. Compiling bytecode translates whatever higher level code (be it Java, Scala, or Clojure) into machine-independent instructions to be read by the JVM. This is why that your (backend-specific) program will generally run without modification on Linux, Windows, and Mac OS X.
The Java language will compile any classes that have dependencies on each other within the path, so if you have a class but it is not used, chances are it will not be compiled. There may be tools that override that, so if you find yourself not using a class, then remove the class so that unnecessary bytecode is not generated.
Difference between languages like C++ and java is byte code. C++ binaries(compiled,assembled,linked) will have the machine(op) codes for the OS it got compiled for. In the case of java the byte code is the target for JVM. Byte code will have the opcodes for JVM. JVM in turn will initiate the respective os calls. So bytecode and JVM makes java programs independent of os.
Reg loading class loading,it happens when the program needs it. This is at runtime. JIT will do the second compilation of class when needed.
When we compile .java we get .class file.
The .class file is called byte code.
The Byte code in Java is nothing but a .class file which is not understandable by humans i.e (00110011). These .class files are generated only after the compilation of .java.
These .class file can be used to run on any platform.

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

Categories

Resources