Creating a console in Java

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

Edit: I was hoping to just create it and not understand why I don't have it, since I need it to work in my program.

+6
java console
source share
5 answers

Perhaps you are trying to get a console by double-clicking in the bank

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

However, you can create a console using Swing and redirect standard input / output there.

It will look like this:

alt text http://img122.imageshack.us/img122/5692/dibujoof2.png

Source: Create a Java Console Inside a GUI Panel

+6
source share

How do you run a jar file? This would be the expected behavior for double-clicking on the icon in Windows Explorer, as Kelly pointed out, but not for launching it from the command line.

From the console entry in the API (my selection):

Regardless of whether the virtual machine depends on the console, the underlying platform, as well as how to invoke the virtual machine . If the virtual machine is launched from the interactive command line without redirecting standard input and output streams, then its console will exist and, as a rule, will be connected to the keyboard and display from which the virtual machine was started. If the virtual machine starts automatically, for example, using the background task scheduler, then usually there will be no console.

+5
source share

java.lang.System.out and java.lang.System.in are I / O streams for accessing the console. Java will not create a "console", but allows you to interact with the input / output streams provided by the operating system.

When you run it from a jar file (for example, by clicking on it from a folder), you will get GUI I / O streams that are not displayed anywhere.

Try creating a batch file with the java -jar command. When you run the batch file, you should see a command window. I guess the windows are here. Another way is to run cmd.exe directly with arguments that open the window, i.e. "Cmd.exe / c".

+4
source share

Instead of launching the jar file directly, open the console (you did not specify the operating system, but it will be the command line on Windows and the console on * Nix or Terminal on OS X). Then run java -jar /path/to/your.jar .

The equivalent of std::cout in Java would be System.out , as you probably already know.

EDIT: As for your editing, there is code for this. For example, you can use Swing . There's even fooobar.com/questions/137590 / ... with more than one working code sample.

0
source share

See JConsole , which is the common java console used, for example, by groovy. Or directly see groovy.

-one
source share

All Articles