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

您可以在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教程以了解有关该工具功能的更多信息。 快速概述:

  • 接受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入门

jdk入门_JDK 9 REPL:入门相关推荐

  1. Scala从入门到入土(从入门到放弃)

    Scala从入门到入土(从入门到放弃) 1 Scala介绍 Scala是一门 综合了 面向对象的 函数式编程的 基于JVm的 语言 特点: 1 语法简洁 2 开发速度快 , 运行速度快 3 兼容 Ja ...

  2. python魔力手册-小白入门宝典:Python快速入门魔力手册 PDF 超清版

    给大家带来的一篇关于Python编程相关的电子书资源,介绍了关于小白入门.python入门.Python手册方面的内容,本书是由魔力手册出版,格式为PDF,资源大小11.8 MB,魔力手册编写,目前豆 ...

  3. python入门视频教程推荐-python入门学习哪个书比较好(python视频教程知乎)

    自学python的学习路线是什么?推荐一些python学习资源 第一段 初级,掌握Python的语法和常用库的使用 这里首先推雪锋在网上的书籍,这是Python2.7的,这本书适合于重头开始一直读完, ...

  4. python入门到实践-Python编程从入门到实践(基础入门)

    Python编程从入门到实践-------基础入门 1.Python中的变量 2.Python首字母大写使用title()方法,全部大写upper()方法,全部小写lower()方法 3.Python ...

  5. python游戏编程入门 免费-python游戏编程入门 python游戏编程入门课

    python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...

  6. LSTM入门必读:从入门基础到工作方式详解 By 机器之心2017年7月24日 12:57 长短期记忆(LSTM)是一种非常重要的神经网络技术,其在语音识别和自然语言处理等许多领域都得到了广泛的应用

    LSTM入门必读:从入门基础到工作方式详解 By 机器之心2017年7月24日 12:57 长短期记忆(LSTM)是一种非常重要的神经网络技术,其在语音识别和自然语言处理等许多领域都得到了广泛的应用. ...

  7. python入门教程傻瓜版_毫无基础的人如何入门 Python ?Python入门教程拿走不谢啦!...

    随着人工智能的发展,Python近两年也是大火,越来越多的人加入到Python学习大军,对于毫无基础的人该如何入门Python呢?这里整理了一些个人经验和Python入门教程供大家参考. 如果你是零基 ...

  8. python程序实例讲解_python入门编程实例 python入门编程实例解析

    python入门编程实例 python入门编程实例解析 python入门编程实例及解析: 题目:有1.2.3.4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十 ...

  9. python编程入门-最好的Python入门教材是哪本?

    更新: 有很多朋友私信问我Python学习有没有什么好的资料,我给大家找了几门不错的免费课,想学的可以看看. <Python入门教程>Python入门教程--Python视频教程-编程语言 ...

最新文章

  1. AI技术必备资源汇总
  2. 【CMake】Android Studio 中使用 CMake 编译单个 C++ 源文件 ( 常用的 CMake 命令解析 )
  3. Kubernetes安装之五:配置kubectl客户端
  4. 【Python】7种方案,彻底实现可视化图片大小/分辨率控制自由
  5. Halcon自定义函数封装方法(全网最详细)
  6. 关于VS2010帮助文档的使用和VC6.0在Win7 64位下的使用
  7. P3358 最长k可重区间集问题(网络流:串联思想)
  8. Loj #6089. 小 Y 的背包计数问题
  9. 洛谷 1087——FBI树
  10. linux驱动日志格式,( 转)嵌入式Linux驱动Makefile
  11. (0.2)linux下Mysql的安装配置与管理入门(目录篇)
  12. python 爬虫 scrapy1_官网教程
  13. python 后台系统 源码_这是我见过最好的博客系统!附源码(前端、后台、APP、小程序都有)...
  14. ppt格式刷快捷键_15个PPT神操作,让老师做课件的效率翻倍!
  15. 使用luac编译lua脚本文件[适用于Windows系统]
  16. 计算机的应届毕业生简历上可以写什么,计算机应届生简历范文计算机专业应届毕业生简历写.doc...
  17. 致加西亚的信 名言佳句
  18. 微机原理、汇编语言与接口技术(韩晓茹)课后答案
  19. Visual SourceSafe如何支持并行开发
  20. Self-paced and auto-weighted multi-view clustering 阅读笔记

热门文章

  1. P1446-[HNOI2008]Cards【Burnside引理,dp】
  2. jzoj6824-[2020.10.17提高组模拟]英雄联盟【期望】
  3. P5127-子异和【线段树,树链剖分,位运算】
  4. USACO2.2【统计,dp,模拟,位运算】
  5. CF1088F Ehab and a weird weight formula(树上最优性问题、贪心+倍增)
  6. 跟我学 Java 8 新特性之 Stream 流(四)并行流
  7. Java的GC机制及算法
  8. 漫画:什么是中间人攻击
  9. Java List面试题汇总
  10. RabbitMQ(mall学习)