Sure, here’s the translation: 将 Java 导出为带有用户输入的 JAR。

huangapple go评论59阅读模式
英文:

java export to jar with user input

问题

我制作了一个小程序,它接受用户输入,然后将输出写入一个txt文件。
该程序在Eclipse中运行时没有任何问题。然后我将其导出为可运行的jar文件。
现在,如果我点击这个文件,它只会生成一个空的txt文件。
那么,是否有可能创建一个能够接受用户输入的jar文件呢?我做错了什么?

英文:

I made a small program which takes a user input then get an output to a txt file.
The program runs without any problem in eclipse. Then i exported it as runnable jar file.
Now if I click the file, it just generates an empty txt file.
So is it possible to create a jar which takes user input? What did I do wrong?

答案1

得分: 1

是的,这是可能的。出了什么问题?谁知道,这里没有太多的细节。

试着随便猜测一下:

  • 你在使用 Windows 操作系统。
  • 你双击它来启动这个应用程序。
  • 你正在使用 System.in 来获取输入。

在 Windows 上,应用程序必须声明它们是否在应用程序本身中执行命令行操作(显示一个黑框)。因此,仅在 Windows 上,java.exe 有两个变体:java.exe 和 javaw.exe。版本带有 -w 的版本被预先声明为不接受用户输入。java.exe 版本将始终显示一个丑陋的黑框,即使应用程序不需要输入或输出。默认情况下,javaw 用于打开 jar 文件,因此在这种情况下你不能使用 System.in。

关于“如果我在 Windows 上双击它,让这个应用程序工作”的问题没有解决方案 - 实际上,对于“使这个 jar 文件在我复制到任何系统时都能正常运行”的问题,也没有通用解决方案 - 获得一个正常运行的 JVM(大约在 10 年前)不再是用户的责任;这是你的责任,你必须构建一个完整的安装程序。

我猜你还在学习,如果是这样的话 - 嘿,那不是目标,对吧?只需打开一个命令提示符(按下 Windows 键,键入“cmd”,然后按回车键),然后以 java -jar myjar.jar 的方式运行你的应用程序,一切都会没问题的。如果你需要分发它,嗯,你可以创建一个批处理脚本来执行这个操作。

英文:

Yes, that is possible. What's going wrong? Who knows, there's not much detail here.

Taking a wild stab in the dark:

  • You're on windows.
  • You're double clicking it to start the app.
  • You're asking for input via System.in.

On windows, an application has to declare whether they do command line stuff (show a black box) in the app itself. Therefore, only on windows, java.exe comes in two variants: java.exe and javaw.exe. The -w version is pre-declared to not take user input. The java.exe version will always show an ugly black box, even if the app takes no input or output. By default, javaw is used to open jar files, thus, you can't use System.in in that case.

There is no solution for the problem of 'make this app work if I double click it on windows' - in fact, there is no general solution to 'make it so that this jar file will run properly on any system I copy it to' - getting a functioning JVM is (since 10 years or so) no longer the responsibility of the user; it is your responsibility, you'd have to build an entire installer.

I presume you're still learning, in which case - hey, that's not the goal, right? Just.. open a dosbox (hit the windows key, type 'cmd', hit enter), and run your app as java -jar myjar.jar and all will be well. If you need to ship it, well, you can make a batch script that does that.

答案2

得分: 0

我假设这个用户输入来自系统输入流 System.in

它需要在提供输入流的控制台/终端/Shell 中运行。

例如,如果你在Linux上运行,打开终端并运行:

java -jar myprogram.jar

在Windows上:

javaw -jar myprogram.jar
英文:

I assume this user input comes from the system input stream System.in.

It needs to run in a console/terminal/shell that provides an input stream.

For example, if you are running on linux, open a terminal and run this:

java -jar myprogram.jar

On Windows:

javaw -jar myprogram.jar

huangapple
  • 本文由 发表于 2020年9月29日 08:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64111464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定