java Inno安装编译器创建环境并使用它来运行.exe(jar)

0aydgbwb  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(97)

我正在尝试为一个用Java编写的小应用程序进行设置。我需要创建一个环境变量(在本例中为YCS_JAVA),该变量指向jdk-17中的.dll。我已经尝试了网上找到的所有方法,但没有任何效果(错误:找不到JAVA VM)。这是一个exe与WinRun 4J与JAR内。与CMD的解决方案的工作,但不工作,因为它应该。我有另一个txt文件,我读的jar,但CMD没有找到它xD函数SetEnvironmentVariable不做任何事情。函数SetEnvPath创建变量,但仍然不工作,错误“找不到Java VM”。但是如果我试着手动运行它,它就能工作了...(只需双击)

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

这是因为(我认为)安装执行框架不知道新的环境变量。我尝试了[Registry]和[Code],但我无法使其工作。SetEnvPath函数甚至不创建ENV变量,不知道为什么。请帮助:'(

dldeef67

dldeef671#

最后一个有效的解决方案是:

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

我已经使用setx来保存环境变量,并使用set让终端知道新的环境变量,现在它终于工作了!
编辑:该exe无法看到安装的文件夹中的其他文件,所以我找到了另一种方法:

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

相关问题