找不到控制台。如何获取我的JVM的控制台? [英] No Console found. How to get the console for my JVM?

查看:343
本文介绍了找不到控制台。如何获取我的JVM的控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是后续问题,

我昨天问过这个问题,解决了,我试图对代码进行一些愚蠢的更改只是让它编译一次( console.format()语句替换 System.out .print 语句,并将 readLine()方法的第二个参数添加 null )。

I asked this question yesterday, and although it is not resolved yet, I tried to make some silly changes to the code to just make it compile once (replacing console.format() statements by System.out.print statements, and adding null as the second argument to the readLine() methods).

幸运的是,代码已经运行,但它输出没有控制台。(显然因为 $ c> JVM 没有控制台设备。 参考

Luckily the code did run, but it prints No console. (obviously because the JVM does not have a console device. Reference)

那么如何获得控制台设备,控制台类?

为方便起见,我在添加代码后,愚蠢地更改它以使其运行: -

import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/*
 *  Enter your regex: foo
 *  Enter input string to search: foo
 *  I found the text foo starting at index 0 and ending at index 3.
 * */

public class RegexTestHarness {

    public static void main(String[] args){
        Console console = System.console();
        if (console == null) {
            System.err.println("No console.");
            System.exit(1);
        }
        while (true) {

            Pattern pattern = 
            Pattern.compile(console.readLine("%nEnter your regex: ", null));

            Matcher matcher = 
            pattern.matcher(console.readLine("Enter input string to search: ", null));

            boolean found = false;
            while (matcher.find()) {
                /*console.format("I found the text" +
                    " \"%s\" starting at " +
                    "index %d and ending at index %d.%n",
                    matcher.group(),
                    matcher.start(),
                    matcher.end());*/

                System.out.println("I found the text " + matcher.group() + " starting at index " + matcher.start() + " and ending at index " + matcher.end() + "."); 

                found = true;
            }
            if(!found){
                //console.format("No match found.%n", null);
                System.out.println("No match found."); 
            }
        }
    }
}


推荐答案

您的应用程序将只有一个控制台设备,如果它是从终端启动,没有stdin或stdout重定向。
基本上,函数isatty()必须返回true。 Windows应用程序通常没有控制台,如果他们从explorer启动。您应该从命令提示符(cmd.exe)启动它们。

Your application will only have a console device to open if it was started from a terminal and does not have stdin or stdout redirection. Basically the function isatty() would have to return true. Windows application typically do not have consoles if they are started from explorer. You should start them from the command prompt (cmd.exe).

这篇关于找不到控制台。如何获取我的JVM的控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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