Friends of OpenJDK Today

Running Single-File Java Source Code Without Compiling (Part 1)

September 08, 2020

Author(s)

  • Mohamed Taman

    Mohamed Taman (@_tamanm) is CEO/Owner at SiriusX Innovations, Chief Architect, Java Champion, Oracle Groundbreaker Ambassador, Jakarta EE Ambassador, JCP, Technology Geek, International Speaker, and Author.

If you recall the old days, before OpenJDK 11, say you have a HelloUniverse.java source file that contains a class definition and a static main method, which prints out as a single line of text to the terminal, like the following:

public class HelloUniverse{
      public static void main(String[] args) { 
            System.out.println("Hello foojay!");
      }
}

Normally to run this class, first, you would need to compile it, using a Java compiler (javac), which would result in a HelloUniverse.class file: 

> javac HelloUniverse.java

Then you would use a java virtual machine (interpreter) command to run the resulting class file:

> java HelloUniverse
Hello foojay!

This starts up the JVM, loads the class, and executes the code.

But what if you want to quickly test a piece of code or if you're new to learning Java and want to experiment with the language? Those two steps in the process may seem like a bit of heavy lifting.

From OpenJDK 11 onwards, you get the option to launch a single Java source file directly, without intermediate compilation. This feature is particularly useful for someone new to the language who wants to try out simple programs and when you combine this feature with the jshell (a topic for another time), you get a great beginner’s learning toolset.

Professionals can also make use of these tools to explore new language changes or to try out an unknown API. In my opinion, greater power comes when we can automate a lot of tasks, such as writing Java programs as scripts, and then executing them from the operating system shell. This combination gives us the flexibility of shell scripts with the power of the Java language.

We will explore this in more detail in the next part of this series.

Note: Used with permission and thanks — originally written and published by Mohamed Taman.

Topics:

Author(s)

  • Mohamed Taman

    Mohamed Taman (@_tamanm) is CEO/Owner at SiriusX Innovations, Chief Architect, Java Champion, Oracle Groundbreaker Ambassador, Jakarta EE Ambassador, JCP, Technology Geek, International Speaker, and Author.

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard