Search code examples
javajvmjava-11

How do java.exe execute a java source code?


When I had only HelloWorld.java in the directory without HelloWorld.class, java HelloWorld in Windows cmd raised a class not found exception. However, java HelloWorld.java was executed properly without making HelloWorld.class.

From what I understand, JVM reads the bytecodes of a file with .class extension, but fails to read .java file. I understand that java.exe is just a launcher to create JVM and run it, but am now confused how it can work properly with java code, not byte code.


Solution

  • tl;dr

    In modern Java, as a convenience to beginner Java students, the java app will graciously compile, and then run, a .java file.

    Think of it as java app detecting a source-code file, then sub-contracting the compilation work out to javac app, caching the compiled class in memory, and lastly continuing onwards to run the newly compiled class.

    Details

    A JDK comes bundled with a few dozen tools. A couple of these are crucial:

    • javac — Compiles .java file of source code text into .class file of bytecode.
    • java — Execute that .class file by launching a JVM.

    The process steps are:

    1. You write Java source code in a .java file, including a main method inside a class definition.
    2. You submit that file to a compiler, such as the javac app bundled with every JDK.
    3. The compiler outputs a .class file.
    4. You execute (run) the main method by launching the java app bundled with every JDK.

    You can skip some steps.

    • Compile and run a single file:
      As of Java 11, you can point the java app to a .java file. The java app will automatically compile first, as if javac were called on your behalf. Then the java app will go on to run the newly compiled class.
      • No .class file is written to storage. The compiled class is cached in memory.
      • See JEP 330: Launch Single-File Source-Code Programs for all the details.
      • This behavior is what you noted: However, java HelloWorld.java was executed properly without making HelloWorld.class.
    • Simplified main
      As a preview feature in Java 21, you can:

    Motivation

    The Java team is making concerted efforts to ease the way for beginning Java programmers, to smooth over the initial speed bumps. They are trying to mask some of the elaborate ceremony, and make the tooling more friendly and accommodating.

    The underlying structures and features of Java are still in place. This is not a “dumbing-down” of Java. These efforts are just attempts at making reasonable accommodations for beginners.

    Hello World

    Combining these features, getting started with a Hello World app is a simple as writing the following in a HelloWorld.java file:

    void main() {
        System.out.println("Hello, World!");
    }
    

    … and then on the command-line calling:

    java HelloWorld.java
    

    Result on the console:

    Hello, World!

    That is a vast reduction in labor and confusion for a brand-new student of Java.


    jshell

    By the way… Another piece of making Java easier to use is jshell, a REPL for Java.

    Both beginners and pros find jshell handy for running short bits of Java with immediate feedback.

    BlueJ

    Another useful tool for beginners is BlueJ, an IDE designed for students.