Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Error al ejecutar programa java con ShellExecute (https://www.clubdelphi.com/foros/showthread.php?t=87908)

newtron 17-03-2015 13:23:02

Error al ejecutar programa java con ShellExecute
 
Hola a tod@s.

Estoy intentando ejecutar un programa java con ShellExecute con la siguiente instrucción:

Código Delphi [-]
 ShellExecute(Handle,
               'open',
               'java',
               '-jar FirEleFa.jar D:\TEMP\A-000001.xml D:\TEMP\A-000001_Firmada.XML Explorer',
               nil,
               SW_SHOWNORMAL);

pero en vez de ejecutar el programa se me abre una ventana con la carpeta C:\WINDOWS\JAVA

Logicamente he probado desde la linea de comandos y lo hace correctamente.

¿Alguien sabe por qué tiene este comportamiento?

jgutti 17-03-2015 14:09:06

utilizo esta funciones para ejecutar rutinas java desde delphi:
Código Delphi [-]
uses ShellApi
function IsWinNT: boolean;
var
  OSV: OSVERSIONINFO;
begin
  OSV.dwOSVersionInfoSize := sizeof(osv);
  GetVersionEx(OSV);
  result := OSV.dwPlatformId = VER_PLATFORM_WIN32_NT;
end;
function ejecutarComando (comando : string) : string;
var
  Buffer: array[0..4096] of Char;
  si: STARTUPINFO;
  sa: SECURITY_ATTRIBUTES;
  sd: SECURITY_DESCRIPTOR;
  pi: PROCESS_INFORMATION;
  newstdin, newstdout, read_stdout, write_stdin: THandle;
  exitcod, bread, avail: Cardinal;
begin
  Result:= '';
  if IsWinNT then
  begin
    InitializeSecurityDescriptor(@sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(@sd, true, nil, false);
    sa.lpSecurityDescriptor := @sd;
  end
  else sa.lpSecurityDescriptor := nil;
  sa.nLength := sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle := TRUE;
  if CreatePipe(newstdin, write_stdin, @sa, 0) then
  begin
    if CreatePipe(read_stdout, newstdout, @sa, 0) then
    begin
      GetStartupInfo(si);
      with si do
      begin
        dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
        wShowWindow := SW_HIDE;
        hStdOutput := newstdout;
        hStdError := newstdout;
        hStdInput := newstdin;
      end;
      Fillchar(Buffer, SizeOf(Buffer), 0);
      GetEnvironmentVariable('COMSPEC', @Buffer, SizeOf(Buffer) - 1);
      StrCat(@Buffer,PChar(' /c ' + comando));
      if CreateProcess(nil, @Buffer, nil, nil, TRUE, CREATE_NEW_CONSOLE, nil, nil, si, pi) then
      begin
        repeat
          PeekNamedPipe(read_stdout, @Buffer, SizeOf(Buffer) - 1, @bread, @avail, nil);
          if bread > 0 then
          begin
            Fillchar(Buffer, SizeOf(Buffer), 0);
            ReadFile(read_stdout, Buffer, bread, bread, nil);
            Result:= Result + String(PChar(@Buffer));
          end;
          Application.ProcessMessages;
          GetExitCodeProcess(pi.hProcess, exitcod);
        until (exitcod <> STILL_ACTIVE) and (bread = 0);
      end;
      CloseHandle(read_stdout);
      CloseHandle(newstdout);
    end;
    CloseHandle(newstdin);
    CloseHandle(write_stdin);
  end;
end;

procedure TForm1.PostBtnClick(Sender: TObject);
begin
txtComando.Text:='c:\jdk1.7.0\bin\java -jar FirEleFa.jar D:\TEMP\A-000001.xml D:\TEMP\A-000001_Firmada.XML Explorer';
txtSalida.Text := ejecutarComando(txtComando.Text);

end;

newtron 17-03-2015 17:11:33

jgutti.

Gracias por tu post. Tampoco me funciona de esta manera pero si me ha servido para darme cuenta de que el problema por lo visto es que tengo varias versiones de java instaladas y desde delphi se hace un pequeño lío a la hora de buscar el java.exe.

Cuando pongas código intenta ponerlo entre los delimitadores de DELPHI /DELPHI (entre corchetes) para que el código sea más legible.

Gracias de nuevo por tu respuesta y un saludo.

AgustinOrtu 17-03-2015 18:36:36

Probaste pasando las rutas de los archivos entre ""?

newtron 17-03-2015 19:12:16

Cita:

Empezado por AgustinOrtu (Mensaje 490093)
Probaste pasando las rutas de los archivos entre ""?

No, no le estaba poniendo rutas porque se supone que el java tiene que cogerlo del path. Lo que estoy haciendo para intentar resolver el conflicto de versiones es copiar el java a la carpeta de mi programa y ejecutarlo desde ahí.

darkerbyte 20-07-2015 22:40:56

Mil Gracias jgutti tu función me ha servido excelente. Un saludo


La franja horaria es GMT +2. Ahora son las 21:14:13.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi