Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a console in Java

Tags:

java

console

When I try to use java.lang.System.console(), I get a null pointer. I can still write to out and read from in, but this only works when I run straight from my IDE. When I run the .jar file directly, nothing happens. How can I create a console like I'd see using std::cout for use in Java?

Edit: I was hoping to just create one, rather than understand why I don't have one, since I need one for my program's operation.

like image 706
Puppy Avatar asked Apr 27 '10 16:04

Puppy


People also ask

What is console method in Java?

io. Console class provides methods to access the character-based console device, if any, associated with the current Java virtual machine. The Console class was added to java.io by JDK 6. Important Points: It is used to read from and write to the console, if one exists.

Does Java have console log?

Console logs in Java At the most basic level, Java errors are logged on the console. Developers typically call System. out. println() to print log messages on the console.


2 Answers

Perhaps you're trying to get the console by double-clicking in the jar

Try creating a batch file that opens the console for you.

You can however create a console using Swing and redirect standard input/output there.

Source: Create Java console inside a GUI panel

like image 191
OscarRyz Avatar answered Oct 01 '22 22:10

OscarRyz


How are you running the JAR file exactly? That would be the expected behavior for double-clicking its icon in Windows Explorer, as Kelly alluded to, but not for firing it up from the command line.

From the Console entry in the API (emphasis mine):

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.

like image 40
Pops Avatar answered Oct 01 '22 23:10

Pops