Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a jar file for console

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.

like image 369
user472221 Avatar asked Nov 29 '22 05:11

user472221


2 Answers

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.

like image 39
Sergei Tachenov Avatar answered Dec 04 '22 13:12

Sergei Tachenov


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)
like image 82
Christian Avatar answered Dec 04 '22 13:12

Christian