Changes
JEP 330: Launch Single-File Source-Code Programs (opens in a new tab)
-
Notes
-
Use
javainterpreter to execute source code directly, akasource-filemode -
java HelloWorld.javais informally equivalent to
javac -d <memory> HelloWorld.javajava -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 usingshebangapproach, as there could beJVMcompatibility issues. When used withshebang, specifying a compatible Java interpreter.
-
-
Cheatsheet
-
Run source code file with
javaextensionjava <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...> -
Shebangfiles-
Add
shebangline#!<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.annotationremoved 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>