LAUDA937

Java JDK Tool Delphi version source code

Jul 22nd, 2025
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.93 KB | Source Code | 0 0
  1. // Java JDK (system environment variable one-click setting tool) Delphi version source code is here
  2.  
  3. unit Unit2;
  4.  
  5. interface
  6.  
  7. uses
  8.   Registry, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  9.   System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  10.   Vcl.StdCtrls;
  11.  
  12. type
  13.   TForm2 = class(TForm)
  14.     btnSetEnv: TButton;
  15.     btnVerify: TButton;
  16.     edtJdkPath: TEdit;
  17.     lblStatus: TLabel;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure btnSetEnvClick(Sender: TObject);
  20.     procedure btnVerifyClick(Sender: TObject);
  21.   private
  22.     function SetJavaEnvironment(const JdkPath: string): Boolean;
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. our
  28.   Form2: TForm2;
  29.  
  30. implementation
  31.  
  32. {$R *.dfm}
  33.  
  34. // Core function: set Java environment variables
  35. function TForm2.SetJavaEnvironment(const JdkPath: string): Boolean;
  36. our
  37.   Reg: TRegistry;
  38.   PathValue: string;
  39. begin
  40.   Result := False;
  41.   Reg := TRegistry.Create;
  42.   try
  43.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  44.  
  45.     // 1. Set JAVA_HOME
  46.     if Reg.OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', True) then
  47.     begin
  48.       Reg.WriteString('JAVA_HOME', JdkPath);
  49.       Reg.CloseKey;
  50.     end;
  51.  
  52.     // 2. Update PATH
  53.     if Reg.OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', False) then
  54.     begin
  55.       PathValue := Reg.ReadString('Path');
  56.       if Pos(JdkPath + 'bin', PathValue) = 0 then
  57.       begin
  58.         PathValue := JdkPath + 'bin;' + PathValue;
  59.         Reg.WriteString('Path', PathValue);
  60.       end;
  61.       Reg.CloseKey;
  62.     end;
  63.  
  64.     // Broadcast environment variable change notification
  65.     SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LPARAM(PChar('Environment')), SMTO_ABORTIFHUNG, 5000, nil);
  66.  
  67.     Result := True;
  68.   except
  69.     on E: Exception do
  70.       lblStatus.Caption := 'Error:' + E.Message;
  71.   end;
  72.   Reg.Free;
  73. end;
  74.  
  75.  
  76.  
  77. // Set environment variables
  78. procedure TForm2.btnSetEnvClick(Sender: TObject);
  79. begin
  80.   was JdkPath: string;
  81.   begin
  82.     JdkPath := IncludeTrailingPathDelimiter(edtJdkPath.Text);
  83.  
  84.     if not DirectoryExists(JdkPath) then
  85.     begin
  86.       lblStatus.Caption := 'Error: JDK path does not exist! ';
  87.       Exit;
  88.     end;
  89.  
  90.     if not FileExists(JdkPath + 'bin\java.exe') then
  91.     begin
  92.       lblStatus.Caption := 'Error: java.exe not found';
  93.       Exit;
  94.     end;
  95.  
  96.     if SetJavaEnvironment(JdkPath) then
  97.       lblStatus.Caption := 'Success: Environment variables have been set. Please restart the command line tool to verify.'
  98.     else
  99.       lblStatus.Caption := 'Error: Setting failed! ';
  100.   end;
  101. end;
  102.  
  103. // Verify Java installation
  104. procedure TForm2.btnVerifyClick(Sender: TObject);
  105. begin
  106.   //winexec(PAnsiChar(AnsiString('cmd.exe /c java -version')), sw_hide);
  107.   WinExec('cmd.exe /k java -version', SW_SHOWNORMAL);
  108. end;
  109.  
  110. procedure TForm2.FormCreate(Sender: TObject);
  111. begin
  112.   edtJdkPath.Text := 'X:\jdk-11';
  113. end;
  114.  
  115. end.
Advertisement
Add Comment
Please, Sign In to add comment