Changes
JEP 330: Launch Single-File Source-Code Programs (opens in a new tab)
-
Notes
-
Use
java
interpreter to execute source code directly, akasource-file
mode -
java HelloWorld.java
is informally equivalent to
javac -d <memory> HelloWorld.java
java -cp <memory> hello.World
-
You can add as many public classes as you need in the single source file. The only thing that matters is that the main method should be defined in the first class in the source file.
-
The in-memory compiled code runs as part of an unnamed module with the option --add-modules=ALL-DEFAULT. This enables the code to use different modules without the need to explicitly declare dependency using the module-info.java.
-
Test with
java --source <version> <script>
first before usingshebang
approach, as there could beJVM
compatibility issues. When used withshebang
, specifying a compatible Java interpreter.
-
-
Cheatsheet
-
Run source code file with
java
extensionjava <Java-source-code-file.java> <arguments...>
-
If the file does not have the .java extension, the --source option must be used to force source-file mode.
java --source <java-source-version> <Java-source-code-file> <arguments...>
-
Shebang
files-
Add
shebang
line#!<java-interpreter-path> --source <java-source-version>
-
Make the source code file executable.
chmod +x <script>
-
Run the source code file like any shell script
./<script>
-
-
-
Resources
JEP 320: Remove the Java EE and CORBA Modules (opens in a new tab)
java.xml.ws.annotation
removed from JDK, use the following dependency instead:
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<scope>provided</scope>
</dependency>