-1

I'm trying to make a setup for a little app written in Java. I need to create a Environment Variable (in this case YCS_JAVA) that points to a .dll from jdk-17. I've tried everything that I've found online and nothing works (error: Failed to find JAVA VM). It's an exe made with WinRun4J with a JAR inside. The solution with CMD works but doesn't work as it should. I have another txt file which i read with the jar but the CMD doesn't find it xD The function SetEnvironmentVariable doesn't do anything. The function SetEnvPath creates the Variable but still doesn't work with error "Failed to find Java VM". But if i try to run it manually it works... (with just a double click)

;[Registry]
;Root: HKCU; Subkey: "Environment"; ValueType: string; ValueName: "YCS_JAVA"; ValueData: "C:\Program Files\Java\jdk-17\bin\server\jvm.dll"
;Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: string; ValueName: "YCS_JAVA"; ValueData: "C:\Program Files\Java\jdk-17\bin\server\jvm.dll"; AfterInstall: RefreshEnvironment;

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; BeforeInstall: SetEnvPath; Flags: postinstall; 

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif
function SetEnvironmentVariable(lpName: string; lpValue: string): BOOL;
  external 'SetEnvironmentVariable{#AW}@kernel32.dll stdcall';
 
procedure SetEnvPath;
begin
  if not SetEnvironmentVariable('YCS_JAVA', 'C:\Program Files\Java\jdk-17\bin\server\jvm.dll') then
    MsgBox(SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;

[Code]
const
  SMTO_ABORTIFHUNG = 2;
  WM_WININICHANGE = $001A;
  WM_SETTINGCHANGE = WM_WININICHANGE;

type
  WPARAM = UINT_PTR;
  LPARAM = INT_PTR;
  LRESULT = INT_PTR;

function SendTextMessageTimeout(hWnd: HWND; Msg: UINT;
  wParam: WPARAM; lParam: PAnsiChar; fuFlags: UINT;
  uTimeout: UINT; out lpdwResult: DWORD): LRESULT;
  external 'SendMessageTimeoutA@user32.dll stdcall';  

procedure RefreshEnvironment;
var
  S: AnsiString;
  MsgResult: DWORD;
begin
  S := 'Environment';
  SendTextMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    PAnsiChar(S), SMTO_ABORTIFHUNG, 5000, MsgResult);
end;

This is because (I think) the setup execution frame isn't aware of the new Environment Variable. I've tried with [Registry] and with [Code] but I can't make it work. The SetEnvPath function doesn't even create the ENV Variable, idk why. Please help :'(

AurysVrV
  • 67
  • 1
  • 10
  • You didn't really explain for what environment/context/purpose you want to create/set the variable. But based on the `[Run]` section in your script, I believe you might be looking for [Environment variable not recognized [not available\] for [Run\] programs in Inno Setup](https://stackoverflow.com/q/21708140/850848). – Martin Prikryl Feb 07 '23 at 12:52
  • "I need to create a Environment Variable (in this case YCS_JAVA) that points to a .dll from jdk-17" to run a jar. I've tried those answers and nothing works... – AurysVrV Feb 07 '23 at 12:53
  • What jar? There's no jar in your code. Is it the `MyAppExeName`? How do we know? + Does the `jdk-17.0.6_windows-x64_bin` and `JREVerifyInstall` have anything to do with your problem? + When does the jar not work? When executing manually (how exactly?) after installed? Or when executing from your installer via the `[Run]` section? => We need [mcve]. – Martin Prikryl Feb 07 '23 at 12:59
  • It's an exe made with WinRun4J with a JAR inside. The solution with CMD works but doesn't work as it should. I have another txt file which i read with the jar but the CMD doesn't find it xD The function SetEnvironmentVariable doesn't do anything. The function SetEnvPath creates the Variable but still doesn't work with error "Failed to find Java VM". But if i try to run it manually it works... (with just a double click) – AurysVrV Feb 07 '23 at 13:02
  • Please do not post information in comments. Edit everything into your question. + *"The solution with CMD works but doesn't work as it should. I have another txt file which i read with the jar but the CMD doesn't find it"* – That's rather unclear to me. + *"creates the Variable but still doesn't work with error "Failed to find Java VM""* – Unclear too. + *"But if i try to run it manually it works"* – So WHEN it doesn't work? – Martin Prikryl Feb 07 '23 at 14:01
  • Using the code in the question, it doesn't work -.- Using this: [Run] Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; BeforeInstall: SetEnvPath; Flags: postinstall; – AurysVrV Feb 07 '23 at 14:11

1 Answers1

-1

FINALLY a fix that works:

Filename: "{cmd}"; Parameters: "/C setx YCS_JAVA ""C:\Program Files\Java\jdk-17\bin\server\jvm.dll"" && set YCS_JAVA=C:\Program Files\Java\jdk-17\bin\server\jvm.dll && START """" ""{app}\{#MyAppExeName}"" & EXIT 1"; \
  Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: postinstall runhidden

I've used setx to save the Env Variable and set for the terminal to know about the new Env Variable and now it finally works!

EDIT: The exe wasn't able to see other files in the folder that was installed so I've found another way:

Filename: "{cmd}"; Parameters: "/C setx YCS_JAVA ""C:\Program Files\Java\jdk-17\bin\server\jvm.dll"" && set YCS_JAVA=C:\Program Files\Java\jdk-17\bin\server\jvm.dll && cd {app} & START """" ""{#MyAppExeName}"""; \
  Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: postinstall runhidden
AurysVrV
  • 67
  • 1
  • 10
  • How can this help? How is this different to running `ExtensieImpoziteYCS.exe` directly? – Martin Prikryl Feb 08 '23 at 07:56
  • Edited with actual fix. Sorry xD – AurysVrV Feb 08 '23 at 08:42
  • So are you claiming that the `start` made the difference? How? Because otherwise your command is identical to the one that was in your original question (before you have edited it out). – Martin Prikryl Feb 08 '23 at 09:27
  • I can't say for sure, I will make some more tests to see exactly what is the difference, but this is the only one that worked ... – AurysVrV Feb 08 '23 at 09:31
  • Actually the EXIT i think did the trick, because without it the console would stay on because my app is supposed to stay active all the time (waiting for requests from a website) so the install would never actually finish. – AurysVrV Feb 08 '23 at 09:38
  • Your question was about setting an environment variable, not exiting a console (and with `/C`, the `cmd` console should exit even without `exit` anyway) – Martin Prikryl Feb 08 '23 at 10:08
  • Well, imo your answer does not answer the problem. The actual answer is here: https://stackoverflow.com/q/21708140/850848#43392128 – Martin Prikryl Feb 08 '23 at 11:08