JEP proposed to target JDK 11: 330: Launch Single-File Source-Code Programs
Peter Levart
peter.levart at gmail.com
Tue May 22 14:43:46 UTC 2018
Hi,
If this JEP added a feature that specifying source file name as "-"
would read STDIN as the Java source file, one could write executable
scripts in the following "hybrid" way:
#!/bin/bash
exec /path/to/java - $* <<EOF
public class Hello {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
EOF
Notice that this does not need shebang support from java launcher. The
benefit of this approach would also be that /path/to/java could be
computed by the bash script prefixing the Java source. Java typically
does not have a standard installation "place" like bash, so shebang
scripts with hard-coded java path would be less portable as bash
scripts. Of course one could do something similar with the following
trick (using support for shebang in java launcher):
#!/bin/bash
//bin/true; JAVA_HOME=<computed path> exec $JAVA_HOME/bin/java $0 $*
public class Hello {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
..but this still needs shebang support and script author is forced to
use a special prefix in each line of bash script prefixing the java source:
//bin/true; ...
//bin/true; ....
//bin/true; ...
Support for "-" STDIN source in java launcher does have one drawback.
Scripts written this way can not process STDIN passed to the script as
STDIN of java launcher is redirected to obtain Java source.
Regards, Peter
More information about the jdk-dev
mailing list