开发者

making a jar file for console

开发者 https://www.devze.com 2023-02-01 02:06 出处:网络
I have a program without a GUI and I use console! So first I read a line from a user from console BufferedReader userReader = new BufferedReader(new InputStreamReader(System.in));

I have a program without a GUI and I use console! So first I read a line from a user from console

BufferedReader userReader = new BufferedReader(new InputStreamReader(System.in));

and then I will write an answer for the user in the console!

System.out.println("Server:"+output);

I want to create a jar file for it ! but how can i show my console 开发者_开发百科in jar file with out using GUI? please help me thanks.


You need to run your jar file from CLI (command line). Like:

java -jar yourJar.jar
pause

If you want to force this, there are different ways to do this:

  • a shortcut file to CMD and your jar as an argument
  • a batch file running your jar using code like mine above
  • calling it from a batch file manually (as I did above)


First, there are no such things as "console JAR" and "GUI JAR". There are different VM launchers for console and GUI modes, though. Or, more precisely, there are different launchers one of which has console, the other one hasn't, but both of them are capable of displaying a GUI if your program has one. These launchers are named "java" (console version) and javaw (no console version).

To start a JAR with a specific launcher, use "javaw -jar JARFILE" or "java -jar JARFILE" command. If you start the console version without opening a console before doing it, then the console closes as soon as your program is finished. This means if you want to see your output you should either not terminate your program too quickly or just start a console first (Win+R, "cmd", Enter) and run "java -jar ..." from the console.

The other way is to go to the Windows Control Panel and change the program associated with the JAR extension from "javaw" to "java". This will make every JAR in the system use console. For JARs with GUI this will only introduce an inconvenience of having another window open. Sometimes it is what you want, sometimes not.


Making an executable jar is the same no matter GUI or not. You need to specify your Main-Class in your META-INF/MANIFEST.MF. See here and and here

Of course, if you want to start something on the console, just open the console and use the following: java -jar archive.jar (with the above requirements met)

Then I'd recommend using java.io.Console for reading and writing to the console.


The process of creating a JAR file is same for console as well as GUI based apps. You are definitely not seeing the output of your JAR. Simply execute it from a console then you will see the output. For the ease of use, you can make a batch file (MS-Windows) to run your JAR.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号