gpt4 book ai didi

java - Gradle 不允许通过 Windows 命令提示符输入

转载 作者:行者123 更新时间:2023-12-01 09:48:45 25 4
gpt4 key购买 nike

在安装了 JDK 的 shell 服务器上,以下程序通过读取下一个整数并显示正确的输出来允许用户输入:

package hello;
import java.util.Scanner;

public class HelloWorld {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int theNextInt = in.nextInt();
System.out.println("Next integer is: " + theNextInt);
}
}

但是,当我在 Windows 控制台上执行 gradlew run 时,出现以下错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at hello.HelloWorld.main(HelloWorld.java:12)
:run FAILED

当然,发生异常是因为 Scanner 没有读取更多元素。 那是因为 Windows 控制台不允许我输入。有办法解决这个问题吗?

最佳答案

我明白了。事实证明,经过进一步研究,我遇到了this questionthis question提供相同的解决方案(有效):

build.gradle 文件中包含以下内容:

run {

standardInput = System.in

}

因为根据the docs ,对于 JavaExec 类型的任务,InputStream 默认为空。

关于java - Gradle 不允许通过 Windows 命令提示符输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com