If you recall the old days before Java SE 11 (JDK 11), you could say that you had a helloworld.java source file that contained the class definition and the static main method, which was printed to the terminal as a single line of text:

More in this series

Content This content is part of the Java Theory & Practice series.

But what if you want to test a piece of code quickly, or if you’re just learning Java and want to try out the language? These two steps in the process may seem cumbersome.

In Java SE 11, you can choose to launch individual source files directly without compiling them.

This feature is especially useful for newcomers to the language. When you combine this with JShell, you get an excellent beginner’s toolset. Professionals can also use these tools to explore new language changes or try out unknown apis. You can also automate tasks, such as writing Java scripts and executing them as scripts at the operating system level.

For more information about the new Jshell 10+, see the following two tutorials on this topic in this series:

 “Java Theory and Practice: Interactive Java Programming with JShell 12, Part 1 (REPL)” (IBM Developer, April 2019)

 “Java Theory and Practice: Interactive Java Programming with JShell 12, Part 2 (REPL)” (IBM Developer, April 2019)

What you need to follow

To run all the demonstrations provided in this tutorial, you will need to download the Java SE 11 JDK. In this tutorial, I use a plain text editor instead of a Java IDE because I want to avoid any IDE magic and use the Java command line directly throughout the terminal.

Running.java and Java JEP 330, launching single-file source code programs, are among the exciting features introduced in the JDK 11 release. This feature allows you to execute Java source code files directly using the Java interpreter. The source code is compiled in memory and then executed by the interpreter.

Function limit

However, this capability is limited to code residing in a single source file. You cannot add other source files to be compiled in the same run. To work around this limitation, all classes must be defined in the same file, but there is no limit to the number of classes in the file.

The simplest example of functionality

Now, let’s take the simplest example: Hello World! What we do all the time when we start learning something new – it’s exactly what you’d guess. I won’t go into the implementation details of this feature, but WILL focus on demonstrating how to use it by trying different examples so you can understand how to use it in your coding.

If the HelloWorld.java file has not been created, compile it and run the generated class file. Now, I want you to delete the class files; You’ll soon see why:

Therefore, compilation is in progress, and if there is a compilation error, you will receive an error notification. In addition, you can check the directory structure to see that no class files have been generated. This is an in-memory compilation process.

Now let’s see how this happens.

How does the Java interpreter run HelloWorld

Starting with JDK 10, Java initiators run in three modes:

1. Run a class file

2. Run the JAR file’s main class

3. Run the main class of the module

Now let’s add a new fourth mode: running classes declared in source files.

The source file mode is determined by two items on the command line:

1. The first item on the command line is neither an option nor part of the option

2. The –source option (if present)

For the first item, the Java command treats the first item on the command line (which is neither an option nor part of an option) as the Java source file to compile and run. You can still provide options for Java commands before the source file name. For example, if you want to set the classpath when the source file uses external dependencies.

For the second item, if filename identifies an existing file with a. Java extension, the source file mode is selected, and the file is compiled and run. The –source option can be used to specify the source version of the source code. I’ll discuss this in detail later.

In other words, Java will look for the first.Java. Note that I said filename, not class name.

If the file does not have a. Java extension, then –source must use this option to enforce the source file schema.

This is required in cases where the source file is a script to execute and the name of the source file does not follow the normal naming convention for Java source files. (I’ll have more information on this when I shebang discuss the files later in this tutorial.)

In source-file mode, the effect is like compiling the source file into memory and executing the first class found in the source file. Can you pass command-line arguments?

Let’s enhance the Hello World program to create a personalized greeting for anyone who accesses IBM Developer World:

Run the following code to see what happens:

And I know you’ve noticed with your hawk-eye how I pass arguments to the code after the file name to execute.

Therefore, any arguments that appear on the command line after the file name are passed to the standard method in the obvious way of main.

Use the -source option to specify the level of the code file

There are two situations in which this –source option can be used:

1. You can specify the source level of the code file using the source option

2. You can force the Java runtime into source execution mode

In the first option, when you omit the source level, assume it is the current JDK version, which in this case is 11. With the second option enabled, you can now also pass files with other Java extensions for compilation and instant running.

Now let’s first check the second case, rename greater.java to Greater without any extension, and try to execute it again using the same method:

Can you take more than one course?

The answer is yes.

Examine a piece of sample code containing two classes to indicate that the code will check to determine whether a particular given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same thing in two directions, such as “lady” or “race car”. This is the code saved in the filename palindromechecker.java:

Are modules allowed?

Yes, modules are fully allowed. The compiled code in memory runs as part of an unnamed module with the option — -add-modules= all-default. This enables code to use different modules without explicitly declaring dependencies on module-info.java.

Let’s take a look at the code for making HTTP calls using the new HTTP client API that comes with JDK 11. These apis are incubator features in JavaSE9, but have been progressively introduced into the java.net.http module.

In this example, I will call a simple REST API GET method by calling the endpoint service for some users to https://reqres.in/api/users? Page = 2. The sample code is in the file named userShttpClient.java:

Script, why is it needed, why is it important

First, let’s look at what scripting is to see why it is an important part of the Java programming language.

We can define the script as follows:

“This is a program written for a special runtime environment that can perform tasks automatically or by a human.”

From this general definition, we can derive a simple definition of a scripting language: A scripting language is a programming language that uses high-level constructs to interpret and execute commands one at a time:

 script using the file in a series of commands, these commands to perform – usually need for compilation, scripting languages are usually rather than compile  and more structured and compile language (such as Java, C and C + +), they are more easy to learn, and you can use them faster encoding a good example of server-side scripting language including Perl, PHP and Python. On the client side is JavaScript.

Java has long been classified as a well-structured, strongly typed, and compatible language that can only be interpreted by the JVM to run on any computer architecture. This is the main complaint about Java. Learning or prototyping in other scripting languages is not fast.

But Java is now a mature language, used by about 900, 000 developers worldwide. To make it easier to learn and to try out its features and apis without having to participate in a complex compilation process, the Release of Java SE 9 added JShell REPL, a tool that supports interactive programming.

JShell REPL makes Java easy to learn and fast to experiment with. If you don’t believe me, skip to the two hands-on tutorials on using JShell in this series and read them.

So, how do you script in Java 11?  You can run the code by calling the command in Java in two basic ways:  Use the Java direct command tool  Use the * nix terminal script as a bash script. We’ve covered the first option, so let’s explore the second option.

Keep in mind that Java SE 11 introduces support for scripting using traditional * nix Shebang files. No change in the Java language specification is required to support this feature.

In a normal Shebang file, the first two bytes must be “0x23” and “0x21”, the ASCII encoding of the two characters “#!” . Read all subsequent bytes using a valid default platform character encoding.

The first line begins with #! Only needed if you want to execute files using the operating system’s shebang mechanism. As in our previous example helloworld.java, you don’t need a special first line when explicitly using the Java launcher to run code in a source file.

There are a few other important rules you need to know when creating shebang files.

The rules for creating shebang files do not mix Java code with the required operating system shell scripting language.

If you need to include virtual machine options, you must specify –source as the first option after the executable file name in the shebang file. These options include: –class-path, –module-path, –add-exports, –add-modules, –limit-modules, –patch-module, –upgrade-module-path, And any variation of these options. It can also include the new options described in JEP 12 — enable-Preview.

You must specify the Java language version in the file for the source code.

Shebang character (#!) Must be the first line of the file and should look like this: #! The/path/to/Java source. The shebang mechanism is not allowed. Java executes files that follow standard naming conventions (ending files) for Java source files.

Finally, you must mark the file as executable using the.

The show begins; Let’s create a shebang file (script utility) that lists the contents of the passed directory as parameters. If the directory fails, the current directory is listed by default. The example runs on a terminal using MacOS Mojave 10.14.3. Source code is:

mohamed_taman:code$chmod +x dirlist

Run as follows:

mohamed_taman:code$ ./dirlist .. /

Note that the interpreter ignores the shebang line (the first line) when evaluating the source code. The shebang file can also be called explicitly by the launcher, perhaps with other options, such as $java-dtrace =true –source 11 dirList. Alternatively, if the script file is in the current directory, it can be executed as follows:

$ ./dirlist

Alternatively, if the script is in the directory of the user’s PATH, it can be executed as follows:

$ dirlist

Finally, I want to show you some tricks and pitfalls to be aware of when using this feature. Tips, Tricks, and pitfalls When using this feature, you can get into some tricky situations. Options may not pass or be unrecognized Java tools that you can pass to certain options javac may not pass (or recognize this), such as the -processor or -werror options. When you are forced to use class files if.class and do.java exist in the classpath, you must use class files.

Remember class and package naming conflicts. Check the following directory structure:

The Java launcher no longer references the class files in the HelloWorld package. Instead, it loads the helloworld.java file from the source. The result is that files in the current directory will run.

I like this shebang feature because it offers endless possibilities for creating scripts that can automate a lot of work using the power of the Java language.