4

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.

user472221
  • 3,014
  • 11
  • 35
  • 42
  • 1
    for How to create jar part check [this](http://stackoverflow.com/questions/4497068/making-a-jar-file/4497088#4497088) – jmj Dec 21 '10 at 08:57

4 Answers4

13

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)
Christian
  • 27,509
  • 17
  • 111
  • 155
  • It runs just as well it seems (I just ran `java jarjar.jar`; it gave me an error that `jarjar.jar` didn't exist). – Christian Dec 21 '10 at 09:04
  • 1
    When you leave out the -jar option the parameter is taken as a classname which will apparently not work. Only when using the -jar option you can run an "executable" jar file –  Dec 21 '10 at 09:15
  • Ah, ok. I admit I couldn't test that out (I don't develop in Java anymore). Thanks for clarification. – Christian Dec 21 '10 at 09:17
  • when i run my jar file like what you said ,I can not see the console!! – user472221 Dec 21 '10 at 09:36
  • @user472221: you need to start the console *first*, then type the above commands –  Dec 21 '10 at 10:00
7

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.

Community
  • 1
  • 1
Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
3

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

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.

Mudassir
  • 13,031
  • 8
  • 59
  • 87