会议是聚会Java名人的好地方。 Devoxx France是与Java语言架构师,前同事和老朋友Brian Goetz( @briangoetz )见面的一个机会。 我们谈论了JDK 9,而他全都热衷于REPL。 他提到,尽管Java SE 9中有很多重要功能 ,例如模块化和HTTP2客户端 ,但该工具将是最常被谈论的。 该声明之所以有意义,是因为它将真正简化Java API的探索,原型开发,会议演示以及类似任务。 该博客来自我们在那里的讨论以及他对REPL的强烈投票!
Read-Evaluate-Print-Loop在Lisp,Python,Ruby,Groovy,Clojure和其他语言中已经存在了一段时间。 Unix shell是一个REPL,它可以读取shell命令,评估它们,打印输出并返回循环以执行相同的操作。
您可以在JEP 222的 JDK 9中阅读有关REPL的全部信息。 JEP的摘要是:
提供一个交互式工具,用于评估Java编程语言的声明,语句和表达式:即,提供Java编程语言的读取-评估-打印循环(REPL)。 另外,提供构建工具的API,使外部工具可以提供此功能。 杰普222
JEP中也明确阐明了动机:
没有Foo类{public static void main(String [] args){…}}的仪式,学习和探索就变得简化了。 杰普222
JEP 222的目标是将REPL与JDK 9一起提供,但是openjdk.java.net/projects/jdk9并未将其列为“目标”或“建议目标”。 好像是一个文档错误:)
从JDK 9 build 61开始,REPL未集成,需要单独构建。 最终,在发布JDK 9之前的某个时间,此工具将集成到内部版本中。
让我们看看在OSX上运行它需要什么。 该博客遵循Java 9 REPL –入门指南来构建和运行REPL。 另外,它提供了命令的完整日志输出,这可能对某些人有用。
让我们开始吧!
安装JDK 9
- 在撰写本文时,下载最新的版本 61。
- 将JAVA_HOME设置为:
export JAVA_HOME=`/usr/libexec/java_home -v1.9`
在OSX上设置JAVA_HOME的更多详细信息在这里 。
- 验证版本:
~> java -version java version "1.9.0-ea" Java(TM) SE Runtime Environment (build 1.9.0-ea-b61) Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b61, mixed mode)
检出并安装jline2
jline2是一个用于处理控制台输入的Java库。 一探究竟:
workspaces> git clone git://github.com/jline/jline2.git
Cloning into 'jline2'...
remote: Counting objects: 6419, done.
remote: Total 6419 (delta 0), reused 0 (delta 0), pack-reused 6419
Receiving objects: 100% (6419/6419), 3.23 MiB | 80.00 KiB/s, done.
Resolving deltas: 100% (2945/2945), done.
Checking connectivity... done.
然后构建它:
jline2> mvn install
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/scm/maven-scm-provider-gitexe/1.8.1/maven-scm-provider-gitexe-1.8.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/scm/maven-scm-provider-gitexe/1.8.1/maven-scm-provider-gitexe-1.8.1.pom (3 KB at 0.3 KB/sec)
. . .
[INFO] Installing /Users/arungupta/workspaces/jline2/target/jline-2.13-SNAPSHOT-sources.jar to /Users/arungupta/.m2/repository/jline/jline/2.13-SNAPSHOT/jline-2.13-SNAPSHOT-sources.jar
[INFO] Installing /Users/arungupta/workspaces/jline2/target/jline-2.13-SNAPSHOT-tests.jar to /Users/arungupta/.m2/repository/jline/jline/2.13-SNAPSHOT/jline-2.13-SNAPSHOT-tests.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 09:52 min
[INFO] Finished at: 2015-05-06T19:23:14-07:00
[INFO] Final Memory: 25M/146M
[INFO] ------------------------------------------------------------------------
克隆并构建JDK 9 REPL
该项目的OpenJDK代号为Kulla,意为“建设者之神”。 该工具的计划名称为jshell
。
- 签出工作区:
workspaces> hg clone http://hg.openjdk.java.net/kulla/dev kulla requesting all changes adding changesets adding manifests adding file changes added 1453 changesets with 2226 changes to 172 files updating to branch default 109 files updated, 0 files merged, 0 files removed, 0 files unresolved
- 获取来源:
workspaces> cd kulla kulla> chmod +x ./get_source.sh kulla> ./get_source.sh # Repositories: corba jaxp jaxws langtools jdk hotspot nashorn corba: hg clone http://hg.openjdk.java.net/kulla/dev/corba corba jaxp: hg clone http://hg.openjdk.java.net/kulla/dev/jaxp jaxp corba: requesting all changes jaxp: requesting all changes corba: adding changesets jaxp: adding changesets corba: adding manifests jaxp: adding manifests corba: adding file changes jaxp: adding file changes corba: added 669 changesets with 4913 changes to 2576 files corba: updating to branch default corba: 1184 files updated, 0 files merged, 0 files removed, 0 files unresolved jaxws: hg clone http://hg.openjdk.java.net/kulla/dev/jaxws jaxws jaxws: requesting all changes jaxws: adding changesets jaxws: adding manifests jaxws: adding file changes jaxp: added 728 changesets with 10192 changes to 7393 files jaxp: updating to branch default jaxp: 3044 files updated, 0 files merged, 0 files removed, 0 files unresolved langtools: hg clone http://hg.openjdk.java.net/kulla/dev/langtools langtools langtools: requesting all changes langtools: adding changesets langtools: adding manifests jaxws: added 589 changesets with 20521 changes to 10746 files jaxws: updating to branch default jaxws: 3750 files updated, 0 files merged, 0 files removed, 0 files unresolved jdk: hg clone http://hg.openjdk.java.net/kulla/dev/jdk jdk langtools: adding file changes jdk: requesting all changes jdk: adding changesets jdk: adding manifests langtools: added 3173 changesets with 28112 changes to 9103 files langtools: updating to branch default langtools: 7100 files updated, 0 files merged, 0 files removed, 0 files unresolved hotspot: hg clone http://hg.openjdk.java.net/kulla/dev/hotspot hotspot hotspot: requesting all changes hotspot: adding changesets hotspot: adding manifests hotspot: adding file changes hotspot: added 8073 changesets with 45889 changes to 6290 files hotspot: updating to branch default hotspot: 5030 files updated, 0 files merged, 0 files removed, 0 files unresolved nashorn: hg clone http://hg.openjdk.java.net/kulla/dev/nashorn nashorn nashorn: requesting all changes nashorn: adding changesets nashorn: adding manifests jdk: adding file changes nashorn: adding file changes nashorn: added 1252 changesets with 11596 changes to 3595 files nashorn: updating to branch default nashorn: 2867 files updated, 0 files merged, 0 files removed, 0 files unresolved jdk: added 11805 changesets with 116593 changes to 42135 files jdk: updating to branch default jdk: 23192 files updated, 0 files merged, 0 files removed, 0 files unresolved # Repositories: . corba jaxp jaxws langtools jdk hotspot nashorn .: cd . && hg pull -u corba: cd corba && hg pull -u jaxp: cd jaxp && hg pull -u jaxws: cd jaxws && hg pull -u langtools: cd langtools && hg pull -u jdk: cd jdk && hg pull -u hotspot: cd hotspot && hg pull -u nashorn: cd nashorn && hg pull -u jaxws: pulling from http://hg.openjdk.java.net/kulla/dev/jaxws corba: pulling from http://hg.openjdk.java.net/kulla/dev/corba langtools: pulling from http://hg.openjdk.java.net/kulla/dev/langtools hotspot: pulling from http://hg.openjdk.java.net/kulla/dev/hotspot jdk: pulling from http://hg.openjdk.java.net/kulla/dev/jdk .: pulling from http://hg.openjdk.java.net/kulla/dev jaxp: pulling from http://hg.openjdk.java.net/kulla/dev/jaxp nashorn: pulling from http://hg.openjdk.java.net/kulla/dev/nashorn nashorn: searching for changes nashorn: no changes found jaxws: searching for changes jaxws: no changes found jaxp: searching for changes jaxp: no changes found corba: searching for changes corba: no changes found jdk: searching for changes hotspot: searching for changes hotspot: no changes found jdk: no changes found .: searching for changes .: no changes found langtools: searching for changes langtools: no changes found
- 编辑
langtools/repl/scripts/compile.sh
脚本,如下所示:#!/bin/sh JLINE2LIB=/Users/arungupta/workspaces/jline2/target/jline-2.13-SNAPSHOT.jar mkdir -p build javac -Xlint:unchecked -Xdiags:verbose -cp ${JLINE2LIB} -d build ../src/jdk.jshell/share/classes/*/*/*.java ../src/jdk.jshell/share/classes/*/*/*/*/*.java ../src/jdk.jshell/share/classes/*/*/*/*/*/*.java
注意,对于OSX,唯一的编辑是
#!/bin/sh
,并将JLINE2LIB
添加到先前编译的jline2
工作区的位置。 从引用JDK 9的JAVA_HOME中拾取javac
。 - 通过从
langtools/repl
目录调用脚本来编译REPL工具:repl> chmod +x ./scripts/compile.sh repl> ./scripts/compile.sh
运行JDK 9 REPL
- 编辑
langtools/repl/scripts/run.sh
脚本,如下所示:#!/bin/sh JLINE2LIB=/Users/arungupta/workspaces/jline2/target/jline-2.13-SNAPSHOT.jar java -ea -esa -cp build:${JLINE2LIB} jdk.internal.jshell.tool.JShellTool "$@"
注意,唯一的编辑是OSX的
!/bin/sh
并添加JLINE2LIB
。 - 将REPL运行为:
repl> ./scripts/run.sh | Welcome to JShell -- Version 0.428 | Type /help for help
JDK 9 REPL Hello World
与引入Java的弹跳球或跳舞Duke引入不同,我们只将常规的Hello World用于REPL
将“ Hello World”运行为:
-> System.out.println("Hello World");
Hello World
瞧!
没有public static void main
,没有类的创建,没有仪式,只是干净而简单的Java代码。 输入的文本称为“代码段”。
使用/list all
可以查看完整的Java代码,如下所示:
-> /list all
1 : import java.util.*;
2 : import java.io.*;
3 : import java.math.*;
4 : import java.net.*;
5 : import java.util.concurrent.*;
6 : import java.util.prefs.*;
7 : import java.util.regex.*;
8 : void printf(String format, Object... args) { System.out.printf(format, args); }
9 : System.out.println("Hello World");
此代码段可以另存为文件:
-> /save test
请注意,这不是Java文件。 保存的代码段正是输入的内容:
repl> more test
System.out.println("Hello World");
该工具可以退出为:
-> /exit
| Goodbye
或者您可以按Ctrl + C。
完整的命令列表很容易看到:
-> /help
Type a Java language expression, statement, or declaration.
Or type one of the following commands:
/l or /list [all] -- list the source you have typed
/seteditor <executable> -- set the external editor command to use
/e or /edit <name or id> -- edit a source entry referenced by name or id
/- or /drop <name or id> -- delete a source entry referenced by name or id
/s or /save [all|history] <file> -- save the source you have typed
/o or /open <file> -- open a file as source input
/v or /vars -- list the declared variables and their values
/m or /methods -- list the declared methods and their signatures
/c or /classes -- list the declared classes
/x or /exit -- exit the REPL
/r or /reset -- reset everything in the REPL
/f or /feedback <level> -- feedback information: off, concise, normal, verbose, default, or ?
/p or /prompt -- toggle display of a prompt
/cp or /classpath <path> -- add a path to the classpath
/h or /history -- history of what you have typed
/setstart <file> -- read file and set as the new start-up definitions
/savestart <file> -- save the default start-up definitions to the file
/? or /help -- this help message
Supported shortcuts include:
<tab> -- show possible completions for the current text
Shift-<tab> -- for current method or constructor invocation, show a synopsis of the method/constructor
JDK 9 REPL后续步骤和反馈
遵循REPL教程以了解有关该工具功能的更多信息。 快速概述:
- 接受Java语句,变量,方法和类的定义,导入和表达式
- 设置和显示信息的命令,例如
/list
显示代码片段列表,/vars
显示变量列表,/save
保存代码片段,/open
以读回它们。 - 片段的历史记录可用,片段可以按数字进行编辑,等等
这是一个有用的RFE:
- 将代码段导出为完整的Java类
随后的博客将展示如何将其用于Java EE应用程序的播放。 您将如何使用REPL?
在kulla-dev上讨论项目/问题。
请享用!
翻译自: https://www.javacodegeeks.com/2015/05/jdk-9-repl-getting-started.html