File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why is the import java.io.Console receives a warning & not able to run console? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why is the import java.io.Console receives a warning & not able to run console?" Watch "Why is the import java.io.Console receives a warning & not able to run console?" New topic
Author

Why is the import java.io.Console receives a warning & not able to run console?

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

I am getting the following warnings on line 3 when editing RegexTestHarness.java tutorial using Java 7 & Netbeans 7.1.1 on Windows XP platform:

“Import section does not correspond to the specified code style rules”

Below is the content of RegexTestHarness.java:



It ran with the following output in Netbeans:

run:
No console.
Java Result: 1

What is wrong with the console import on line 3 and how RegexTestHarness invoke it within Netbeans to use it? It appears to have failed due to not able to locate / launch java.io.Console.
JConsole plugin has been added to Netbeans without success. I have done some Googling but could not find an answer for it.

I have been directed by Netbeans forum members back here since this is a general Java question.

Thanks,

Jack
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Read the Javadoc page of java.lang.System(). It tells you exactly when console() will return null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Rob Spoor wrote: . . . It tells you exactly when console() will return null.
Not in my Java7 version, it doesn’t. You get a Console object for anything started with the java tool. Most IDEs (and executable jars) use the javaw tool to start, and there is likely to be no Console.
I am surprised it says “no console” rather than “Exception in thread "main" java.lang.NullPointerException...”

Replace all those console objects with a Scanner using System.in and a print instruction for the prompt.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Rob Spoor wrote: It tells you exactly when console() will return null.

I read java.lang.System() but still do not understand how console() is used but assumed that the reason why it return null because there are no console available.

Campbell Ritchie wrote: Replace all those console objects with a Scanner using System.in and a print instruction for the prompt.
Could you please elaborate a bit more using the above code as example since I do not know how to replace console with Scanner using System.ini or print instruction for prompt.

Thanks to you both for your advice,

Jack
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Have you never used a Scanner? Start by reading its API page. Except for nextLine(), its methods are all pretty intuitive and easy to use.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Jack Bush wrote: . . . Scanner using System.ini . . .
Not System.ini. System.in. And the print instruction would look like this:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Campbell Ritchie wrote:
Rob Spoor wrote: . . . It tells you exactly when console() will return null.
Not in my Java7 version, it doesn’t. You get a Console object for anything started with the java tool. Most IDEs (and executable jars) use the javaw tool to start, and there is likely to be no Console.

You're right, the description isn't in the Javadoc of System.console(). It's in the Javadoc of java.io.Console itself.

I am surprised it says “no console” rather than “Exception in thread "main" java.lang.NullPointerException...”

Jack checks for null and prints this message if that's the case.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Rob Spoor wrote: . . . Jack checks for null and prints this message if that's the case.
I really ought to learn to read the code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is the import java.io.Console receives a warning & not able to run console?