JDK 9 REPL:入门

会议是聚会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命令,评估它们,打印输出并返回循环以执行相同的操作。

jdk9-repl

您可以在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

  1. 在撰写本文时,下载最新的版本 61。
  2. 将JAVA_HOME设置为:
    export JAVA_HOME=`/usr/libexec/java_home -v1.9`

    在OSX上设置JAVA_HOME的更多详细信息在这里 。

  3. 验证版本:
    ~> 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

  1. 签出工作区:
    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
  2. 获取来源:
    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 corbajaxp:   hg clone http://hg.openjdk.java.net/kulla/dev/jaxp jaxpcorba:   requesting all changesjaxp:   requesting all changescorba:   adding changesetsjaxp:   adding changesetscorba:   adding manifestsjaxp:   adding manifestscorba:   adding file changesjaxp:   adding file changescorba:   added 669 changesets with 4913 changes to 2576 filescorba:   updating to branch defaultcorba:   1184 files updated, 0 files merged, 0 files removed, 0 files unresolvedjaxws:   hg clone http://hg.openjdk.java.net/kulla/dev/jaxws jaxwsjaxws:   requesting all changesjaxws:   adding changesetsjaxws:   adding manifestsjaxws:   adding file changesjaxp:   added 728 changesets with 10192 changes to 7393 filesjaxp:   updating to branch defaultjaxp:   3044 files updated, 0 files merged, 0 files removed, 0 files unresolvedlangtools:   hg clone http://hg.openjdk.java.net/kulla/dev/langtools langtoolslangtools:   requesting all changeslangtools:   adding changesetslangtools:   adding manifestsjaxws:   added 589 changesets with 20521 changes to 10746 filesjaxws:   updating to branch defaultjaxws:   3750 files updated, 0 files merged, 0 files removed, 0 files unresolvedjdk:   hg clone http://hg.openjdk.java.net/kulla/dev/jdk jdklangtools:   adding file changesjdk:   requesting all changesjdk:   adding changesetsjdk:   adding manifestslangtools:   added 3173 changesets with 28112 changes to 9103 fileslangtools:   updating to branch defaultlangtools:   7100 files updated, 0 files merged, 0 files removed, 0 files unresolvedhotspot:   hg clone http://hg.openjdk.java.net/kulla/dev/hotspot hotspothotspot:   requesting all changeshotspot:   adding changesetshotspot:   adding manifestshotspot:   adding file changeshotspot:   added 8073 changesets with 45889 changes to 6290 fileshotspot:   updating to branch defaulthotspot:   5030 files updated, 0 files merged, 0 files removed, 0 files unresolvednashorn:   hg clone http://hg.openjdk.java.net/kulla/dev/nashorn nashornnashorn:   requesting all changesnashorn:   adding changesetsnashorn:   adding manifestsjdk:   adding file changesnashorn:   adding file changesnashorn:   added 1252 changesets with 11596 changes to 3595 filesnashorn:   updating to branch defaultnashorn:   2867 files updated, 0 files merged, 0 files removed, 0 files unresolvedjdk:   added 11805 changesets with 116593 changes to 42135 filesjdk:   updating to branch defaultjdk:   23192 files updated, 0 files merged, 0 files removed, 0 files unresolved
    # Repositories:  . corba jaxp jaxws langtools jdk hotspot nashorn .:   cd . && hg pull -ucorba:   cd corba && hg pull -ujaxp:   cd jaxp && hg pull -ujaxws:   cd jaxws && hg pull -ulangtools:   cd langtools && hg pull -ujdk:   cd jdk && hg pull -uhotspot:   cd hotspot && hg pull -unashorn:   cd nashorn && hg pull -ujaxws:   pulling from http://hg.openjdk.java.net/kulla/dev/jaxwscorba:   pulling from http://hg.openjdk.java.net/kulla/dev/corbalangtools:   pulling from http://hg.openjdk.java.net/kulla/dev/langtoolshotspot:   pulling from http://hg.openjdk.java.net/kulla/dev/hotspotjdk:   pulling from http://hg.openjdk.java.net/kulla/dev/jdk.:   pulling from http://hg.openjdk.java.net/kulla/devjaxp:   pulling from http://hg.openjdk.java.net/kulla/dev/jaxpnashorn:   pulling from http://hg.openjdk.java.net/kulla/dev/nashornnashorn:   searching for changesnashorn:   no changes foundjaxws:   searching for changesjaxws:   no changes foundjaxp:   searching for changesjaxp:   no changes foundcorba:   searching for changescorba:   no changes foundjdk:   searching for changeshotspot:   searching for changeshotspot:   no changes foundjdk:   no changes found.:   searching for changes.:   no changes foundlangtools:   searching for changeslangtools:   no changes found
  3. 编辑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

  4. 通过从langtools/repl目录调用脚本来编译REPL工具:
    repl> chmod +x ./scripts/compile.sh
    repl> ./scripts/compile.sh

运行JDK 9 REPL

  1. 编辑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

  2. 将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 all1 : 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 messageSupported 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教程以了解有关该工具功能的更多信息。 快速概述: keepcalm-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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/360259.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sinaapp mysql连接_手把手教你在新浪云上免费部署自己的网站--连接数据库

看完之后&#xff0c;默认你知道怎么将代码上传到新浪云SAE&#xff0c;并且能够成功运行&#xff0c;连接数据库之前&#xff0c;你必须先创建有一个应用。现在我创建一个名称为sampleone的应用&#xff0c;如下图点击左侧的代码管理&#xff0c;选在右侧创建一个版本然后就会…

HDU-4527 小明系列故事——玩转十滴水 模拟

题意&#xff1a;就是平时玩的十滴水游戏&#xff0c;游戏者拥有一定的水滴&#xff0c;能够滴在某些位置&#xff0c;如果一个点上的体积超过了4就会爆炸&#xff0c;向四周传递一个小水滴。该题就是要求模拟这个过程。 分析&#xff1a;这里有一个问题就是不能够使用递归来处…

win8改win7 教程

新入手的WIN8怎么改成WIN7呢&#xff1f;下面以戴尔Dell V3560 win8换win7系统为例说明&#xff1a; win8改win7本文转自http://jingyan.baidu.com/article/ed2a5d1f2a97ee09f6be17e2.html 第一步&#xff1a;开机过程中不断按F2进入到BIOS&#xff0c;更改bios设置&#xff0c…

7 centos 查看程序文件数量_解析CentOS 7中系统文件与目录管理

LINUXLinux操作系统解析CentOS 7中系统文件与目录管理Linux目录结构Linux目录结构是树形的目录结构根目录所有分区、目录、文件等的位置起点整个树形目录结构中&#xff0c;使用独立的一个“/”表示常见的子目录目录目录名称目录目录名称/root管理员家目录/bin所有用户可执行命…

IE浏览器模式设置

文件兼容性用于定义让IE如何编译你的网页。此文件解释文件兼容性&#xff0c;如何指定你网站的文件兼容性模式以及如何判断一个网页该使用的文件模式。 前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外观&#xff0c;IE8引入了文件兼容性。在IE6中引入一个增设的兼容…

mysql存储过程套嵌_mysql存储过程套嵌

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云数据库专家保驾护航&#xff0c;为用户…

拆卸invokedynamic

许多Java开发人员认为JDK的第七版有些令人失望。 从表面上看&#xff0c;仅少数语言和库扩展使它成为了发行版&#xff0c;即Project Coin和NIO2 。 但在幕后&#xff0c;该平台的第七个版本对JVM类型系统进行了最大的扩展&#xff0c;这是它最初发布后引入的。 添加invokedyna…

CRON

http://blog.csdn.net/tianlesoftware/article/details/5315039 1. 同时修改文件的修改时间和访问时间 touch -d "2010-05-31 08:10:30" test.doc 2. 只修改文件的修改时间 touch -m -d "2010-05-31 08:10:30" test.doc 3. 只修改文件的访问时间 touch -a …

mysql innodb_file_format_Innodb表压缩过程中遇到的坑(innodb_file_format)

对于越来越多的数据&#xff0c;数据库的容量越来越大&#xff0c;压缩也就越来越常见了。在我的实际工作中进行过多次压缩工作&#xff0c;也遇到多次问题&#xff0c;在此和大家分享一下。首先&#xff0c;我们先说说怎么使用innodb的压缩.第一&#xff0c;mysql的版本需要大…

java内存泄漏和内存溢出_Java和内存泄漏

java内存泄漏和内存溢出总览 术语“内存泄漏”在Java中的使用方式不同于在其他语言中使用的方式。 通用术语中的“内存泄漏”是什么意思&#xff0c;在Java中如何使用&#xff1f; 维基百科的定义 当计算机程序消耗内存但无法将其释放回操作系统时&#xff0c;就会发生计算机科…

JSP内置对象之WEB安全性及config对象

一、WEB-INF的安全性是最高的。 在Java EE的标准中&#xff0c;Web目录中的WEB-INF是必须存在的&#xff0c;而且此文件夹的安全性是最高的&#xff0c;在各个程序的开发中&#xff0c;基本上都将一些配置信息保存在此文件夹中。在定义WEB-INF目录时一定要注意大小写的问题&…

一行中的Java 8 Lambda

如果您了解这一行&#xff0c;或者更好的是仍然可以编写此代码&#xff0c;则可以说您已经了解了Java 8 Lambda的本质。 当然&#xff0c;它们可以与集合一起使用。 我在Peter Lawrey最近的一次演讲中发现了这一点 。 &#xff08;有空余时间绝对值得观看整个演示文稿。&#…

mysql事务的重复性读_Mysql下InnoDB的可重复读级别的事务测试

Mysql下InnoDB的可重复读的事务测试### 背景&#xff1a;* mysql版本&#xff1a;Server version: 5.1.71* 操作系统&#xff1a;CentOS 6.5 X64* 事务隔离级别&#xff1a;不可重复读### 实验前的准备&#xff1a;mysql> use test;mysql> create table test_table(id in…

python动态绘图并保留之前绘图_[转]基于Python实现matplotlib中动态更新图片(交互式绘图)...

最近在研究动态障碍物避障算法&#xff0c;在Python语言进行算法仿真时需要实时显示障碍物和运动物的当前位置和轨迹&#xff0c;利用Anaconda的Python打包集合&#xff0c;在Spyder中使用Python3.5语言和matplotlib实现路径的动态显示和交互式绘图(和Matlab功能类似)。Anacond…

一步一步学Silverlight 2系列(25):综合实例之Live Search

概述 Silverlight 2 Beta 1版本发布了&#xff0c;无论从Runtime还是Tools都给我们带来了很多的惊喜&#xff0c;如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython&#xff0c;对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlig…

gateway中的局部过滤器_Spring Cloud Gateway中的过滤器工厂:重试过滤器

Spring Cloud Gateway基于Spring Boot 2&#xff0c;是Spring Cloud的全新项目&#xff0c;该项目提供了一个构建在Spring 生态之上的API网关。本文基于的Spring Cloud版本为Finchley M9&#xff0c;Spring Cloud Gateway对应的版本为2.0.0.RC1。Spring Cloud Gateway入门一文介…

MySql命令行基本操作

启动mysql服务&#xff1a; net start mysql关闭mysql服务&#xff1a; net stop mysql命令行登陆mysql&#xff1a;mysql -h localhost -u root -p admin 命令行登陆mysql&#xff1a;mysql -uroot -padmin 退出mysql控制台&#xff1a;quit或者exit查看mysql控制台当前信息&a…

mysql 5.7 启动脚本_MySQL数据库 5.7 启动脚本

本文主要向大家介绍了MySQL数据库 5.7 启动脚本&#xff0c;通过具体的内容向大家展现&#xff0c;希望对大家学习MySQL数据库有所帮助。最近这段时间&#xff0c;在看mysql&#xff0c;安装了&#xff0c;也应用过&#xff0c;对于生产环境中&#xff0c;一般都选择使用source…

cocos2d-x for android配置 运行 Sample on Linux OS

1.从http://www.cocos2d-x.org/download下载稳定版 比如cocos2d-x-2.2 2.解压cocos2d-x-2.2.zip,比如本文将其解压到 /opt 目录下 3.运行 android-buildsetup.sh,运行之前需要先设置3个环境变量,如将以下变量写到文件 /etc/profile中 export ANDROID_SDK_ROOT/opt/android-sdk-…

转变馆藏

您是否曾经想替换过HashSet或HashMap使用的equals和hashCode方法&#xff1f; 或者有一个List的一些元素类型伪装成的List相关类型的&#xff1f; 转换集合使这成为可能&#xff0c;并且本文将展示如何实现。 总览 转换集合是LibFX 0.3.0的一项功能&#xff0c;该功能将在今天…