当应用程序通过 gradle run 启动时,java.util.scanner 抛出 NoSuchElementException

2024-03-19

我创建了一个简单的 java“echo”应用程序,它接受用户的输入并将其显示给他们以演示问题。我可以使用 IntelliJ 的内部“运行”命令毫无问题地运行该应用程序,并且在执行由gradle build。但是,如果我尝试使用执行应用程序gradle run,我收到扫描仪抛出的 NoSuchElementException 异常。

我认为 gradle 或应用程序插件特别对系统 IO 做了一些奇怪的事情。

应用

package org.gradle.example.simple;

import java.util.Scanner;

public class HelloWorld {
  public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    String response = input.nextLine();
    System.out.println(response);
  }
}

构建.gradle

apply plugin: 'java'
version '1.0-SNAPSHOT'

apply plugin: 'java'

jar {
    manifest {
        attributes 'Main-Class': 'org.gradle.example.simple.HelloWorld'
    }
}

apply plugin: 'application'

mainClassName = "org.gradle.example.simple.HelloWorld"

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

任何想法如何使这个应用程序使用gradle run?


您必须将默认标准输入连接到 gradle,将其放入 build.gradle 中:

run {
    standardInput = System.in
}

更新:2021 年 9 月 9 日

正如建议的nickbdyer在评论中运行gradlew run with --console plain避免所有这些嘈杂和烦人的提示的选项

Example

gradlew --console plain run

如果您还想完全摆脱所有 gradle 任务日志,请添加-q option

Example

gradlew -q --console plain run
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当应用程序通过 gradle run 启动时,java.util.scanner 抛出 NoSuchElementException 的相关文章

随机推荐