Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: System.out.println() should write to the console, but it doesn't

Tags:

java

console

I am trying to write Java console application, the code is quite simple:

public class ConsoleTest {
    public static void main(String[] args) {
        System.out.println("test");
    }
}

If I run this application from Eclipse, then i see "test" in Eclipse's "Console", but if I export my app as a "Runnable JAR file" and run it from Windows XP cmd.exe, then nothing is echoed to the console.

On the safe side, i tried to check System.console(), it returns null.

What can be wrong?

like image 867
Dmitry Frank Avatar asked May 01 '12 17:05

Dmitry Frank


People also ask

Does system out Println print to the console?

System. out. println does not print to the console, it prints to the standard output stream ( System. out is Java's name of the standard output stream).

Can system out Println () be used to write data to a file?

Invoke the out() method of the System class, pass the PrintStream object to it. Finally, print data using the println() method, and it will be redirected to the file represented by the File object created in the first step.

Where does system out Println print to?

The println is a method of java. io. PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.


1 Answers

How are you running your program outside eclipse?

You should use command line like

java -jar yourjar.jar

or

java -cp yourjar.jar ConsoleTest

If you are occasionally using javaw instead no console output will be produced. The STDOUT of javaw is null. Probably this is what happens when you are clicking on your jar file.

like image 179
AlexR Avatar answered Nov 09 '22 23:11

AlexR