Creating a jar file for the console

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

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

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

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

I want to create a jar file for it! but how can I show my console in a jar file without using a GUI? please help me thanks.

+4
source share
4 answers

You need to run the jar file from the command line (CLI). How:

java -jar yourJar.jar pause 

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

  • shortcut file for CMD and your banner as an argument
  • the batch file on which your bank is running using a code similar to mine above
  • calling it from a batch file manually (as I said above)
+11
source

First off, there are no such things as a โ€œconsole JARโ€ or a โ€œGUI JARโ€. However, there are different VM launchers for console and graphic modes. Or, more precisely, there are different launchers, one of which has a console, the other does not, but both of them are capable of displaying a GUI if your program has one. These launchers are called "java" (console version) and javaw (without console version).

To start the JAR using a specific launcher, use the javaw -jar JARFILE or java -jar JARFILE command. If you launch the console version without opening the console before executing it, the console closes as soon as your program is completed. This means that if you want to see your result, you must either not interrupt your program too quickly, or simply start the console first (Win + R, "cmd", Enter) and run "java -jar ..." from the console.

Another way is to go to the Windows control panel and change the program associated with the JAR extension from "javaw" to "java". This will force every JAR in the system to use the console. For a JAR with a graphical interface, this will only lead to the inconvenience of opening another window. Sometimes this is what you want, sometimes not.

+7
source

Creating an executable bank is the same regardless of which GUI or not. You need to specify the Main-Class in META-INF/MANIFEST.MF . See here and here

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

Then I would recommend using java.io.Console to read and write to the console.

+3
source

The process of creating a JAR file is the same for the console, as well as for GUI-based applications. You definitely don't see the output of your JAR. Just execute it from the console, after which you will see the output. For ease of use, you can make a batch file (MS-Windows) to run the JAR.

0
source

Source: https://habr.com/ru/post/1332871/


All Articles