简体   繁体   English

如何使用launch4j包装还提供命令行界面的GUI应用程序?

[英]How can I use launch4j to wrap a GUI application that also provides a command line interface?

I am developing a Java application, which is mainly a GUI application. 我正在开发Java应用程序,主要是GUI应用程序。 However, it also provides a command line interface. 但是,它也提供了命令行界面。 I can divide the command line parameters in three different types: 我可以将命令行参数分为三种不同的类型:

  • The first type of parameter changes the behavior of the GUI. 第一种类型的参数更改GUI的行为。
  • The second type starts the GUI in a specific state. 第二种类型以特定状态启动GUI。
  • The third type causes the application to directly execute an action without displaying the GUI. 第三种类型使应用程序直接执行操作而不显示GUI。

When I execute the application with java -jar <application-name>.jar -parameter , everything works as expected. 当我使用java -jar <application-name>.jar -parameter执行应用程序时,一切都会按预期进行。 The application executes what it is asked to do. 应用程序执行要求执行的操作。 After the application is closed or finished, the shell prompt returns. 在应用程序关闭或完成后,shell提示返回。

Problem 问题

When I use launch4j to create a windows wrapper from the jar file, this behavior changes. 当我使用launch4j从jar文件创建Windows包装器时,此行为会更改。 The shell prompt always returns immediately. shell提示总是立即返回。 Thus, I can still pass command line parameters, but I cannot interact with the application via the terminal. 因此,我仍然可以传递命令行参数,但是无法通过终端与应用程序进行交互。 It does not print anything that is written to stdout or stderr and it also cannot read anything from stdin. 它不打印任何写到stdout或stderr的内容,也不能从stdin读取任何内容。

Question

How can I configure launch4j to create an exe wrapper that provides the same command line behavior as the wrapped jar file? 如何配置launch4j创建一个exe包装器,该包装器提供与包装后的jar文件相同的命令行行为?

Additional Information 附加信息

I use the launch4j maven plugin with the following configuration: 我将launch4j maven插件用于以下配置:

<plugin>
    <groupId>com.akathist.maven.plugins.launch4j</groupId>
    <artifactId>launch4j-maven-plugin</artifactId>
    <version>1.7.8</version>
    <executions>
        <execution>
            <id>launch4j</id>
            <phase>package</phase>
            <goals>
                <goal>launch4j</goal>
            </goals>
            <configuration>
                <headerType>gui</headerType>
                <stayAlive>true</stayAlive>
                <outfile>path/to/outfile.exe</outfile>
                <jar>path/to/infile.jar</jar>
                <dontWrapJar>false</dontWrapJar>
                <errTitle>Error in Launcher</errTitle>
                <classPath>
                    <mainClass>com.example.Launch</mainClass>
                    <addDependencies>false</addDependencies>
                </classPath>
                <icon>path/to/icon.ico</icon>
                <jre>
                    <minVersion>1.8.0</minVersion>
                    <initialHeapSize>512</initialHeapSize>
                    <maxHeapSize>1024</maxHeapSize>
                </jre>
            </configuration>
        </execution>
    </executions>
</plugin>

The documentation of launch4j says, that the given combination of the settings headerType=gui and stayAlive=true will Wait for the application to close , which is not the case. launch4j文档说,设置headerType=guistayAlive=true的给定组合将等待应用程序关闭 ,情况并非如此。

Since I do not have a windows machine available, I cannot try out what happens when I set headerType=console . 由于我没有可用的Windows计算机,因此无法尝试设置headerType=console时发生的情况。 The documentation says that this causes the wrapper to always wait and return the application's exit code . 文档说,这会使包装程序始终等待并返回应用程序的退出代码 However, since my application is mainly a GUI application, I wonder whether this setting has any negative side effects. 但是,由于我的应用程序主要是GUI应用程序,所以我想知道此设置是否有负面影响。 At this point I start to ask myself why I would ever want the wrapped application to have another command line behavior than the jar file. 在这一点上,我开始问自己,为什么我希望包装的应用程序具有不同于jar文件的其他命令行行为。

I finally got a windows machine up and running to test this by myself. 我终于启动了Windows机器,并自行运行对其进行测试。

I solved this issue by replacing this 我通过替换这个解决了这个问题

<headerType>gui</headerType>
<stayAlive>true</stayAlive>

by that 通过那个

<headerType>console</headerType>

According to the documentation stayAlive=true is actually implied by headerType=console , so I can safely remove it from the configuration. 根据文档, stayAlive=true实际上由headerType=console隐含,因此我可以安全地将其从配置中删除。

I was not able to notice any side-effects of this to the GUI. 我无法注意到GUI的任何副作用。 The only annoying thing is that this opens a console which displays the console output. 唯一令人讨厌的是,这将打开一个控制台,该控制台显示控制台输出。

So, I guess the intended solution to this is the configuration as described in the question: headerType=gui and stayAlive=true . 因此,我猜想对此的预期解决方案是问题中所述的配置: headerType=guistayAlive=true However, this does not work. 但是,这不起作用。 I think this is a bug in launch4j. 我认为这是launch4j中的错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM