Ivan Spajić
Ivan Spajić

Reputation: 169

How to call for the system console in Java?

I made a console-based Java application but every time I try to start the .jar file by clicking on it the program seems to be running but there is no console displayed. Is there specific code I must write in order to call for the system console?

Upvotes: 1

Views: 972

Answers (3)

porfiriopartida
porfiriopartida

Reputation: 1556

gcivil is right, you can see the results in console only if you start from console, if you are in windows you can open the command line Super + R and type cmd, then press enter (Super is the one with the Windows icon)

there you can type : java -jar "absolute path to your file" (don't forget the quotes)

another way is create a .bat file next to the .jar one, the bat file should contain java -jar filename.jar

you don't need the quotes nor absolute path since it is next to the .bat file now you can double click that instead of the .jar

Once the app is terminated it will close the console, if you need to see what is next you have to add pause

java -jar filename.jar
pause

Upvotes: 1

domsom
domsom

Reputation: 3171

The OS takes care of displaying console output. There is no code that you can write within Java to display or hide the "console" (because within Java, there's only standard output & error streams that you write to).

Windows usually leaves the console open after your program exits, but there might be a setting within the Java Runtime Environment that configures that behavior.

Upvotes: 1

gcivil
gcivil

Reputation: 61

Can you start the console first , change directory to where your jar file is and then run java -jar yiurjarfilename ?

Upvotes: 3

Related Questions