Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compilation in JAVA

 
gauravkv gupta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to know how compilation is done in JAVA. Is it done by JVM.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gauravkv gupta wrote:Hi all,

I need to know how compilation is done in JAVA. Is it done by JVM.


That's a very broad question. For that kind of question, you should SearchFirst(⇐click) and only post to a forum once you've done some research and have a more specific quesitons.

A couple of things to note though:

1) It's Java, not JAVA. It's not an acronym.

2) You'll need to be specific about what kind of compilation you're asking about. When people talking about compiling in a Java context, they usually mean compiling Java source code into byte code, which is done by a compiler, not by the JVM. However, another possible meaning for your question is about the JVM compiling byte code to native code during execution. For that you could start your research by reading up on "java hotspot".
 
Stephan van Hulst
Bartender
Pie
Posts: 5898
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partly. First a compiler like javac.exe will compile Java to bytecode, stored in .class files. Then when you run a class, the JVM may compile parts of it to native code when it feels it's a good thing to do so, or it may just interpret the bytecode and run it immediately.

For a more detailed and technical description you could take a look at the Java Virtual Machine Specification. It's no easy reading though.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
For a more detailed and technical description you could take a look at the Java Virtual Machine Specification. It's no easy reading though.


That will cover compiling to bytecode. I don't think it covers anything about compiling bytecode to native, since that's purely up to the implementation.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gauravkv gupta wrote:I need to know how compilation is done in JAVA. Is it done by JVM.

Short off, Yes. compiler is made by logic/thinking ... compiler is a software
 
gauravkv gupta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Partly. First a compiler like javac.exe will compile Java to bytecode, stored in .class files. Then when you run a class, the JVM may compile parts of it to native code when it feels it's a good thing to do so, or it may just interpret the bytecode and run it immediately.

For a more detailed and technical description you could take a look at the Java Virtual Machine Specification. It's no easy reading though.


Since JVM is a virtual machine then my question is how is it possible to compile to native code using a Virtual machine. Unit which compile the byte code is JVM or JRE.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First install jdk from oracle website


First you need to set the path variable in command prompt


Compile the file


run the file

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gauravkv gupta wrote:Unit which compile the byte code is JVM or JRE.

Java Runtime Environment (JRE) just gives you a execution environment for executing your byte code. JRE itself contains a native JVM implementation and other required jars / libs to execute your java program (byte code).

The tool that compiles your java classes into executable byte code is compiler (javac tool that comes with JDK).

Wiki - JVM
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gauravkv gupta wrote:
Since JVM is a virtual machine then my question is how is it possible to compile to native code using a Virtual machine.


It's a piece of software. Software can do whatever we tell it to do. The JVM compiling byte code to native code is no different than a C compiler producing native code.

Unit which compile the byte code is JVM or JRE.

As I said in my first reply: The compiler (usually javac) compiles source code to byte code, and the JVM can compile byte code to native code.

The JVM is part of the JRE.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic