Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 991807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:10:02+00:00 2026-05-16T06:10:02+00:00

Confused by java compilation process OK i know this: We write java source code,

  • 0

Confused by java compilation process

OK i know this: We write java source code, the compiler which is platform independent translates it into bytecode, then the jvm which is platform dependent translates it into machine code.

So from start, we write java source code. The compiler javac.exe is a .exe file. What exactly is this .exe file? Isn’t the java compiler written in java, then how come there is .exe file which executes it? If the compiler code is written is java, then how come compiler code is executed at the compilation stage, since its the job of the jvm to execute java code. How can a language itself compile its own language code? It all seems like chicken and egg problem to me.

Now what exactly does the .class file contain? Is it a abstract syntax tree in text form, is it tabular information, what is it?

can anybody tell me clear and detailed way about how my java source code gets converted in machine code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:10:02+00:00Added an answer on May 16, 2026 at 6:10 am

    OK i know this: We write java source code, the compiler which is platform independent translates it into bytecode,

    Actually the compiler itself works as a native executable (hence javac.exe). And true, it transforms source file into bytecode. The bytecode is platform independent, because it’s targeted at Java Virtual Machine.

    then the jvm which is platform dependent translates it into machine code.

    Not always. As for Sun’s JVM there are two jvms: client and server. They both can, but not certainly have to compile to native code.

    So from start, we write java source code. The compiler javac.exe is a .exe file. What exactly is this .exe file? Isn’t the java compiler written in java, then how come there is .exe file which executes it?

    This exe file is a wrapped java bytecode. It’s for convenience – to avoid complicated batch scripts. It starts a JVM and executes the compiler.

    If the compiler code is written is java, then how come compiler code is executed at the compilation stage, since its the job of the jvm to execute java code.

    That’s exactly what wrapping code does.

    How can a language itself compile its own language code? It all seems like chicken and egg problem to me.

    True, confusing at first glance. Though, it’s not only Java’s idiom. The Ada’s compiler is also written in Ada itself. It may look like a “chicken and egg problem”, but in truth, it’s only a bootstrapping problem.

    Now what exactly does the .class file contain? Is it an abstract syntax tree in text form, is it tabular information, what is it?

    It’s not Abstract Syntax Tree. AST is only used by tokenizer and compiler at compiling time to represent code in memory. .class file is like an assembly, but for JVM. JVM, in turn, is an abstract machine which can run specialized machine language – targeted only at virtual machine. In it’s simplest, .class file has a very similar structure to normal assembly. At the beginning there are declared all static variables, then comes some tables of extern function signatures and lastly the machine code.

    If You are really curious You can dig into classfile using “javap” utility. Here is sample (obfuscated) output of invoking javap -c Main:

    0:   new #2; //class SomeObject
    3:   dup
    4:   invokespecial   #3; //Method SomeObject."<init>":()V
    7:   astore_1
    8:   aload_1
    9:   invokevirtual   #4; //Method SomeObject.doSomething:()V
    12:  return
    

    So You should have an idea already what it really is.

    can anybody tell me clear and detailed way about how my java source code gets converted in machine code.

    I think it should be more clear right now, but here’s short summary:

    • You invoke javac pointing to your source code file. The internal reader (or tokenizer) of javac reads your file and builds an actual AST out of it. All syntax errors come from this stage.

    • The javac hasn’t finished its job yet. When it has the AST the true compilation can begin. It’s using visitor pattern to traverse AST and resolves external dependencies to add meaning (semantics) to the code. The finished product is saved as a .class file containing bytecode.

    • Now it’s time to run the thing. You invoke java with the name of .class file. Now the JVM starts again, but to interpret Your code. The JVM may, or may not compile Your abstract bytecode into the native assembly. The Sun’s HotSpot compiler in conjunction with Just In Time compilation may do so if needed. The running code is constantly being profiled by the JVM and recompiled to native code if certain rules are met. Most commonly the hot code is the first to compile natively.

    Edit: Without the javac one would have to invoke compiler using something similar to this:

    %JDK_HOME%/bin/java.exe -cp:myclasspath com.sun.tools.javac.Main fileToCompile
    

    As you can see it’s calling Sun’s private API so it’s bound to Sun JDK implementation. It would make build systems dependent on it. If one switched to any other JDK (wiki lists 5 other than Sun’s) then above code should be updated to reflect the change (since it’s unlikely the compiler would reside in com.sun.tools.javac package). Other compilers could be written in native code.

    So the standard way is to ship javac wrapper with JDK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know about C and I am entering into Java and confused about its
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
I just started learning Java and I'm confused about the topic of platform independence.
I'm quite confused about some terms, I know Java for writting some usual executable
I am confused about StringPool in Java. I came across this while reading the
I'm confused with variable declarations in Java code. I read... don't try to use
I am confused with be below behaviour in Java, I know that static inheritance
I'm pretty new to Java and this has me really confused. I have two
I am a bit of confused about java generics Here is the code class
I am a Java beginner and pretty confused with this. How is System.out.println(4*2147483647) equal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.