在IntelliJ IDEA和Gradle上进行调试时等待用户输入 [英] Wait for user input while debugging on IntelliJ IDEA and Gradle

查看:643
本文介绍了在IntelliJ IDEA和Gradle上进行调试时等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有最简单的Java应用程序,只要从命令行执行就可以工作。但是如果我想通过IntelliJ IDEA 14 Ultimate进行调试,那么 System.in.read()部分总是返回 -1

  import java.io.IOException; 

public class Main {
public static void main(String [] args)throws IOException {
System.out.println(This is a test ... Hit [enter ] 退出。);
int cha = System.in.read();
System.out.println(你点击[enter],退出...);




$ b $ p
$ b这是Windows 8.1的某种问题,它与IntelliJ IDEA相关?



更新:我发现只有在通过Gradle启动应用程序时才会出现问题( gradlew run ),所以这是一个Gradle问题。这是我的 build.gradle

  apply plugin:'java' 
apply plugin:'application'

sourceCompatibility = 1.7
targetCompatibility = 1.7
version ='1.0'
mainClassName ='net.frakbot.ws。 Main'

仓库{
mavenCentral()
}

运行{
main ='net.frakbot.ws.Main'
standardInput = System.in
}

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


解决方案

It is has been Intellij Idea中的一个已知错误:错误描述



编辑:显然,正如评论中指出的那样,自从给出这个答案并修补后,它已经被修复了。Intellij将在2017年夏季发布。


I have the simplest Java application that just works if executed from command line. But if I want to debug it through IntelliJ IDEA 14 Ultimate, the System.in.read() part always returns -1, without ever typing anything into it:

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        System.out.println("This is a test... Hit [enter] to exit.");
        int cha = System.in.read();
        System.out.println("You hit [enter], exiting...");
    }
}

Is this some kind of issue with Windows 8.1 or is it IntelliJ IDEA related?

UPDATE: I have found out the issue presents itself only when starting the application through Gradle (gradlew run), so it's a Gradle issue. This is my build.gradle:

apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
mainClassName = 'net.frakbot.ws.Main'

repositories {
    mavenCentral()
}

run {
    main = 'net.frakbot.ws.Main'
    standardInput = System.in
}

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

解决方案

It is has been a known bug in Intellij Idea: bug description

EDIT: Apparently, as pointed out in the comments, it has been fixed since this answer was given and patched Intellij will be released in Summer 2017.

这篇关于在IntelliJ IDEA和Gradle上进行调试时等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆