为控制台制作 jar 文件

发布于 2024-10-09 00:50:39 字数 323 浏览 0 评论 0原文

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

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

然后我会在控制台中为用户写一个答案!

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流殇 2024-10-16 00:50:39

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

java -jar yourJar.jar
pause

如果你想强制执行此操作,有不同的方法可以做到这一点:

  • CMD 的快捷方式文件和你的 jar 作为参数
  • 使用像我上面这样的代码运行你的 jar 的批处理文件
  • 手动从批处理文件调用它(就像我所做的那样)多于)

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)
北音执念 2024-10-16 00:50:39

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

要使用特定启动器启动 JAR,请使用“javaw -jar JARFILE”或“java -jar JARFILE”命令。如果您在启动控制台版本之前没有打开控制台,那么控制台将在程序完成后立即关闭。这意味着如果您想查看输出,您不应该太快终止程序,或者首先启动控制台(Win+R,“cmd”,Enter)并从控制台运行“java -jar ...”。

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

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.

弥枳 2024-10-16 00:50:39

无论是否使用 GUI,制作可执行 jar 都是一样的。您需要在 META-INF/MANIFEST.MF 中指定您的Main-Class。请参阅此处和这里

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

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

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.

墨洒年华 2024-10-16 00:50:39

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文