我的java控制台不工作 [英] my java console is not working

查看:167
本文介绍了我的java控制台不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常严重的问题,使用我的java控制台。我已经在java程序中启用显示控制台,它位于开始>控制面板>程序> java。
我双击它打开,进入高级并启用显示控制台。
当我尝试运行此程序,我得到的错误消息没有控制台!请帮助!! 提前感谢。

i have a very serious problem with working with my java console. i have already enabled "show console" in java program which is located at start>control panel>programs>java. i double clicked it to open, went to advanced and enabled show console. BUT when i try to run this program, I'm getting the error message no console!. please help!! thanks in advance.

    public class RegexTest {

    public static void main(String[] args) {
        // TODO code application logic here
        Console console=System.console();

        if(console==null){
            System.err.println("no console");
            System.exit(1);
        }
        while(true){
            Pattern pattern=Pattern.compile(console.readLine("enter your regex: "));
            Matcher matcher=pattern.matcher(console.readLine("enter inputstring to serch"));
            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());
                found=true;
            }
            if(!found){
                console.format("no match found %n");
            }
        }
    }
}



是输出。

here is the output.

no console
Java Result: 1
BUILD SUCCESSFUL (total time: 13 seconds)


推荐答案

运行代码时避免创建新的操作系统控制台窗口大多数IDE如Eclipse,NetBeans,InteliiJ使用 javaw.exe (无窗口)而不是 java.exe

To avoid creating new OS console window when running code most IDEs like Eclipse, NetBeans, InteliiJ is using javaw.exe (window-less) instead of java.exe.

由于 javaw.exe 不创建控制台窗口,因此没有要打印的控制台 System.console()返回 null

Since javaw.exe doesn't create console window there is no console on which you would want to print so System.console() returns null.

这个例子可能更容易理解我的意思。

This example may be easier to understand what I mean.

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Demo {

    public static void main(String[] args) throws Exception {

        JFrame frame = new JFrame("Hello");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.setSize(250, 100);

        frame.add(new JLabel("HELLO WORLD"));
        frame.add(new JLabel("Is console available? " + (System.console() != null)));
        frame.setVisible(true);

        System.out.println("text in console");
    }

}



如果您使用 java -cp pathToYourPackage Demo 您将看到两个窗口:

If you run this code using java -cp pathToYourPackage Demo you will see two windows:

但如果您使用 javaw -cp pathToYourPackage Demo 您将看不到控制台窗口(这是大多数IDE使用它的原因),但只有

But if you use javaw -cp pathToYourPackage Demo you will not see console window (that is why most IDEs use it) but only

这篇关于我的java控制台不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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