making a jar file for console

后端 未结 4 609
春和景丽
春和景丽 2020-12-03 23:30

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

相关标签:
4条回答
  • 2020-12-04 00:21

    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.

    0 讨论(0)
  • 2020-12-04 00:24

    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)
    0 讨论(0)
  • 2020-12-04 00:31

    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.

    0 讨论(0)
  • 2020-12-04 00:32

    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 讨论(0)
提交回复
热议问题