jdk入门_JDK 9 REPL:入门

jdk入门

会议是聚会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

jdk入门

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

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

相关文章

c语言中局部变量是什么

点击上方蓝字关注我&#xff0c;了解更多咨询1、函数内部定义的变量称为局部变量&#xff0c;其作用域仅限于函数内部&#xff0c;离开函数后无效&#xff0c;使用后报错。2、局部变量只能在函数内部使用&#xff0c;离开函数后无效&#xff0c;再次使用会报错。实例#include &…

java接口自动化Excel占位符_基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport的接口自动化测试框架...

接口自动化框架项目说明本框架是一套基于mavenjavaTestNGhttpclientpoijsonpathExtentReport而设计的数据驱动接口自动化测试框架&#xff0c;TestNG 作为执行器&#xff0c;poi用于读取存放于excel的接口用例&#xff0c;jsonPath用于校验返回值&#xff0c;以及提取返回值。本…

本地构建和自动化构建_构建自动化面板

本地构建和自动化构建上周二&#xff0c;我作为持续讨论&#xff08;&#xff03;c9d9&#xff09;的一部分&#xff0c;参加了一个关于Build Automation主题的在线讨论会&#xff0c;这是一系列有关敏捷&#xff0c;持续交付和DevOps的社区讨论会。 自动化构建流程面临许多挑战…

C语言为什么要定义short,int,long这么多整数类型?

点击上方蓝字关注我&#xff0c;了解更多咨询整数类型有int、short int、long int三种类型&#xff0c;用于需要不同存储空间的整数使用。整数类型有正整数和负整数之分&#xff0c;在C语言中&#xff0c;规定整型的最高位为符号位&#xff0c;最高位为“0”表示正数&#xff0…

java中逗号怎么加_Java中如何将字符串从右至左每三位加一逗号

/*** * 将字符串从右至左每三位加一逗号* ** param str 需要加逗号的字符串* return 以从右至左每隔3位加一逗号显示*/public static String displayWithComma(String str){str new StringBuffer(str).reverse().toString(); // 先将字符串颠倒顺序String str2 "";…

小度拆卸_拆卸invokedynamic

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

c语言中函数参数类型的探究

点击上方蓝字关注我&#xff0c;了解更多咨询函数中的参数个数可以是0&#xff0c;也可以是一个或多个参数。下面我们带着这三种不同的情况&#xff0c;分别在c语言中进行讨论。1、函数没有参数&#xff0c;表示没有参数列表。int func1(); //声明一个函数&#xff0c;该函数…

java rotate怎么用_jQuery旋转插件jqueryrotate用法详解

本文实例讲述了jQuery旋转插件jqueryrotate用法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;CSS3 提供了多种变形效果&#xff0c;比如矩阵变形、位移、缩放、旋转和倾斜等等&#xff0c;让页面更加生动活泼有趣&#xff0c;不再一动不动。然后 IE10 以下版本的浏览…

c语言野指针产生的原因

点击上方蓝字关注我&#xff0c;了解更多咨询1、指针变量未初始化&#xff0c;任何指针变量刚被创建时不会自动成为 NULL 指针&#xff0c;它的缺省值是随机的。所以&#xff0c;指针变量在创建的同时应当被初始化&#xff0c;要么将指针设置为 NULL &#xff0c;要么让它指向合…

java rhino_在Java 8中使用Rhino

java rhinoJava将Nashorn作为JSR 223的新JavaScript实现&#xff08;javax.scripting&#xff09;。 尽管这无疑是个好消息&#xff08;通过直接生成Java代码&#xff0c;Nashorn比Rhino快得多&#xff09;&#xff0c;但它也带来了一些挑战&#xff1a; Nashorn与Rhino并非10…

docx 图片预处理 Java_如何在java中将docx文件头图像复制到另一个docx文件中

尝试(未经测试的):void attachHeader(HeaderPart sourcePart, WordprocessingMLPackage targetPkg) throws Docx4JException {HeaderPart newHeaderPart new HeaderPart();newHeaderPart.setContents(XmlUtils.deepCopy(sourcePart.getContents()));if (sourcePart.getRelatio…

c语言中assert函数的使用注意

点击上方蓝字关注我&#xff0c;了解更多咨询1、使用assert检查函数开始时传入参数的合法性。2、每个assert只检验一个条件&#xff0c;因为在同时检验多个条件时&#xff0c;如果断言失败&#xff0c;就无法直观地判断哪个条件失败。实例int resetBufferSize(int nNewSize) {/…

es 调整gc_实际中进行GC调整

es 调整gc调优垃圾回收与任何其他性能调优活动没有什么不同。 您需要确保您了解当前的情况和所需的结果&#xff0c;而不是因为对应用程序的随机部分进行调整而产生了诱惑。 通常&#xff0c;只需执行以下过程即可&#xff1a; 陈述您的绩效目标 运行测试 测量 与目标比较…

game connect4 java_为我的connect 4数学游戏创建一个积分系统

我试着在每次用户得到正确答案时加一分&#xff0c;如果在我的“连接4”数学游戏中答错了&#xff0c;我就拿走一分。在游戏中&#xff0c;用户把一个计数器放在想要的地方&#xff0c;问题就会弹出&#xff0c;等待答案&#xff0c;以便继续。这段代码中的所有内容都能正常工作…

内核中断处理流程_处理中断

内核中断处理流程我只是在观看Heinz Kabutz的VJUG采访 &#xff0c;这启发了我写一篇有关中断的文章。 顺便说一句&#xff0c;我建议您订阅VJUG YouTube频道 -确实非常有用。 Heinz始终是物有所值的&#xff0c;很难不学习很多东西就很难观看他的演讲。 他提出了如何处理Inte…

c语言中assert函数是什么

点击上方蓝字关注我&#xff0c;了解更多咨询1、assert将通过检查表达式expresion的值来决定是否需要终止执行程序。2、如果表达式expresion的值是假的(即0)&#xff0c;它将首先向标准错误流stderr打印错误信息。通过调用abort函数终止程序运行。否则&#xff0c;assert没有效…

java怎么清空一个文件内容_如何从文本文件中删除特定内容? - java

我正在Java的SO的帮助下从事此项目的工作&#xff0c;正在读取一个文件夹&#xff0c;然后将其内容写入文件。然后&#xff0c;我需要浏览该内容&#xff0c;仅保留末尾带有Thumbnail.jpg的图像。编辑&#xff1a;public static final File outFile new File(System.getProper…

sci检索没有馆藏号_转变馆藏

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

c语言中exit和return的区别

点击上方蓝字关注我&#xff0c;了解更多咨询1、返回函数值的return是关键字&#xff0c;exit是一个函数。return是语言级的&#xff0c;它表示调用堆栈的返回&#xff1b;exit是系统调用级的&#xff0c;它表示一个过程的结束。2、return是函数的退出&#xff0c;exit是进程的…

java怎样返回json_java怎么返回json

详细内容本篇文章将介绍如何编写Java程序来返回Json数据&#xff0c;本次以三个方式进行介绍。推荐课程&#xff1a;Java教程&#xff0c;方式1&#xff1a;当然是手敲所有代码&#xff0c;来进行json数据的返回。需要 HttpHttpServletRequest request HttpServletResponse res…