Are Java programs just instances of the JRE?

All of this is to ask, are all Java programs simply console java [argument] programs?

Not that specifically, no, because not all Java programs are run via the java tool, but keep reading.

Another way to ask, are all Java programs just JRE programs/instances that are reading a particular class file?

Java programs are usually run by a Java virtual machine (JVM), such as the one in the Java Runtime Environment, yes. That is, in most situations, the Java program (the collection of class bytecode and other resources that makes up the program, sometimes in a .jar/.war/.ear/etc. file) is loaded and run by an instance of the JVM, which is launched by the java tool or a servlet container (or, back in the day, an applet container) or some other environment that knows how to spawn a JVM instance.

Usually, it goes like this:

  1. .java files are compiled to Java bytecode, typically output as .class files. Java bytecode is a high-level machine language that isn't dependent on a specific CPU architecture or operating system.

  2. Sometimes, .class files (and other resources) are bundled together into containers (.jar files, .war files, .ear files, etc.).

  3. When it's time to run the program, you use the java tool or a servlet container or some other kind of process that knows how to run Java bytecode. These are CPU- and OS-specific and incorporate or load a JVM.

  4. Code in the tool (java or servlet container or other) loads the bytecode (from the .class file or similar) and passes it to the JVM to be instantiated and executed. Depending on the JVM, that might involve just interpreting the bytecode, or compiling it to CPU- and OS-specific machine code ("just-in-time" [JIT] compilation) and executing that, or a combination of the two. Sun's HotSpot JVM, for instance, does at least two levels of JIT compilation depending on whether a specific segment of code is used enough to bother to compile it and, if so, enough to justify aggressively optimizing it.

There are compilers that compile Java source code or Java bytecode to machine code for specific CPU architectures and OSes, outputting an all-in-one executable, but the above is the usual scenario.


To put a simpler spin on this, the answer is: Yes (although you really mean the JVM rather than the JRE). The program that the OS is running is the JVM (Java virtual machine), and the Java application is data being read by that program. The JVM is like Microsoft Word, and Java programs are like Word documents.

This question is hitting upon the essential difference between compiled and interpreted languages, as described well here.

To use the analogy further to explain what the JVM and JRE are...The JVM is like the Microsoft Word program itself, and the JRE is like the MS Word program plus all the other stuff, like templates, sample documents, fonts, etc. that is installed along with it to support what it does.


I think it helps here to step back and look at the bigger picture here. When you run a Java program the way you describe, it's running in a virtual machine. A specific one that happens to have the name 'java'.

There are other VMs that run Java, however. One of the newer VMs is GraalVM. I'm not sure it's completely correct to call it a JVM because it can (supposedly) also run other languages such as Python, Ruby, C, and C++. So if you run a C++ program in GraalVM, is that C++ program now 'just' a GraalVM application? I don't think so. To further muddy the waters, GraalVM can compile Java programs to native binaries.

As kind of an aside, there's nothing special about Java with regard to having a runtime environment. C# (.NET) has the CLR which was definitely and absolutely in no way based on the ideas of the JVM, of course. CPython has a runtime called 'python'.

If I am running Windows in a virtual machine running on Linux, and I am running program written in C++ in it, is Windows now just a program running on Linux? If we say yes, what does that make the C++ application? Is it a standalone program? What about a C++ application running in a container on a virtual machine running running on a server in the cloud. Is that program any less 'real' running in that configuration than it is when running on your desktop?

TLDR: Virtualization is ubiquitous in computing. There are definitely aspects of the standard JVM that are distinct from other virtualization technologies, but these are fairly minor distinctions in the grand scheme of things.

Tags:

Java