简体   繁体   中英

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?

您可以先启动控制台,将目录更改为您的jar文件所在的位置,然后再运行java -jar yiurjarfilename吗?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM