I want associate my .jar file to open with java.exe using Windows registry and have a doubt about how return the complete path of java.exe file ignoring java version installed on computer.
Ex:
in my case i have:
C:\Program Files\Java\jdk1.7.0_45\bin\java.exe
then how access java.exe file ignoring this part 1.7.0_45?
uses
Windows, Registry;
function GetProgramFilesDir: string;
var
reg: TRegistry;
begin
reg := TRegistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion', False);
Result := reg.ReadString('ProgramFilesDir');
finally
reg.Free;
end;
end;
procedure RegisterFileType(cMyExt, cMyFileType, ExeName: string);
var
reg: TRegistry;
begin
reg := TRegistry.Create;
try
reg.RootKey := HKEY_CURRENT_USER;
if reg.OpenKey('\Software\Classes\.jar', True) then
reg.WriteString('', 'MyAppDataFile');
if reg.OpenKey('\Software\Classes\MyAppDataFile', True) then
reg.WriteString('', 'myappname');
if reg.OpenKey('\Software\Classes\MyAppDataFile\DefaultIcon', True) then
reg.WriteString('', GetProgramFilesDir + '\Java\jdk1.7.0_45\bin\java.exe');
if reg.OpenKey('\Software\Classes\MyAppDataFile\shell\open\command', True)
then
reg.WriteString('', GetProgramFilesDir + '\Java\jdk1.7.0_45\bin\java.exe "%1"');
finally
reg.Free;
end;
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
end;
Don't use the Registry to discover the path to system folders, like
Program Files. UseSHGetFolderPath()orSHGetKnownFolderPath()instead, eg:That being said, to get the current installed path of
java.exe, there are a few options you can try:check if the
%JAVA_HOME%environment variable is set.check the
HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\<version>Registry key for aJavaHomevalue (there may be multiple<version>subkeys!).search the
%PATH%environment variable for any listed folder that hasjava.exein it (there may be multiple folders!). You can parse the%PATH%yourself manually, or you can useSearchPath()with itslpPathparameter set to NULL (if you only care about the first copy ofjava.exefound).Also, don't forget that paths with spaces must be wrapped in double-quotes. You can use Delphi's
AnsiQuotedStr()to help you with that, eg: