Java 为控制台制作 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4497579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 18:03:13  来源:igfitidea点击:

making a jar file for console

javaconsoleexecutable-jar

提问by user472221

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

我有一个没有 GUI 的程序,我使用控制台!所以首先我从控制台读取用户的一行

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

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);

System.out.println("服务器:"+输出);

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.

我想为它创建一个 jar 文件!但是如何在不使用 GUI 的情况下在 jar 文件中显示我的控制台?请帮助我谢谢。

采纳答案by Christian

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

您需要从 CLI(命令行)运行 jar 文件。喜欢:

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)
  • CMD 的快捷方式文件和您的 jar 作为参数
  • 使用上面的代码运行您的 jar 的批处理文件
  • 手动从批处理文件中调用它(如我上面所做的那样)

回答by Bozho

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

无论是否有 GUI,制作一个可执行的 jar 都是一样的。您需要Main-Class在您的META-INF/MANIFEST.MF. 见这里这里

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)

当然,如果你想在控制台上启动一些东西,只需打开控制台并使用以下内容:(java -jar archive.jar满足上述要求)

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

然后我建议使用java.io.Console读取和写入控制台。

回答by Mudassir

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.

对于控制台和基于 GUI 的应用程序,创建 JAR 文件的过程是相同的。您肯定没有看到 JAR 的输出。只需从控制台执行它,然后您就会看到输出。为了便于使用,您可以制作一个批处理文件 (MS-Windows) 来运行您的 JAR。

回答by Sergei Tachenov

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).

首先,没有“控制台 JAR”和“GUI JAR”之类的东西。不过,控制台和 GUI 模式有不同的 VM 启动器。或者,更准确地说,有不同的启动器,其中一个有控制台,另一个没有,但是如果您的程序有一个 GUI,它们都能够显示 GUI。这些启动器被命名为“java”(控制台版本)和 javaw(无控制台版本)。

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.

要使用特定启动器启动 JAR,请使用“javaw -jar JARFILE”或“java -jar JARFILE”命令。如果您在启动控制台版本之前没有打开控制台,那么程序一完成,控制台就会关闭。这意味着如果你想看到你的输出,你要么不要太快地终止你的程序,要么先启动一个控制台(Win+R,“cmd”,回车)然后从控制台运行“java -jar ...”。

The other way is to go to the Windows Control Panel and change the program associated with the JAR extensionfrom "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.

另一种方法是转到 Windows 控制面板并将与 JAR 扩展名关联的程序从“javaw”更改为“java”。这将使系统中的每个 JAR 都使用控制台。对于带有 GUI 的 JAR,这只会带来打开另一个窗口的不便。有时这是你想要的,有时不是。