简体   繁体   English

如何将动态JVM命令行标志传递给自包含的JavaFX应用程序?

[英]How can dynamic JVM command line flags be passed to a self-contained JavaFX application?

EDIT: Oracle accept my bug report requesting an enhancement as JDK-8138944 : Support command line arguments to the JVM passed to self-contained app launchers . 编辑:Oracle接受我的错误报告请求增强为JDK-8138944:支持传递给自包含应用程序启动器的JVM命令行参数

The problem 问题

My team is working on an open source Java SE project, ImageJ , that currently has a custom native launcher written in cross-platform C. We would like to move away from this launcher, switching to a more modern, industry standard and maintainable deployment mechanism. 我的团队正在开发一个开源Java SE项目ImageJ ,它目前有一个用跨平台C编写的自定义本地启动器 。我们希望摆脱这个启动器,转而采用更现代的行业标准和可维护的部署机制。 JavaFX self-contained applications are the most promising solution we have found so far. JavaFX自包含应用程序是迄今为止我们发现的最有前途的解决方案。

One great feature of ImageJ's current native launcher is its ability to customize how the JVM is launched. ImageJ当前本机启动器的一个重要特性是它能够自定义JVM的启动方式。 For example, you can write: 例如,你可以写:

ImageJ --debugger=8000 myFile.png

And the launcher will invoke the JVM with flag: 并且启动器将使用标志调用JVM:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=localhost:8000

while keeping the myFile.png as an argument to the Java main class. 同时保持myFile.png作为Java主类的参数。

But from the docs, we cannot see a way to accomplish something similar with the JavaFX packaging tool. 但是从文档中,我们看不到用JavaFX打包工具完成类似工作的方法。

Considerations 注意事项

I know that the UserJvmOptionsService provides a means to configure how the JVM is started (via Java Preferences API internally, which the JavaFX launcher reads before JVM startup). 我知道UserJvmOptionsService提供了一种配置JVM启动方式的方法(通过内部的Java Preferences API,JavaFX启动程序在JVM启动之前读取)。 This is nice for providing the user with a friendly dialog box where they can tweak maximum heap size and other common parameters. 这对于为用户提供友好的对话框非常有用,他们可以调整最大堆大小和其他常用参数。 Certainly we could add a remote debugging toggle and port settings to such a dialog box, and/or support JVM configuration via the CLI—but it would then require a restart of the application. 当然,我们可以在这样的对话框中添加远程调试切换和端口设置,和/或通过CLI支持JVM配置 - 但是它需要重新启动应用程序。

Ideally we would not need to support this scenario at all, and instead would handle all command line arguments after the JVM starts up, in Java. 理想情况下,我们根本不需要支持这种情况,而是在Java启动JVM启动后处理所有命令行参数。 For example, in most cases, system properties of the form -Dfoo=bar can be supported by simply parsing the arg and setting the system property at runtime, as long as it is done early enough in the application's startup cycle. 例如,在大多数情况下,只要在应用程序的启动周期中尽早完成,就可以通过简单地解析arg并在运行时设置系统属性来支持形式-Dfoo=bar的系统属性。 But there are clearly many cases where doing it after JVM startup is too late: 但很明显,很多情况下,在JVM启动之后这样做太晚了:

Our users expect to be able to pass these settings on the CLI, and have the Java runtime operate accordingly—and in the case of ImageJ, it is particularly important for backward compatibility. 我们的用户希望能够在CLI上传递这些设置,并使Java运行时相应地运行 - 对于ImageJ,它对于向后兼容性尤为重要。

Possible solutions 可能的解决方案

  • We could retain the native C launcher, replacing the native executable that the Java packaging tool installs. 我们可以保留本机C启动程序,替换Java打包工具安装的本机可执行文件。 But this strikes me as highly fragile, and largely defeats the purpose of switching to JavaFX deployment, since we would still need to maintain, build and test the custom native launcher across several different platforms. 但这让我觉得非常脆弱,并且在很大程度上违背了切换到JavaFX部署的目的,因为我们仍然需要在几个不同平台上维护,构建和测试自定义本机启动器。

  • Alternately, we could have the application main class be a very thin CLI option parser, which then spawns a second instance of the JVM. 或者,我们可以让应用程序主类成为一个非常精简的CLI选项解析器,然后生成JVM的第二个实例。 This would keep the bootstrapping logic in pure Java, which would be far more maintainable than the current native C code, while fully leveraging the JavaFX deployment scheme's cross-platform bundling capabilities. 这将使引导逻辑保持纯Java,这将比当前的本机C代码更易于维护,同时充分利用JavaFX部署方案的跨平台捆绑功能。 But that seems like a big hack with potentially challenging side effects. 但这似乎是一个具有潜在挑战性副作用的大黑客。

Finally, the question 最后,问题

Has anyone achieved support for JVM CLI arguments with JavaFX self-contained application deployment? 有没有人通过JavaFX自包含应用程序部署获得对JVM CLI参数的支持? If so, how did you accomplish it? 如果是这样,你是如何完成的? If not, any alternative suggestions? 如果没有,还有其他建议吗?

You can modify the launch arguments to the JVM by modifying the jvm user overrides config file that the API writes to: 您可以通过修改API写入的jvm用户覆盖配置文件来修改JVM的启动参数:

• Mac ~/Library/Application Support/[app.preferences.id]/packager/jvmuserargs.cfg • Windows C:\\Users[username]\\AppData\\Roaming[app.preferences.id]\\packager\\jvmuserargs.cfg • Linux ~/.local/[app.preferences.id]/packager/jvmuserargs.cfg •Mac~ / Library / Application Support / [app.preferences.id]/packager/jvmuserargs.cfg•WindowsC:\\ Users [username] \\ AppData \\ Roaming [app.preferences.id] \\ packager \\ jvmuserargs.cfg•Linux 〜/。本地/ [app.preferences.id] /packager/jvmuserargs.cfg

NOTE: This is an internal implementation detail and subject to change. 注意:这是一个内部实现细节,可能会有变化。

Rather than having any kind of two-phase launch system, where you see what options you have and then launch a replacement JVM, I would suggest you create your own native launcher by copying and editing the platform java.c launcher. 我建议您通过复制和编辑平台java.c启动器来创建自己的本机启动器,而不是使用任何类型的两阶段启动系统,在那里您可以看到有什么选项,然后启动替换JVM。 You can see what they are doing in the open JDK project. 您可以在打开的JDK项目中看到他们正在做什么。

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/914cd9b482c8/src/share/bin/java.c http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/914cd9b482c8/src/share/bin/java.c

There are quite a few places where they are looking for various options and converting them into init arguments for the JVM itself. 有很多地方他们正在寻找各种选项并将它们转换为JVM本身的init参数。 Take a look at the functions ParseArguments, AddApplicationOptions, TranslateApplicationArgs, CheckJvmType, AddOption etc. 看一下ParseArguments,AddApplicationOptions,TranslateApplicationArgs,CheckJvmType,AddOption等函数。

I am no C programmer but I have maintained my own launcher on quite a few occasions: such as a launcher that loaded a specific class path from a one-entry-per-line text file that could be checked into source control and one that used a different entry point to main(). 我不是C程序员,但我在很多场合都维护了自己的启动程序:例如一个启动程序,它从一个每行一行的文本文件加载一个特定的类路径,可以检查到源代码管理中,另一个使用了main()的不同入口点。 It doesn't change that much and you can isolate your changes quite easily, so that they are easy to reapply on later versions of java.c. 它没有那么大的改变,你可以很容易地隔离你的更改,以便它们很容易在更高版本的java.c上重新应用。 For your purposes, you would not really need to change every time someone makes a change to java.c, you only really need to change it when JavaVMInitArgs or some other critical aspect of the invocation interface changes. 出于您的目的,每次有人对java.c进行更改时,您都不需要更改,当JavaVMInitArgs或调用接口的其他一些关键方面发生更改时,您只需要更改它。

I am sure that if you proposed and contributed a more elegant option-handling solution, maybe one that behaved differently when argv[0] is not 'java', then maybe the open jdk team would adopt your approach and maintain it for you, or for all us. 我相信如果你提出并提供了一个更优雅的选项处理解决方案,也许当argv [0]不是'java'时表现不同,那么也许开放的jdk团队会采用你的方法为你维护它,或者为了我们所有人。 I am sure there are lots of developers out there needing features like these. 我相信有很多开发人员需要这些功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自包含JavaFX应用程序中的包含图标 - Include icon in Self-Contained JavaFX application 在Eclipse中创建JavaFX自包含应用程序(使用自定义JDK) - Create JavaFX self-contained application (with custom JDK) in Eclipse JavaFX:将参数传递给自包含应用程序不起作用 - JavaFX: passing Arguments to a Self-Contained Application does not work 如何在不使用 Maven/Gradle/IDE 的情况下部署自包含的 JavaFX 应用程序? - How to deploy a self-contained JavaFX application without using Maven/Gradle/IDEs? 如何使用捆绑在JavaFX 2自包含应用程序中的JRE在Mac OS X上启动可运行的JAR? - How to use the JRE bundled in a JavaFX 2 self-contained application to start a runnable JAR on Mac OS X? 如何在命令行上将系统属性传递给独立的Java应用程序 - How to pass system properties to self-contained Java applications on the command line 部署 JavaFX 应用程序、创建 JAR 和自包含应用程序以及本机安装程序的最佳方式是什么 - What is the best way to deploy JavaFX application, create JAR and self-contained applications and native installers 自包含的Javafx应用程序无法在ubuntu中运行:GLIBCXX_3.4.21的问题 - self-contained javafx application fail to run in ubuntu : issue with GLIBCXX_3.4.21 在 Eclipse 中设置 JavaFX 自包含构建时遇到问题 - Trouble setting up a JavaFX self-contained build in Eclipse JavaFX自包含的应用程序缺少密码加密服务吗? - JavaFX self-contained apps are missing the Cipher cryptographic service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM