1

As I understand

JVM: A Java virtual machine (JVM) is a virtual machine that can execute Java bytecode. It is the code execution component of the Java software platform. JVM interprets the byte code into the machine code

JRE: Java Runtime Environment contains JVM, class libraries . It's needed to run Java programs. You can't compile Java programs with it

JDK: jre + development tools such as debugger ,compiler (javac), java (launcher)

Question 1 : It is possible to compile only using jdk ? I mean get .class files ?

Question 2 : JRE needed to run java ? how it run if it can't compile class?

Question 3: why JRE provide jvm if it can't compile ?

Question 4 :Did I missed something in jdk explanation ?

nairavs
  • 475
  • 1
  • 7
  • 22

5 Answers5

2

Consider downloading a JRE, JDK, and checking the questions yourself. You never know if we're telling the truth here, you know...

You are mixing up the two stages of compilation.

  1. javac, part of the JDK, compiles .java to .class bytecode.

  2. The Hotspot JVM (part of the JRE) compiles heavily used bytecode to optimized machine code.

  3. It can compile .class to machine code, but not .java to .class.

  4. java (runtime) is part of the JRE, not the JDK.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
2

Q: It is possible to compile only using jdk ? I mean get .class files ?
A: Yes, e.g. javac Main.java will produce Main.class

Q: JRE needed to run java ? how it run if it can't compile class?
A: For example I give you a .class file, and you can run it on your jre. The .clas is compiled by me, you just run it.

Q: why JRE provide jvm if it can't compile ?
A: JRE is for clients. They don't need to compile .java files, the just need to run .class files. Developers need to compile .java files, which produces .class files.

Q: Did I missed something in jdk explanation ?
A: No.

2

To understand the differences better, you should know the promise of Java that made it so popular when it's launched.

  • WORA - Write Once Run Anywhere
  • Platform Independence (not entirely but mostly)

If you write a C program, you need to compile the .c file into a .exe file on windows which will not work on *NIX like environments. So here your resulting binary (in this case .exe) is dependent on your platform. If you want run the same C program on Linux, you've to compile the .c file again on that platform. But to make the Java binaries (.jar files usually) portable across multiple platforms, they came up with a uniform environment called JRE which will take care of executing your binaries in all platforms. If you ever downloaded Java from Oracle's site, you will find different files(exe,rpm,tar.gz for different platforms like Windows, Linux, Solaris,Mac etc)

Any .java compiled always gives a .class file which is the one understood by your JVM. The resulting class file is always same whether you compile it in on Windows or a Mac.

JRE is what you need to run your java programs. The one which will make sure that your programs written and compiled on Windows are executed on Linux servers. It doesn't need your fancy source code written in English to execute your program. That's why most of the new OS installations will have only JRE installed but not JDK because that's all you need.

JDK is for the developers who develop their applications/programs in java and then distribute for others to use.But the people who usually develop also run these programs in their local machines to test/debug their code. To facilitate that, JRE is included in the JDK distribution. JVM is the actual weight lifting guy, part of JRE which will translate your program instructions in to machine specific assembly code and execute on your CPU to get the job done.

Hope this answers your questions :)

Arkantos
  • 6,530
  • 2
  • 16
  • 36
1

There are lots of threads on this site that explain the differences. JDK is a superset of the JRE. As the name suggests: JDK is the development environment which also contains the runtime which is the JRE.

Please refer to one of my answers on this subject for an explanation and there is a link therein that would take you to one of those questions.

EDIT 1: A google got me "difference-between-jvm-jdk-jre-openjdk" as well.

Khanna111
  • 3,627
  • 1
  • 23
  • 25
1

In short:

  1. javac compile .java (text files with java code) into .class files (Java byte code).
  2. JVM interprets (and often just-in-time compile) Java byte code in .class file to run them.
  3. JRE also include the Java class libraries, i.e. set of .class files in .jar archives implementing the Java libraries.

I think what you miss is that there is often 2 compilation phases in Java: the one time compilation of .java text files to .class Java byte code, which are done by developers using the JDK and the Java byte code just-in-time compilation of byte code to machine code done by the JVM, which is done completely transparently while executing the Java byte code.

Now, sometimes things can get a little more complicated than that. For example, eclipse JDT contains its own java compiler different from javac, so does not need a JDK to compile java files to java byte code, just a JRE. It needs the JRE because eclipse is a Java applications, shipped as a set of JAR files containing the Java byte code and other resource files.