Are Java programs just instances of the JRE?
Solution 1:
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.
Solution 2:
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:
-
.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. -
Sometimes,
.class
files (and other resources) are bundled together into containers (.jar
files,.war
files,.ear
files, etc.). -
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. -
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.