Search code examples
javacmdinno-setuppascalscript

How to run a command with path including spaces (bundled JRE) when using AfterInstall with Inno Setup


I am installing a Java program as an exe with a bundled JRE folder. I can't get the setup to successfully call the bundled java.exe with my application.

So my laptop has Java installed already so the following worked:

[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;

Where {app} is by default c:\Example Software\Application Lite.

The following does not work when I try use the bundled 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;

I get the error:

'c:\Example' is not recognized as an internal or external command, operable program or batch file.

If I use echo with the code like this:

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);

and copy the command it works. I don't understand why it is breaking.


Solution

  • You do not need the cmd, it only makes it more complicated. This should work:

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

    Had it not work and you have wanted to debug the command with the cmd /k, you need to 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);