ã¿ããªã§Javaã¹ã¯ãªããã«å ¥éãã¾ãããï¼
ã¯ã©ã¹ãã¡ããã¨å®ç¾©ãã
ããã§ã¯ãã¾ãã¯Javaã£ã½ãæ¸ãã¦ã¿ã¾ãããã
public class Main { public static void main(String[] args) { System.out.println("Hello!"); } }
ããããä»»æã®ãã¡ã¤ã«åã§ä¿åãã¾ãããã¨ãã°helloã
å®è¡ããã«ã¯Java 11以éãå¿
è¦ã§ããã¿ãªãããã§ã«SDKMANãå
¥ãã¦ãã¨æãã®ã§Oracle OpenJDKã®12.0.1ã使ã£ã¦ã¿ã¾ãããã
$ sdk use java 12.0.1-open
ã§ã¯å®è¡ãã¾ã
$ java --source 12 hello Hello!
å®è¡ã§ãã¾ããï¼
ããã§ã¯ã¡ãã£ã¨é¢ç½ããªãã®ã§ãå ã»ã©ã®ãã¡ã¤ã«ã®å é ã«ã¡ãã£ã¨ãªã«ã追å ãã¦ããã¾ãã
#! /home/naoki/.sdkman/candidates/java/current/bin/java --source 12 public class Main { public static void main(String[] args) { System.out.println("Hello!"); } }
ããããShebangã§ããã
ããã¦å®è¡æ¨©éãä»å
$ chmod +x hello
å®è¡ãã¦ã¿ã¾ãã
$ ./hello Hello!
ãã£ãï¼
ããã¯JEP 330: Launch Single-File Source-Code Programsã«åºã¥ããã®ã§ãJava 11ããå°å
¥ããã¦ãã¾ãã
Java11ã§ã¯javacããã«Javaãã¡ã¤ã«ãå®è¡ã§ããããã«ãªã - ããã ã®Hatena
ããããã¹ã¯ãªãããã¨å¼ã°ãªãã¨ããç¾ä»£ã®ã¹ã¯ãªããè¨èªãå®è¡æã«ãã¤ãã£ãã³ã¼ãã«ãªã£ããããã®ã§ãã©ã®æç¹ã§ã³ã³ãã¤ã«ãããã°ã¹ã¯ãªãããªã®ã(ã¹ã¯ãªããã§ã¯ãªãã®ã)ã¨ãã話ã«ãªã£ã¦ãããããã§ããã
ãã£ã¨ã¹ã¯ãªããã£ã½ãæ¸ã
ãã£ãã®ã¯ ãã¾ãã«ãJavaã ã£ãã®ã§ããã£ã¨Javaã£ã½ããªãæ¸ãã¦ã¿ã¾ãã
System.out.println("Hello2!")
/exit
ãã®ãã¡ã¤ã«ãä»»æã®ãã¡ã¤ã«åã§ä¿åãã¾ãããã¨ãã°hello2ãè¡æ«ã®ã»ãã³ãã³ãä¸è¦ã§ãã
ã§ã¯å®è¡ãã¾ããå®è¡ããããã«ã¯Java 9以éãå¿ è¦ã§ãããã§ã«å ã»ã©Java 12ãå°å ¥ãã¦ã¾ããã
$ jshell hello2 Hello2!
ãã£ãï¼
ããã¡ãã£ã¨è¤éãªã³ã¼ããæ¸ãã¦ã¿ã¾ãããã
import java.time.* var t = LocalDate.now() System.out.println(t.minusMonths(1) + " to " + t) System.out.println("Hello2!") /exit
å®è¡ãã¦ã¿ã¾ãã
$ jshell hello2 2019-05-29 to 2019-06-29 Hello2!
ã ãã¶ã¹ã¯ãªããã£ã½ãï¼
注æãå¿ è¦ãªã®ã¯ããããã¯ã®ä¸ã§ã¯ã»ãã³ãã³ãå¿ è¦ã¨ããã¨ããã§ãã
import java.time.* var t = LocalDate.now() System.out.println(t.minusMonths(1) + " to " + t) for (int i=0; i< 3; ++i) { System.out.println("Hello" + i); } /exit
å®è¡ãã¦ã¿ã¾ãã
$ jshell hello2 2019-05-29 to 2019-06-29 Hello0 Hello1 Hello2
Shebangããã£ã¦ã¿ã¾ãããã
#! /home/naoki/.sdkman/candidates/java/current/jshell import java.time.* var t = LocalDate.now() System.out.println(t.minusMonths(1) + " to " + t) for (int i=0; i< 3; ++i) { System.out.println("Hello" + i); } /exit
ããã¦å®è¡ã
$ ./hello2 ã¨ã©ã¼: '#'ã¯ä¸æ£ãªæåã§ã #! /home/naoki/.sdkman/candidates/java/current/bin/jshell ^ ã¨ã©ã¼: å¼ã®éå§ãä¸æ£ã§ã #! /home/naoki/.sdkman/candidates/java/current/bin/jshell ^ ã¨ã©ã¼: å¼ã®éå§ãä¸æ£ã§ã #! /home/naoki/.sdkman/candidates/java/current/bin/jshell ^ 2019-05-29 to 2019-06-29 Hello0 Hello1 Hello2
ãã¼ãï¼ï¼
å®è¡ãã㯠ãããã©ã#!
ã®è¡ãJavaã¨ãã¦è§£éãããã¨ããããã«ã¨ã©ã¼ã«ãªã£ã¡ãã£ã¦ã¾ããã
ããã©ãã«ãããªãã®ããªï¼
7/1è¿½è¨ BTSã¯ç«ã£ã¦ã¾ããã https://bugs.openjdk.java.net/browse/JDK-8167440
ã¡ãªã¿ã«ãã·ã§ã«ã¹ã¯ãªããã§1è¡ç®ãç¡è¦ããã¨ããããã¾ãããOpenJDKã½ã¼ã¹ãæ¹å¤ãããªããããã«
if (src.startsWith("#")) { return false; }
ãå
¥ããã¨#
ãã³ã¡ã³ãã«ãªã£ã¦ã¤ã±ã¾ãã
https://github.com/openjdk/jdk/blob/master/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java#L1194
ã¾ã¨ã
Javaã¹ã¯ãªããæé«ï¼