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?

Recommended Answers

All 6 Replies

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.

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              
     	}               
 	}           
}

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              
     	}               
 	}           
}

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

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.