0

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)
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • 1
    Are you running in eclipse? Which Java-Version du you use? Maybe that is a [bug](https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429) when using java 1.6 – Jens Jun 01 '14 at 14:45
  • @Jens hello jens, am using netbeans 7.4 and my java version is 7 update 55. please help me solve my problem – Edijae Crusar Jun 01 '14 at 15:07
  • @David hello sir, its not a duplicate of that question because the question is about console returning null in eclipse and mine is console returning null in netbeans 7.4. my java version is 7 update 55. please help! – Edijae Crusar Jun 01 '14 at 15:09
  • @gikarasojokinene have you tried to run from command line? – Jens Jun 01 '14 at 15:14
  • @jens i have tried running my programs in cmd but keep on getting "NoClassDefFoundError". i have set the path to my JDK tools e.g javac successfully but the problem is setting the CLASSPATH. i really don't know where my CLASSPATH is. i have tried setting it to my program folder(C:\Users\user\Documents\NetBeansProjects\regexTest\build\classes\regextest) and even to the "lib" folder which is contained in the C:\Program Files (x86)\Java\jdk1.7.0_60\lib but still gets the error.please help. – Edijae Crusar Jun 01 '14 at 16:16
  • @gikarasojokinene You must include the folder with your class file to the classpath. – Jens Jun 01 '14 at 16:50
  • @Jens the folder with my class file is C:\Users\user\Documents\NetBeansProjects\regexTest\build\classes\regextest which contains regextext.class. so,is this the value of my classpath? – Edijae Crusar Jun 01 '14 at 17:10
  • @gikarasojokinene Is regextest you package? If yes the classpath is `C:\Users\user\Documents\NetBeansProjects\regexTest\build\classes\` – Jens Jun 01 '14 at 17:16

1 Answers1

1

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.

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

}

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

enter image description here

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

enter image description here

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • so you mean there is no way i can use System.console?. by the way, did you run your program in cmd? and if so how did you do it. i have tried to run in cmd but keep on getting "noClassDefFoundError" when i reach the part of typing e.g java Trying(lets say Typing is my program name). (i think the problem is in defining my classpath but am arent able to do so. i will be thankful if you show me) Am learning how to use regex in java using java tutorials and now if console is not working, what else gonna i use to successfully run my program above? please help sir. – Edijae Crusar Jun 01 '14 at 16:06
  • @gikarasojokinene No, you shouldn't rely on `System.console()` if you are using IDE like NetBeans. Why don't you use `java.util.Scanner` to read input from console? You can just wrap `System.in` with Scanner using `Scanner keybord = new Scanner(System.in);` and to read data from user use `String line = keybord.nextLine();`. If you want to print something in console use just `System.out.println("your massage")` (you can use `print` instead of `println` if you don't want to add line separator after your massage). This should let you use your IDE without any problems. – Pshemo Jun 01 '14 at 16:18
  • thanks alot, i will try. what about my problem in using cmd? how can i set the CLASSPATH? correctly coz i think that's the reason for NoClassDefFoundError. – Edijae Crusar Jun 01 '14 at 17:05
  • Yes that is probably the reason. You should provide in classpath `-cp` parameter location of top package of your project. Also when you run you application you need to provide full.package.name.of.YourClass for instance `java -cp location/of/your/packages foo.bar.MyClass`. – Pshemo Jun 01 '14 at 17:24
  • What do you mean by problem with cmd? If "it worked" then it means that you used correct classpath with full.package.name.of.YourClass. What problem you are having now? – Pshemo Jun 01 '14 at 17:48
  • ooh am sorry my problem is that my program still gives me NoClassDefFoundError when i try to run it from c.m.d. i have gone to windows system and security, clicked on advanced system settings,clicked on enviromental variables, clicked on new, in the variables textfield i typed "classpath", and in the variables value, i typed this "C:\Users\user\Desktop\my properties\src\my\properties" since this is where my .class file is located(lets say job.class). but when i open my c.m.d and try to run java job(my java application name/class name). where do you think my problem is? – Edijae Crusar Jun 01 '14 at 18:18
  • @gikarasojokinene If you have another question about NoClassDefFoundError then you should create separate post about it. But before you do read tutorial about running and compiling Java code. Also read questions about this problem first. – Pshemo Jun 09 '14 at 14:23