Run executable jar from batch file but with console open so that user can see the output. Not using "java -jar"

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

Run executable jar from batch file but with console open so that user can see the output. Not using "java -jar"

问题

我已经创建了一个可执行的JAR文件,并且双击它可以正常运行。但在执行结束时有需要显示输出。我使用了带有 start xxx.jar 命令的批处理文件,它执行了JAR文件,但是没有控制台和输出。然后我使用了 java -jar XXX.jar,它完成了任务,但问题在于用户可能没有安装JRE。是否有任何建议?

英文:

I have created an executable jar file and it works fine on double clicking on it. But there is this need to show output at the end of the execution. I used batch file with start xxx.jar command and it executes the jar but without console and output. I then used java -jar XXX.jar it does the job but the thing here is user may not be having jre installed.
Any suggestions?

答案1

得分: 1

start 将会请求 Windows 使用注册表中配置的默认启动器来启动文件。

这自然意味着,除非在用户安装了 JVM 的奇怪情况下(你不应该依赖这种情况),否则不会发生任何事情 / 会发生错误,而且你将完全不知道是谁安装了 JVM,或者它的版本是什么,或者是否已经得到了适当的维护。它还将使用 javaw.exe,这是不显示控制台的变体,你无法改变这一点。

我认为你有误解 - 你想要的解决方案是:

  • 用户自己安装了 JDK 或(已过时的)JRE,并以使双击 jar 文件实际起作用的方式安装了它,但是...
  • 没有更新 PATH 变量或以其他方式确保命令行上的 java 可用

我认为这几乎没有涵盖很多安装情况。绝大多数部署要么是 '用户没有安装任何 Java',在这种情况下,startjava -jar 都不是解决方案,要么是他们在路径上安装了带有 Java 的安装包,在这种情况下,批处理文件中的 java -jar 可以正常工作。

那么,你如何部署 Java 应用程序呢?

在现代?使用 jlink 创建一个精简的可执行文件,然后编写自己的安装程序,安装一个您信任的 JVM(由 jlink 生成,其中包含的只有应用程序所需的部分),从而使用户能够在不需要 '查找 Java 安装并安装它' 的情况下运行您的应用程序。不幸的是,这比想象中的要困难,并且需要使用模块系统。

听起来工作量太大,传统方法有什么问题吗?

JRE 的概念(一个可以运行 Java 应用程序但不包含任何开发工具的 Java 虚拟机)已经 过时。没有更多的 JRE 了;Java 8,现在已经有 7 年的历史,是最后一次出现这种情况,而 Oracle 提供的 JRE 已经基本到期:他们已经停止支持它,或者将很快停止支持。这意味着 您要求用户安装将不再接收安全更新的软件。这就是为什么用这种方式做是个坏主意。很抱歉成为这个坏消息的传递者。

英文:

start will ask windows to launch the file using the default launcher configured in your registry.

This, naturally, means nothing happens / an error occurs unless you've got a bizarre situation where the end user has actually installed a JVM, which you should not be relying on, and you'd have absolutely no idea whose JVM that is, or what version it is, or if it has been properly maintained. It will also use javaw.exe which is the one that doesn't show a console, and there is absolutely nothing you can do to change this.

I think you're misinformed - you want a solution that works if:

  • The user has installed a JDK or (obsolete) JRE themselves, and installed it in a way that double clicking jar files actually works, but...
  • did not update the PATH variable or otherwise ensured that java on the command line works

I don't think that covers many installations at all. The vast majority of deployments will either be 'the user did not install any java', in which case neither start nor java -jar is going to be a solution, or they installed it with java on the path, in which case java -jar in a batch file works fine.

Okay so how DO you deploy java apps

In modern times? With jlink to create a cut-down executable, and then you write your own installer which installs a JVM that you trust (the one jlink made, which is smaller, as all bits you don't need for the app in question aren't included in it), and thus results in the user being able to run your app without the need to 'find a java installation and install it'. This is unfortunately more difficult than it could be and requires that you use the module system.

Sounds like too much work, what's wrong with the old way?

The concept of the JRE (a java virtual machine that can run java applications, but without any development tools included) is dead. There is no more JRE; java8, now 7 years old, was the last time that was a thing, and the JRE that oracle ships is pretty much out of time: They have stopped supporting it, or will real soon now. That means you are asking your users to install software that will no longer receive security updates. That's why it's such a bad idea to do it like this. Sorry to be the bearer of bad news.

huangapple
  • 本文由 发表于 2020年10月13日 21:48:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64336469.html
匿名

发表评论

匿名网友

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

确定