JEP 330 and MemoryClassLoader.getResourceAsStream
Jonathan Gibbons
jonathan.gibbons at oracle.com
Sat Aug 25 14:52:28 UTC 2018
John,
A much simpler way to do what you want is to build upon a suggestion
that arose during the discussion for this JEP [1]. This is just to use
shell scripts and here documents.
Here is the example proposed by Stuart, amended to also use a
system property for the filename.
#!/bin/bash
exec java --source 11 -Dsource="$0" /dev/fd/3 "$@" 3<<EOF
public class Hello {
public static void main(String[] args) {
String file = System.getProperty("source");
String name = System.console().readLine(source + ": Please enter your name: ");
System.console().printf("Hello, %s!%n", name);
}
}
EOF
Although the example is not as concise as direct use of shebang for Java,
it has the advantage of getting round many of the problems we have seen
with shebang, and it also provides an easy way to get at the $0 path as
well.
-- Jon
[1] http://mail.openjdk.java.net/pipermail/jdk-dev/2018-May/001260.html
On 8/25/18 12:00 AM, John Rose wrote:
> On Aug 24, 2018, at 9:34 PM, Jonathan Gibbons
> <jonathan.gibbons at oracle.com <mailto:jonathan.gibbons at oracle.com>> wrote:
>>
>>> I also believe that this shouldn't require a workaround at all,
>>> but should be somehow native to the JVM, as $0 is to
>>> the shell. So I also filed JDK-8209963 to track this RFE.
>> We could define and use a system property with the filename.
>
> Yep. My sample code is designed around the hope that
> we'd do that someday. At that point, the nasty stuff at the
> top could be stripped, up to the second shebang "#!",
> and without the explicit -D options.
>
> — John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180825/835d9c62/attachment.html>
More information about the compiler-dev
mailing list