Ce code permet d'executer un programme java en utilisant java.exe
Il est entièrement écrit avec les api windows pour avoir un executable de la plus petite taille possible.
Il verifie que le programme java.exe se trouve bien dans un des répertoires de la variable d'environnement path et vérifie aussi qu'un fichier .jar se trouve dans le répertoire courant.
Dans le cas contraire, un message d'erreur est affiché.
Le code fonctionne correctement sur win 2000, xp, et 2003 (sur la version 98 il faut rajouter dbghelp.dll que l'on peut trouver dans windows/system32 de win xp).
Source / Exemple :
unit UnitMain;
interface
uses
ShellApi, Windows;
procedure main;
implementation
function GetCurrentDir(): string;
const
SIZE: cardinal = 1024;
var
path: string;
l: DWORD;
begin
SetLength(path, SIZE);
l := GetCurrentDirectory(length(path), pchar(path));
SetLength(path, l);
result := path;
end;
function IncludeTrailingPathDelimiter(s: string): string;
begin
if ((s = '') or (s[length(s)] <> '\')) then
s := s + '\';
result := s;
end;
function FindFile(const FileName: string): pchar;
var
lpFindFileData: WIN32_FIND_DATA;
hFindFile: HWND;
cFileName: pchar;
begin
cFileName := nil;
hFindFile := FindFirstFile(pchar(FileName), lpFindFileData);
if (hFindFile <> INVALID_HANDLE_VALUE) then
begin
cFileName := lpFindFileData.cFileName;
FindClose(hFindFile);
end;
result := cFileName;
end;
function GetEnvironmentPath(): string;
const
SIZE: cardinal = 4096;
var
path: string;
l: DWORD;
begin
SetLength(path, SIZE);
l := GetEnvironmentVariable('Path', pchar(path), length(path));
SetLength(path, l);
result := path;
end;
function FindExecutableImage(FileName: pchar; SymbolPath: pchar; ImageFilePath: pchar): HWND;
stdcall; external 'dbghelp.dll';
procedure erreur(const msg: string);
begin
MessageBox(0, pchar(msg), nil, MB_OK or MB_ICONERROR or MB_APPLMODAL);
end;
procedure main();
const
lpFile: string = 'java.exe';
FileJar: string = '*.jar';
var
lpParameters: string;
ImageFilePath: array [0..1023] of char;
FilePath: string;
cFileName: pchar;
begin
if (FindExecutableImage(pchar(lpFile), pchar(GetEnvironmentPath), ImageFilePath) = 0) then
begin
erreur(lpFile + ' n''a pas été trouvé, vérifier la variable d''environnement path.');
exit;
end;
FilePath := IncludeTrailingPathDelimiter(GetCurrentDir);
cFileName := FindFile(FilePath + FileJar);
if (cFileName = nil) then
begin
erreur('Aucun fichier ' + FileJar + ' n''a été trouvé dans le répertoire courant');
exit;
end;
lpParameters := '-jar ' + FilePath + cFileName;
ShellExecute(0, 'open', pchar(lpFile), pchar(lpParameters), nil, 0);
end;
end.
Conclusion :
Il manque encore le test lorsque java.exe se trouve dans le même répertoire que le fichier jar mais j'ai préféré ne pas le rajouter.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.