JDK-8209963 : source file mode for JVM should provide a hook to locate the source file
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 11
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2018-08-25
  • Updated: 2022-02-14
  • Resolved: 2018-09-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 12
12 b14Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Description
JEP 330 adds a mode which allows Java to be delivered in "shebang" scripts.  A feature very commonly used in shebang scripts is locating the script file itself, with the Unix shell using the syntax "$0" (a special parameter name).  Shebang scripts coded in Java will have the same kinds of occasions to locate themselves, and so there should be a hook for obtaining the pathname of the script file, from code executing in the script itself.

(Uses of the script pathname are not specific to the Unix shell or to any particular language; they include issuing diagnostics and locating resource files packaged with the script.  These uses potentially apply to Java scripts, even the simple ones envisioned by JEP 330.)

This requirement can be met almost trivially, by picking a property name, such as "source", and ensuring that the JVM (when invoked in source-file mode) binds the pathname (or perhaps URL) of the source file the given property name, as if by the JVM command line option "-Dsource=path/to/script/file".  The script can easily pick up this pathname using the System.getProperty API point, as long as there is an agreed-upon property name.

Workaround:  Use a carefully crafted shell script wrapper to launch the JVM when an explicit "-Dsource=$0" argument.  See http://cr.openjdk.java.net/~jrose/scripts/myshebang.txt
Comments
URL: http://hg.openjdk.java.net/jdk/jdk/rev/904c4e07cc22 User: jjg Date: 2018-09-26 23:23:41 +0000
26-09-2018

I think the name "source" is too short. It should have a "standard" prefix, like "jdk." I suggest "jdk.source.file" ... and be the filename. I don't anticipate any need for the URL at this time.
29-08-2018

The shebang feature is somewhat limited by the spec (or lack thereof) for shebang itself. During the discussion for the JEP, one idea that arose was the use of a combination of shell script and using the source-file launcher with a here-document [1], and this suggestion is one that gets around most of the problems with the basic shebang feature. Using the system property as suggested in this issue would not help in that case, because the name of the input file will be something like /dev/fdN for some N. On the other hand, it would be trivially easy to use a custom system property set to $0 using the shell-script and here-document technique, in a much simpler form than the cryptic head-scratcher proposed as a workaround. [1] http://mail.openjdk.java.net/pipermail/jdk-dev/2018-May/001260.html
25-08-2018