We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,965 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to get a console using System.console() ?

I am trying to get a Console using System.console() method,
but it returns null. Please let me know how to get a Console object?
Is their other criteria need to be satisfied?

3
Contributors
6
Replies
1 Day
Discussion Span
2 Years Ago
Last Updated
9
Views
java_programmer
Junior Poster
125 posts since May 2006
Reputation Points: 10
Solved Threads: 18
Skill Endorsements: 0

Did you read the API doc? It says:

Returns the unique Console object associated with the current Java virtual machine, if any.

I assume the "if any" means there might not be a console.

Why do you think there is a console for the JVM?
There wouldn't be if the code were executed with the javaw command.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Do you mean you try to get another windonw ? Did you use javaw on DOS? If so there would be no console available so that no printed output on screen. But how do you know the System.console() returns null?
The standard method to use console() method (starting from 1.6)is shown as follows:

import java.io.Console;           
public class TestConsole {            
     public static void main(String[] args) {
     	Console console = System.console(); // obtain an instance of Console
     	if (console != null) {              // make sure if you may use the console: if console is available                 
     	String user = new String(console.readLine("Enter username:"));      // read a line of characters                  
     	String pwd = new String(console.readPassword("Enter passowrd:"));   // read a password whti nothing printed on screen                  
     	console.printf("Username is: " + user + "\n");      // print username
     	console.printf("Password is: " + pwd + "\n");   // print passward
     	} else {                  
     	System.out.println("Console is unavailable.");  // have no right to use console              
     	}               
 	}           
}
tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 34
Solved Threads: 72
Skill Endorsements: 0

I didn't use console. I just write a program, compiled it with javac and run it with java. I checked the value and find that it is null.

Do you mean you try to get another windonw ? Did you use javaw on DOS? If so there would be no console available so that no printed output on screen. But how do you know the System.console() returns null?
The standard method to use console() method (starting from 1.6)is shown as follows:

import java.io.Console;           
public class TestConsole {            
     public static void main(String[] args) {
     	Console console = System.console(); // obtain an instance of Console
     	if (console != null) {              // make sure if you may use the console: if console is available                 
     	String user = new String(console.readLine("Enter username:"));      // read a line of characters                  
     	String pwd = new String(console.readPassword("Enter passowrd:"));   // read a password whti nothing printed on screen                  
     	console.printf("Username is: " + user + "\n");      // print username
     	console.printf("Password is: " + pwd + "\n");   // print passward
     	} else {                  
     	System.out.println("Console is unavailable.");  // have no right to use console              
     	}               
 	}           
}
java_programmer
Junior Poster
125 posts since May 2006
Reputation Points: 10
Solved Threads: 18
Skill Endorsements: 0

How did you start its execution?
Did you open a command prompt window and enter the java command there or how?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

I tried following two ways -
1. Open a command promt and run the commands there.But it did not run successfully -
failed to load the class - may be I need to check the classpath setting.
2. I tried to run from eclipse, but failed again.

How did you start its execution?
Did you open a command prompt window and enter the java command there or how?

java_programmer
Junior Poster
125 posts since May 2006
Reputation Points: 10
Solved Threads: 18
Skill Endorsements: 0

But it did not run successfully -

You'll need to copy and paste the contents of the command prompt screen here to get
help.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0757 seconds using 2.75MB