问题描述
我创建了一个简单的javaecho应用程序,它接受用户的输入并将其显示给他们以证明问题。我可以毫无困难地使用IntelliJ的内部运行命令来运行这个应用程序,而且在执行由 gradle build
生成的编译java文件时也可以运行此应用程序。但是,如果我尝试使用 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);
}
}
build.gradle
apply plugin:'java'
version'1.0-SNAPSHOT'
apply plugin: 'java'
$ b $ jar {
manifest {
属性'Main-Class':'org.gradle.example.simple.HelloWorld'
}
}
apply插件:'application'
mainClassName =org.gradle.example.simple.HelloWorld
sourceCompatibility = 1.5
存储库{
mavenCentral()
}
依赖关系{
testCompile组:'junit',名称:'junit',版本:'4.11 '
}
如何使这个应用程序使用 gradle run
?
你必须连接默认的标准输入到gradle,把它放在build.gradle :
运行{
standardInput = System.in
}
I have a created a simple java "echo" application that takes a user's input and shows it back to them to demonstrate the issue. I can run this application without trouble using IntelliJ's internal "run" command, and also when executing the compiled java file produced by gradle build
. However, if I try to execute the application using gradle run
, I get a NoSuchElementException thrown from the scanner.
I think gradle or the application plugin specifically is doing something strange with the system IO.
Application
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);
}
}
build.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'
}
Any ideas how to make this application work using gradle run
?
You must wire up default stdin to gradle, put this in build.gradle:
run{
standardInput = System.in
}
这篇关于当使用Gradle运行应用程序时,java.util.scanner抛出NoSuchElementException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!