环境变量.JAVA_HOME

Posted CodeSkill

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了环境变量.JAVA_HOME相关的知识,希望对你有一定的参考价值。

1、资料:(我的文章)

  http://www.cnblogs.com/cppskill/p/8341263.html

2、

  2.1、界面

  2.2、代码

 

unit formMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Registry, IniFiles;

type
  TfrmMain = class(TForm)
    btnRegeditWrite: TButton;
    Memo1: TMemo;
    btnRegeditRead: TButton;
    edtJavaHome: TEdit;
    procedure btnRegeditWriteClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnRegeditReadClick(Sender: TObject);
  public
    function ReadString( _dwRootKey :DWORD; const _strPath, _strKey :string; var _strOut : string):Integer;
    function WriteString(_dwRootKey :DWORD; const _strPath, _strKey, _strValue :string):Integer;
  public
    function ReadIni():integer;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

function TfrmMain.ReadString(_dwRootKey :DWORD; const _strPath, _strKey: string; var _strOut: string): Integer;
var reg :TRegistry;
begin
  Result := 0;
  reg := TRegistry.Create;
  try
    reg.RootKey := _dwRootKey;//HKEY_LOCAL_MACHINE; //\'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\'
    if not reg.OpenKey( _strPath, false ) then
    begin
      Result := -1;
      Exit;
    end;

    _strOut := reg.ReadString(_strKey);
  finally
    reg.CloseKey;
    reg.Destroy;
  end;
end;

function TfrmMain.WriteString(_dwRootKey: DWORD; const _strPath, _strKey, _strValue: string): Integer;
var reg :TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := _dwRootKey;//HKEY_LOCAL_MACHINE; //\'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\'
    if not reg.OpenKey( _strPath, false ) then
    begin
      Result := -1;
      Exit;
    end;

    reg.WriteString( _strKey, _strValue );
  finally
    reg.CloseKey;
    reg.Destroy;
  end;
end;


procedure TfrmMain.FormCreate(Sender: TObject);
begin
  ReadIni;
  btnRegeditReadClick(nil);
end;

procedure TfrmMain.btnRegeditWriteClick(Sender: TObject);
var strValue :string;
begin
  Memo1.Lines.Add(\'\');
// HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Session Manager\\Environment\\path
// HKEY_CURRENT_USER \\Environment
// HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment (官方)

  //strValue := \'C:\\Program Files\\Java\\jdk1.8.0_152;AAA\';
  //strValue := \'C:\\Program Files\\Java\\jdk1.8.0_152\';
  strValue := trim(edtJavaHome.Text);
  if (strValue = \'\') then
  begin
    Memo1.Lines.Add(\'新的 JAVA_HOME 的值为空 .\');
    Exit;
  end;
  
  if 0>WriteString(HKEY_LOCAL_MACHINE,
    \'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\', \'JAVA_HOME\', strValue) then
  begin
    Memo1.Lines.Add(\'注册表写入失败 .\');
    Exit;
  end;
  Memo1.Lines.Add(\'注册表写入成功 .\');
end;

procedure TfrmMain.btnRegeditReadClick(Sender: TObject);
var strValue :string;
begin
  Memo1.Lines.Add(\'\');

  if (0 > ReadString(HKEY_LOCAL_MACHINE,
    \'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\', \'JAVA_HOME\', strValue)) then
  begin
    Memo1.Lines.Add(\'读取 注册表 出错\');
    Exit;
  end;
  Memo1.Lines.Add(\'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\\JAVA_HOME\');
  Memo1.Lines.Add(\'   \'+strValue);
end;

function TfrmMain.ReadIni(): integer;
var ini :Tinifile;
    strX86, strX64 :string;
    strFileName :string;
begin
  strFileName := ExtractFilePath(Paramstr(0)) + \'_EnvironmentVariable.ini\';
  ini := Tinifile.Create(strFileName);
  try
    strX86 := ini.ReadString(\'JAVA_HOME\', \'Win7x86\', \'\');
    strX64 := ini.ReadString(\'JAVA_HOME\', \'Win7x64\', \'\');

    Memo1.Lines.Add(\'JAVA_HOME(Win7x86) : \'+strX86);
    Memo1.Lines.Add(\'JAVA_HOME(Win7x64) : \'+strX64);
  finally
    ini.Free;
  end;
end;

end.

 

  2.3、ini文件(_EnvironmentVariable.ini)

 

 

3、

4、

5、

 

以上是关于环境变量.JAVA_HOME的主要内容,如果未能解决你的问题,请参考以下文章

Java环境变量配置

JDK安装与环境变量配置

如何在应用程序启动脚本中用环境变量替换硬编码的 JAVA_HOME?

java_home环境变量配置的问题?

JDK安装与环境变量配置(windows)

如何设置 JAVA_HOME 环境变量以使 Android 工具正常工作?