xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Java REPL & JShell

Java 11

JShell

Java Shell

https://www.infoq.com/articles/jshell-java-repl/

The Java Shell or JShell is an official Read-Evaluate-Print-Loop, or REPL as it's commonly called, introduced with Java 9.
It provides an interactive shell for quickly prototyping, debugging, and learning Java and Java APIs, all without the need for a public static void main or the need to compile your code before executing it.
And as an added plus, using JShell just got a whole lot less verbose (and more practical) with the advent of the var keyword in Java 10!

Last login: Sat Aug  1 12:20:36 on ttys006
➜  ~ jshell
|  Welcome to JShell -- Version 11.0.2
|  For an introduction type: /help intro

jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|  	list the source you have typed
|  /edit <name or id>
|  	edit a source entry
|  /drop <name or id>
|  	delete a source entry
|  /save [-all|-history|-start] <file>
|  	Save snippet source to a file
|  /open <file>
|  	open a file as source input
|  /vars [<name or id>|-all|-start]
|  	list the declared variables and their values
|  /methods [<name or id>|-all|-start]
|  	list the declared methods and their signatures
|  /types [<name or id>|-all|-start]
|  	list the type declarations
|  /imports 
|  	list the imported items
|  /exit [<integer-expression-snippet>]
|  	exit the jshell tool
|  /env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ...
|  	view or change the evaluation context
|  /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]...
|  	reset the jshell tool
|  /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
|  	reset and replay relevant history -- current or previous (-restore)
|  /history [-all]
|  	history of what you have typed
|  /help [<command>|<subject>]
|  	get information about using the jshell tool
|  /set editor|start|feedback|mode|prompt|truncation|format ...
|  	set configuration information
|  /? [<command>|<subject>]
|  	get information about using the jshell tool
|  /! 
|  	rerun last snippet -- see /help rerun
|  /<id> 
|  	rerun snippets by ID or ID range -- see /help rerun
|  /-<n> 
|  	rerun n-th previous snippet -- see /help rerun
|  
|  For more information type '/help' followed by the name of a
|  command or a subject.
|  For example '/help /list' or '/help intro'.
|  
|  Subjects:
|  
|  intro
|  	an introduction to the jshell tool
|  id
|  	a description of snippet IDs and how use them
|  shortcuts
|  	a description of keystrokes for snippet and command completion,
|  	information access, and automatic code generation
|  context
|  	a description of the evaluation context options for /env /reload and /reset
|  rerun
|  	a description of ways to re-evaluate previously entered snippets

jshell> /exit
|  Goodbye

online REPL

OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)

https://java-repl.xgqfrms.repl.run

https://repl.it/@xgqfrms/java-repl#HelloWorld.java

 javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . HelloWorld.java Main.java
 java -classpath .:/run_dir/junit-4.12.jar:target/dependency/* Main
Hello world!

demos

⚠️ 创建文件 HelloWorld.java 的文件名必须要与类名保持一致

// HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

注:String args[] 与 String[] args 都可以执行,但推荐使用 String[] args,这样可以避免歧义和误读。

$ javac --version
# javac 11.0.2

$ javac HelloWorld.java
$ java HelloWorld
# Hello World

refs

https://www.runoob.com/java/java-tutorial.html


Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/13414149.html