8

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?

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • 1
    Your code works fine for me. When you run it from cmd.exe, what *do* you see? – Hovercraft Full Of Eels May 01 '12 at 17:09
  • 1
    i think `System.console()` will return null in eclipse too. – Boris Strandjev May 01 '12 at 17:09
  • How are you running the jar from the command line? – Attila May 01 '12 at 17:11
  • @HovercraftFullOfEels, i see exactly the same as without `System.out.println("test");`. I.e. nothing. – Dmitry Frank May 01 '12 at 17:11
  • Related question: http://stackoverflow.com/questions/996517/java-console-api – Gilbert Le Blanc May 01 '12 at 17:11
  • Do you call it with the classpath? I have to call it from the Eclipse workspace bin directory like so: `java -cp .; yr12.m05.a.ConsoleTest`, but your classpath will likely be different, depending on your packages. Also you'll be running from a jar. Does your jar's manifest name this class as the main method? – Hovercraft Full Of Eels May 01 '12 at 17:13
  • @Attila, I just run cmd.exe, go to directory with my consoletest.jar, and type: `consoletest.jar`, and press Enter. By the way, I have another Java Swing application, and if i export it to runnable JAR file and start the same way, then my GUI starts, but I still do not see any output from `System.out`. Of course, in Eclipse i see the output. – Dmitry Frank May 01 '12 at 17:14
  • Try the following command: java -jar consoletest.jar – Chetter Hummin May 01 '12 at 17:15
  • @AmitBhargava, thanks, that command does the trick. But is there any way to make it work just when i type `consoletest.jar`? – Dmitry Frank May 01 '12 at 17:24

6 Answers6

16

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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 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 01 '12 at 17:21
  • 2
    @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 01 '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 01 '12 at 17:44
  • I've run into the same thing as @AlexR. I found that a shortcut containing a system variable pointing to javaw was a functional solution to this (in Windows) rather than a batch script. – BuvinJ Dec 30 '16 at 21:57
5

You need to run your jar file from the command line.

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

To run a jar file from a command prompt, 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 doesn't work, then it is likely not your code's fault. There is also the chance that the manifest file pointing to the Main class was set up incorrectly.

EnKrypt
  • 777
  • 8
  • 23
  • thank you, you are right. But is there any way to make it work just when I type `consoletest.jar`? – Dmitry Frank May 01 '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 01 '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 01 '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 01 '12 at 17:44
2

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

javac ConsoleTest.java
java ConsoleTest
ulu5
  • 439
  • 7
  • 11
1

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

rooftop
  • 3,031
  • 1
  • 22
  • 33
0

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.

Chad
  • 872
  • 8
  • 24
-2

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
Karim Garram
  • 22
  • 1
  • 9
  • 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 01 '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 01 '12 at 20:54