Take the 2-minute tour ×

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

public class ConsoleTest {
    public static void main(String[] args) {

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?

share|improve this question
Your code works fine for me. When you run it from cmd.exe, what do you see? –  Hovercraft Full Of Eels May 1 '12 at 17:09
i think System.console() will return null in eclipse too. –  Boris Strandjev May 1 '12 at 17:09
How are you running the jar from the command line? –  Attila May 1 '12 at 17:11
@HovercraftFullOfEels, i see exactly the same as without System.out.println("test");. I.e. nothing. –  Dmitry Frank May 1 '12 at 17:11
Related question: stackoverflow.com/questions/996517/java-console-api –  Gilbert Le Blanc May 1 '12 at 17:11
show 5 more comments

6 Answers

up vote 8 down vote accepted

How are you running your program outside eclipse?

You should use command line like

java -jar yourjar.jar


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.

share|improve this answer
AlexR, you are right, thanks, i tried to run my app as you suggested, it works. But this is a bit painful to run app every time in such manner, is there any way to make it work just when i type yourjar.jar and press Enter? –  Dmitry Frank May 1 '12 at 17:21
@Dmitry Frank, running program by clicking jar file is not safe anyway. It depends on mapping of extension jar to program that executes it (by default javaw) on client's machine. Many developers change this mapping to winzip because they want to view content of jar file by clicking it. So, the best way is to write bat file for windows and shell script for Unix to execute java program or to use WebStart. –  AlexR May 1 '12 at 17:36
thanks, but anyway I would like to have some way to compile my app as a "Console" app, and javaw should run it the same way as java -jar myapp.jar. It will work at least for people who didn't change his settings =) Pity that there's no such option. –  Dmitry Frank May 1 '12 at 17:44
add comment

You need to run your jar file from command prompt.

If you double click on it , then you wont be able to see the command line operations being run in the background. Jar files are run by double clicking only when they involve GUI.

To run jar files from command prompt is pretty simple. You just do this:

java -jar ConsoleTest.jar

Assuming you've set environment variables for java.exe and the current directory has the jar file.

If this din't work , then it is not your code's fault at all. There is also the chance that the manifest file pointing to the Main class was set up incorrectly

share|improve this answer
thank you, you are right. But is there any way to make it work just when I type consoletest.jar? –  Dmitry Frank May 1 '12 at 17:27
Just typing consoletest.jar in cmd wont help as that will be same as double clicking it. You can however make a batch file which runs java -jar consoletest.jar when you click on it –  EnKrypt May 1 '12 at 17:32
of course I understand that typing that is the same as double-clicking it. But I would like to have some way to compile my app as a "Console" app, and this app should behave by double-clicking exactly the same as it is runned by java -jar myapp.jar. I see no reasons to not to provide such option, as far as batch files and manual typing is painful. –  Dmitry Frank May 1 '12 at 17:39
Well you could do one thing. You could use the Runtime.getRuntime().exec("start") method to start a new command line window and make it run java -jar consoletest.java on that window to display text to the console. In this case however , if you try running it through command prompt , it will open two windows which may be an inconvenience. But double clicking it will open a single cmd window and will work –  EnKrypt May 1 '12 at 17:44
EnKrypt, thank you, i'll keep this in mind. –  Dmitry Frank May 1 '12 at 18:05
add comment

Chances are your runnable jar file isn't working as you expect and isn't actually running your ConsoleTest class.

share|improve this answer
add comment

Try compiling it into a class from the command line and then running it. Does that work?

javac ConsoleTest.java
java ConsoleTest
share|improve this answer
Huh, it works! Thanks. Actually @AlexR is right, i tried to run my app as he suggested, and it works. –  Dmitry Frank May 1 '12 at 17:20
add comment

Like what Hovercraft Full Of Eels wrote, compile the .java file using cmd.exe with the following command:

javac ConsoleTest.java

Then this will create a .class file, and we want to compile it with this command:

java ConsoleTest

It should then display the "test" output.

share|improve this answer
add comment

you have to install the java jdk and add the root of the directory containing the file java.exe to your system's directories, you will find further informations in the jdk's installation guide.

and then run the file in the console with the command

> java file.java
share|improve this answer
I would have voted down if i had that reputation. Your answer is unrelated to the problem and even that small piece of command that you put there is wrong –  EnKrypt May 1 '12 at 19:13
sorry then, I just wanted to say that you have to use the java command wich is part of the java jdk and make sure that the environment variable is well configured.I know this for .java files but it works too for .jar files with more arguments. –  Karim Garram May 1 '12 at 20:54
add comment

Your Answer


