【问题标题】:How to run a command with path including spaces (bundled JRE) when using AfterInstall with Inno Setup将 AfterInstall 与 Inno Setup 一起使用时如何运行包含空格的路径(捆绑的 JRE)的命令
【发布时间】:2019-05-17 12:55:11
【问题描述】:

我正在将 Java 程序安装为带有捆绑 JRE 文件夹的 exe。我无法通过我的应用程序成功调用捆绑的java.exe

所以我的笔记本电脑已经安装了 Java,所以以下工作正常:

[Files]
Source: "jre\*"; DestDir: "{app}\jre"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "build\launch4j\Application Lite.exe"; DestDir: "{app}"; Flags: ignoreversion; \
    AfterInstall: MyAfterInstall
[Code]
procedure MyAfterInstall();
var ResultCode: Integer;
begin
    Exec(
        'cmd.exe',
        '/c java -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) +
            ' com.examplesoftware.applicationlite.support.hibernateSupport',
        '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
end;

其中{app} 默认为c:\Example Software\Application Lite

当我尝试使用捆绑的 JRE 时,以下内容不起作用:

[Code]
procedure MyAfterInstall();
var ResultCode: Integer;
begin
    Exec(
        'cmd.exe',
        '/k ' + AddQuotes(ExpandConstant('{app}\jre\bin\java.exe')) +
            ' -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) +
            ' com.examplesoftware.applicationlite.support.hibernateSupport',
        '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
end;

我得到错误:

'c:\Example' 未被识别为内部或外部命令, 可运行的程序或批处理文件。

如果我将echo 与这样的代码一起使用:

Exec(
    'cmd.exe',
    '/k echo ' + AddQuotes(ExpandConstant('{app}\jre\bin\java.exe')) +
        ' -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) +
        ' com.examplesoftware.applicationlite.support.hibernateSupport',
    '', SW_SHOW, ewWaitUntilTerminated, ResultCode);

并复制它工作的命令。我不明白它为什么会坏。

【问题讨论】:

  • 你试过简单的Exec(ExpandConstant('{app}\jre\bin\java.exe'), ' -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) + ' com.examplesoftware.applicationlite.support.hibernateSupport', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)吗?
  • 虽然你确定{app}\Application Lite.exe 是正确的搜索路径?不应该只是{app}
  • @MartinPrikryl 好吧,“Application Lite.exe”是一个包装好的 jar,里面有一个支持类,当调用它时,它会在 MySQL 中创建必要的表。如果我运行 Exec('cmd.exe', '/k echo ...' 我得到: "c:\Example Software\Application Lite\jre\bin\java.exe" -cp "c:\Example Software\Application Lite\Application Lite.exe" com.examplesoftware .applicationlite.support.hibernateSupport 如果我将它复制粘贴到 cmd 中,它就可以工作了。
  • 或者,在/k (/c) 之后为整个命令加上另一对引号,例如:Exec('cmd.exe', '/k "' + AddQuotes(ExpandConstant('{app}\jre\bin\java.exe')) + ' -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) + ' com.examplesoftware.applicationlite.support.hibernateSupport"', '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
  • 但是即使使用Exec(ExpandConstant('{app}\jre\bin\java.exe'), '-cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) + ' com.examplesoftware.applicationlite.support.hibernateSupport', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)也应该可以工作

标签: java cmd inno-setup pascalscript


【解决方案1】:

你不需要cmd,它只会让它变得更复杂。这应该有效:

Exec(
  ExpandConstant('{app}\jre\bin\java.exe'),
  '-cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) + 
    ' com.examplesoftware.applicationlite.support.hibernateSupport',
  '', SW_SHOW, ewWaitUntilTerminated, ResultCode);

如果它不起作用并且你想debug the command with the cmd /k,你需要wrap whole command to double-quotes

Exec(
  'cmd.exe',
  '/k "' + AddQuotes(ExpandConstant('{app}\jre\bin\java.exe')) +
    ' -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) +
    ' com.examplesoftware.applicationlite.support.hibernateSupport"',
  '', SW_SHOW, ewWaitUntilTerminated, ResultCode);

【讨论】:

    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多