我正在使用 Install4j 6.1.6,并且我正在尝试执行以下操作:
我想使用来自安装程序(第一个屏幕)的参数在外部运行 jar 文件,然后在第二个屏幕上显示结果。
<小时/>所以,第一个屏幕有:
- 一些输入字段
- “运行可执行文件或批处理文件”操作
- 可执行文件:${installer:sys.javaHome}\bin\java
- 前面输入字段中的参数放在一起
- 选中“等待终止”
- “返回代码的变量名称”是my-returncode
- “重定向 stderr”到安装程序变量 my-stderr
- 以及“运行脚本”操作,该操作会自动转到下一个屏幕
- context.gotoScreen(context.getScreenById("nextScreenId")); 返回真;
第二个屏幕根据 my-returncode 和 my-stderr 的值显示适当的标签。
每个案例都工作得很好。
但是,我决定将第二个屏幕上的“后退按钮”更改为始终可见,因为这样用户就可以解决问题(如果他在输入字段中输入了错误的内容)。
因此,我在第一个屏幕上的“运行可执行文件或批处理文件”操作中选中了“可以多次执行”。
<小时/>现在,当我在输入字段中输入错误数据时,单击 Next >
脚本将运行并失败,第二个屏幕将显示出现错误。 my-returncode 和 my-stderr 包含退出代码和错误消息。
然后我单击 < Back
按钮并修复问题,我单击 Next >
那么这次脚本运行成功,但我仍然收到之前的错误,因为 my-returncode 和 my-stderr 没有更新,它们仍然包含最后的错误消息并退出代码!
在这种情况下我该如何解决这个问题并使Install4J更新变量?
<小时/>更新:
我意识到第二个操作(转到下一个屏幕)完全没有必要,所以我删除了它。
这仍然没有解决我的问题,以下是 .install4j/installation.txt
的相关部分:
[INFO] com.install4j.runtime.beans.screens.FormScreen [ID 1199]: Show screen
Variable changed: shouldLaunchExternalScript=true[class java.lang.String]
Variable changed: externalScriptDataFolder=F:\badfolderpath[class java.lang.String]
在此屏幕上,用户可以选择启动此外部脚本并设置数据文件夹。我单击了“启动脚本”单选按钮并设置了错误的文件夹路径。
[INFO] com.install4j.runtime.beans.actions.misc.RunExecutableAction [ID 1367]: Execute action
Property arguments: [-cp, ./*, -Ddata.dir=F:\badfolderpath, ...]
Property rollbackSupported: false
Property returnCodeVariable: my-returncode
Property stdoutRedirectionMode: No redirection
Property stdoutVariableName:
Property workingDirectory: ...
Property failOnStderrFileError: false
Property failOnStdinFileError: false
Property failOnStdoutFileError: false
Property rollbackExecutable: null
Property rollbackArguments: null
Property rollbackWorkingDirectory: null
Property stderrRedirectionMode: To installer variable
Property stderrVariableName: my-stderr
Property stdinRedirectionMode: No redirection
Property environmentVariables: {}
Property keepConsoleWindow: true
Property showWindowsConsole: false
Property useRollbackExecutable: false
Property includeParentEnvironmentVariables: true
Property stdinString:
Property timeout: 0
Property stdinFile: null
Property stderrFile: null
Property stdoutFile: null
Property wait: true
Property waitForStreams: true
Property executable: c:\users\user\appdata\local\temp\e4jb23d.tmp_dir1564671223\jre\bin\java
Property logArguments: true
used working dir: ...
execute using batch file
脚本使用给定的参数启动,但会失败:
[ERROR] com.install4j.runtime.beans.actions.misc.RunExecutableAction [ID 1367]: return value is 1
[INFO] com.install4j.runtime.beans.actions.misc.RunExecutableAction [ID 1367]: Variable changed: my-stderr
Variable changed: my-returncode=1[class java.lang.Integer]
[ERROR] com.install4j.runtime.beans.actions.misc.RunExecutableAction [ID 1367]: return value is 1
Execute action not successful after 19696 ms
[INFO] com.install4j.runtime.beans.screens.FormScreen [ID 1199]: command: move 1 screens, executing actions, checking condition
[INFO] com.install4j.runtime.beans.screens.FormScreen [ID 1360]: Show screen
外部脚本中返回码1表示失败,0表示成功。变量已更新,现在我在第二个屏幕上看到外部脚本失败。我现在点击返回并修复问题(选择正确的文件夹路径):
[INFO] com.install4j.runtime.beans.screens.FormScreen [ID 1360]: command: go back 1 screens using history
[INFO] com.install4j.runtime.beans.screens.FormScreen [ID 1199]: Show screen
Variable changed: shouldLaunchExternalScript=true[class java.lang.String]
Variable changed: externalScriptDataFolder=F:\goodfolderpath[class java.lang.String]
变量已更新,外部脚本使用正确的参数再次启动:
[INFO] com.install4j.runtime.beans.actions.misc.RunExecutableAction: Execute action
Property arguments: [-cp, ./*, -Ddata.dir=F:\goodfolderpath, ...]
Property rollbackSupported: false
Property includeParentEnvironmentVariables: true
Property stderrFile: null
Property executable: c:\users\user\appdata\local\temp\e4jb23d.tmp_dir1564671223\jre\bin\java
Property stdinString:
Property wait: true
Property stdinFile: null
Property stdoutFile: null
Property waitForStreams: true
Property logArguments: true
Property timeout: 0
Property stderrVariableName: my-stderr
Property stdinRedirectionMode: No redirection
Property rollbackArguments: null
Property stdoutRedirectionMode: No redirection
Property rollbackWorkingDirectory: null
Property workingDirectory: ...
Property stdoutVariableName:
Property environmentVariables: {}
Property returnCodeVariable: my-returncode
Property rollbackExecutable: null
Property stderrRedirectionMode: To installer variable
Property useRollbackExecutable: false
Property failOnStdinFileError: false
Property failOnStdoutFileError: false
Property failOnStderrFileError: false
Property showWindowsConsole: false
Property keepConsoleWindow: true
used working dir: ...
execute using batch file
Variable changed: my-stderr
Variable changed: my-returncode=0[class java.lang.Integer]
Execute action successful after 16416 ms
脚本成功运行,并且变量已更改(my-returncode 是 1,现在是 0)。
[INFO] com.install4j.runtime.beans.screens.FormScreen [ID 1199]: command: move 1 screens, executing actions, checking condition
[INFO] com.install4j.runtime.beans.screens.FormScreen [ID 1360]: Show screen
但是,在结果屏幕上,我仍然看到未更新的先前 my-stderr 和 my-returncode 值,表明运行外部脚本时出现错误。
最佳答案
我可以通过将此行添加到第二个屏幕的预激活脚本来解决该问题:
formEnvironment.reinitializeFormComponents();
关于java - Install4J:stderr 和返回代码重定向到变量仅写入一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57271402/