如何在jshell脚本中获取用户输入?

时间:2018-01-02 15:16:15

标签: java-9 jshell

问题:

如何在 jshell脚本中获取用户输入?或者我做错了什么?

注意:正在寻找how to pass arguments to jshell script

实施例

例如脚本hello.java

Scanner in = new Scanner(System.in);

System.out.print("Enter number n1: ");
int n1 = in.nextInt();
System.out.print("Enter number n2: ");
int n2 = in.nextInt();

System.out.println("n1 + n2 = "+ (n1 +n2));

/exit

如果我在jshell中逐行输入,它会起作用,但是我运行jshell hello.java它没有。抛出java.util.NoSuchElementException

我得到的输出:

@myMint ~/Java $ jshell hello.java 
Enter number n1: |  java.util.NoSuchElementException thrown: 
|        at Scanner.throwFor (Scanner.java:858)
|        at Scanner.next (Scanner.java:1497)
|        at Scanner.nextInt (Scanner.java:2161)
|        at Scanner.nextInt (Scanner.java:2115)
|        at (#3:1)
Enter number n2: |  java.util.NoSuchElementException thrown: 
|        at Scanner.throwFor (Scanner.java:858)
|        at Scanner.next (Scanner.java:1497)
|        at Scanner.nextInt (Scanner.java:2161)
|        at Scanner.nextInt (Scanner.java:2115)
|        at (#5:1)
n1 + n2 = 0

我的系统: Linux Mint 18.2(x64),JShell Version 9.0.1

3 个答案:

答案 0 :(得分:2)

在JDK11中,您可以直接执行Java源文件:

$java Script.java

请参见Launch Single-File Source-Code Programs

答案 1 :(得分:1)

您可以解决此问题,但不能直接使用基于JShell的代码。

此项目jshell_script_executorhttps://github.com/kotari4u/jshell_script_executor

您可以下载JShellScriptExecutor.java

内的小修改

try(JShell jshell = JShell.create()){

// This call will map System.in in your main code
// to System.in inside JShell evaluated code
try(JShell jshell =
  JShell.builder()
    .in(System.in)
    .out(System.out)
    .err(System.err)
    .build()){

和(还)对代码的小修改(我知道这不是你想要的 - 我们不在这里使用Scanner):

/* Put this code into file.jshell */
import java.io.*;

InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int n1;
System.out.print("Enter the number: ");
n1 = Integer.parseInt(in.readLine());

int n2;
System.out.print("Enter the number: ");
n2 = Integer.parseInt(in.readLine());

System.out.println("n1 + n2 = " + (n1 + n2));

你可以让它运行:

> javac src/main/java/com/sai/jshell/extension/JShellScriptExecutor.java
> java -cp src/main/java com.sai.jshell.extension.JShellScriptExecutor ./file.jshell
Enter the number: 1
Enter the number: 2
n1 + n2 = 3

嗯......实际上它也适用于您的代码 - 稍加修改:

/* Put this code into file_scanner.java */
import java.util.Scanner;

Scanner in = new Scanner(System.in);

System.out.print("Enter number n1: ");
int n1 = in.nextInt();
System.out.print("Enter number n2: ");
int n2 = in.nextInt();

System.out.println("n1 + n2 = "+ (n1 +n2));

并尝试一下

> java -cp src/main/java com.sai.jshell.extension.JShellScriptExecutor ./file_scanner.java
Enter number n1: 1
Enter number n2: 2
n1 + n2 = 3

答案 2 :(得分:0)

默认情况下,jshell将执行委派给远程VM。如果您通过--execution local,它将使用相同的VM进程,该进程将按预期提供System.in的实例。根据您的问题量身定制,以下调用可以解决问题:

jshell --execution local hello.java

通过jshell --help-extra查看详细信息,或在https://docs.oracle.com/en/java/javase/11/docs/api/jdk.jshell/module-summary.html浏览API文档