1

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.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
Osher Shuman
  • 205
  • 2
  • 7
  • Did you try simple `Exec(ExpandConstant('{app}\jre\bin\java.exe'), ' -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) + ' com.examplesoftware.applicationlite.support.hibernateSupport', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)`? – Martin Prikryl May 18 '19 at 09:12
  • Though are you sure that `{app}\Application Lite.exe` is correct search path? Shouldn't is be just `{app}`? – Martin Prikryl May 18 '19 at 09:13
  • @MartinPrikryl well "Application Lite.exe" is a wrapped jar that has a support class in it that when called it creates the necessary tables in MySQL. If I run `Exec('cmd.exe', '/k echo ...' ` I get: _"c:\Example Software\Application Lite\jre\bin\java.exe" -cp "c:\Example Software\Application Lite\Application Lite.exe" com.examplesoftware.applicationlite.support.hibernateSupport_ If I copy paste this in cmd it works then. – Osher Shuman May 20 '19 at 05:46
  • @MartinPrikryl also I did try your first suggestion but no luck – Osher Shuman May 20 '19 at 05:52
  • *"If I copy paste this in cmd"* - From what path do you run that command? – Martin Prikryl May 20 '19 at 05:55
  • @MartinPrikryl The path of the cmd that opens during setup is "C:\WINDOWS\system32" – Osher Shuman May 20 '19 at 05:57
  • And if you remove the `-cp "c:\Example Software\Application Lite\Application Lite.exe" `? – Martin Prikryl May 20 '19 at 06:00
  • @MartinPrikryl sorry I do not follow? I need that to point to my jar/exe to run the support class – Osher Shuman May 20 '19 at 06:03
  • Can you just try please? – Martin Prikryl May 20 '19 at 06:08
  • @MartinPrikrylIf I run `Exec('cmd.exe', '/k java -cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) + ' com.examplesoftware.applicationlite.support.hibernateSupport', '', SW_SHOW, ewWaitUntilTerminated, ResultCode);` it works perfectly. I am really trying to avoid having someone install java and setup their environment variables but rather use my bundled jre. – Osher Shuman May 20 '19 at 06:09
  • @MartinPrikryl tried what you suggested and got an error "Error: Could not find or load main class com.examplesoftware.applicationlite.support.hibernateSupport" – Osher Shuman May 20 '19 at 06:14
  • 1
    Or, put another pair of quotes around whole command after `/k` (`/c`), like: `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);` – Martin Prikryl May 20 '19 at 06:20
  • @MartinPrikryl that worked!! Thank you so much! never thought to wrap the entire command. – Osher Shuman May 20 '19 at 06:31
  • 1
    But then it should work even with `Exec(ExpandConstant('{app}\jre\bin\java.exe'), '-cp ' + AddQuotes(ExpandConstant('{app}\Application Lite.exe')) + ' com.examplesoftware.applicationlite.support.hibernateSupport', '', SW_SHOW, ewWaitUntilTerminated, ResultCode)` – Martin Prikryl May 20 '19 at 06:32
  • I'll do that. Seems better. Thanks again! – Osher Shuman May 20 '19 at 06:34
  • But you wrote that you have tried that already and that it does not work. – Martin Prikryl May 20 '19 at 06:34
  • @MartinPrikrylsorry I did try it but I must have mistyped something. Sorry for wasting your time. – Osher Shuman May 20 '19 at 06:50

1 Answers1

1

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);
Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846