控制台查看java显示不存在,找不到控制台。如何获取我的JVM的控制台?

This is a follow-up question to this.

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).

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

So how can I get the console device, supposed to be represented by an object of the Console class?

For convenience, I am adding the code after I made the aforementioned silly changes to it to make it run:-

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.");

}

}

}

}

解决方案

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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值