Results 1 to 7 of 7
  1. #1
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Console in NetBeans

    Hello,

    I'm using NetBeans 6.7.1 and trying to use this piece of code:

    Console c = System.console();
    if (c == null) {
    System.err.println("No console.");
    System.exit(1);
    }
    String str = c.readLine("Enter line: ");

    to read a line from the console, but I always get null for the console. It seems that the io.Console cannot be used from NetBeams.

    I also found this in the class doc (java.sun.com/javase/6/docs/api/java/io/Console.html):

    "Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console. "

    It seems that it has somenting to do with how the JVM was started, but I run the file from a command line an it works perfectly...

    Is there any way to use this Console from NetBeans?

    Anaconda.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Personally, I hate the console class for the problems you specified; I avoid it. Also, it breaks compatibility with java 1.5 which is still popular!

    Someone might have a brilliant and simple solution for your problem, but personally I would just avoid using it. As far as I know, Console doesn't do anything that can't be done without Console. Good luck!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Did you read the documentation? Do you have the idea what's the meaning of Console?

    On IDE you cannot see the console, because IDE use it's own panel. Write your code in a text file and run it accordingly. Yes, as quad64bit said, Console class is a mess.

  4. #4
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Of curse the program runs fine from the command line, the trick would be to use it in NetBeans...
    Yes, NetBeans has its own panel, and you can use it in this way to read a line:

    InputStreamReader inputStreamReader = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(inputStreamReader);
    System.out.println("Type the line:");
    String line = reader.readLine();

    However, it would be nice to use the other way. The question that I am asking is not clear from the documentation I found. I guess that's why I am asking it in this forum....

    Eranga, could you be more specific? Do you mean that the NetBean panel cannot be used as a console, as specified in the console definition? Do you know the reason?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Anaconda View Post
    Eranga, could you be more specific? Do you mean that the NetBean panel cannot be used as a console, as specified in the console definition? Do you know the reason?
    Fine, we'll do it in this way.

    You are working on NetBeans, so I guess that you know how to create jar package for the application. So create a simple java application, create jar package and run it and see. A simple example to to print a text line is fine.

  6. #6
    Anaconda is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    It works, but still cannot use the System.console to read lines...

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    It works means you can see the console/ command prompt isn't it? When you run the code from the IDE, each IDEs have there own console to execute the code. IDE is not execute external applications. I guess it's much better.

    Use of System.console is another thing. As quad64bit says console gives lots of pros/cons. Can you show your code here.

Similar Threads

  1. How Do You?? Get the Console Output as a GUI??
    By Lyricid in forum AWT / Swing
    Replies: 10
    Last Post: 11-20-2009, 11:35 PM
  2. how to take input from console in jsp
    By veena in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 04:39 AM
  3. ModSecurity Console 1.0.3
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-15-2008, 06:44 PM
  4. Console doesn't appear!
    By PeteMarsh in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 05:41 PM
  5. Help with java console
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •