1

The simplest Java program is helloWorld. Just one class, one main function, and one statement: System.out.println("hello world");

Entered that program in Eclipse Oxygen.3A as a Java project, in a package called helloWorld in a class called App, running on Java 1.8.

Runs fine in Eclipse, I can see hello world in Eclipse console window. Runs fine, too, in a cmd window when started as java helloWorld.App.

Then - in Eclipse again - I entered a Run Configuration specifying the Main class as helloWorld.App and exported the project as a Runnable JAR file helloWorld.jar with Extract required libraries into generated JAR to some folder on the %PATH%.

In a cmd window I entered helloWorld.jar and expected to see hello world in that cmd window. But no, nothing. The program finsished correctly (I did other experiments, it does what it should) but System.out.println refuses to print in the cmd window.

What did I miss? E.g. which checkbox have I overlooked so that the exported JAR does not know what the system output is (I could not find one - but sometimes you are blind)?

ngong
  • 754
  • 1
  • 8
  • 23
  • What is the full command with which you execute the `jar`? Is it for example `java -jar helloWorld.jar`? Or maybe `javaw -jar helloWorld.jar`? Or just `helloWorld.jar`? Note that on most systems the default program tied to `jar` is `javaw.exe` which is `java.exe` but without command window. Thus also no sysout/syserr/sysin. – Zabuzard Jul 04 '18 at 17:33
  • use `ftype jarfile` to list the actual command, and to change it from `javaw.exe` to `java.exe` - like in `ftype jarfile="C:\Program Files\Java\JRE8\bin\java.exe" -jar "%1" %*` – user85421 Jul 04 '18 at 18:21

1 Answers1

2

Run your jar like:

java -jar helloWorld.jar

and not just helloWorld.jar.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
  • While this probably solves, can you also explain the issue? (default binding `javaw.exe`, difference to `java.exe`, ...) – Zabuzard Jul 04 '18 at 17:35
  • Yes, that worked. Thank you Shanu Gupta. However, it is not as convenient. Do you know how to instrument the program so it sees the cmd window as system output even when started by Win mechanisms? You're right Zabuza: if I start with javaw -jar helloWorld.jar I have the same missing output as with just helloWorld.jar. – ngong Jul 04 '18 at 17:41
  • The explanation can be found at [https://stackoverflow.com/questions/8194713/difference-between-java-javaw-javaws] – ngong Jul 04 '18 at 17:52