Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Ejecutar archivo .JAR desde DElphi (https://www.clubdelphi.com/foros/showthread.php?t=86033)

jscubillos5 10-06-2014 00:13:47

Ejecutar archivo .JAR desde DElphi
 
Hola comunidad he estado buscando como ejecutar un archivo .JAR desde delphi, (XE5), y he encontrado varia información al respecto: http://www.clubdelphi.com/foros/showthread.php?t=41916, pero las he probado y ninguna me ha funcionado, actualmente este es el código de pascal que tengo:

Código Delphi [-]
if (FileExists('API_WS_SDL_SIMPLE/API_WS_SDL_SIMPLE.jar')) then
       begin
         LineaDeComandos:= '"' + '1' + '"' + ' ' + '"' + '1' + '"' + ' ' + '"' + IntToStr(IDTIPODOCUMENTO) + '"' + ' ' + '"' + NUMERODOCUMENTO + '"' + ' ' + '"' + CORREO +
         '"' + ' ' + '"' + DIRECCION + '"' + ' ' + '"' + TELEFONO + '"' + ' ' + '"' + '14' + '"' + ' ' + '"' + IntToStr(IDDEPARTAMENTOEXPEDICIONDOCUMENTO) + '"' + ' ' + '"' + 
        IntToStr(IDCIUDADEXPEDICIONDOCUMENTO) + '"' + ' ' + '"' + '14' + '"' + ' ' + '"' + IntToStr(IDDEPARTAMENTODOMICILIO) + '"' + ' ' + '"' + IntToStr(IDCIUDADDOMICILIO) +
        '"' + ' ' + '"' + DateToStr(FECHAEXPEDICIONDOCUMENTO) + '"' + ' ' + '"' + DateToStr(FECHANACIMIENTO) + '"' + ' ' + '"' + PRIMERAPELLIDO + '"' + ' ' + '"' + SEGUNDOAPELLIDO +
        '"' + ' ' + '"' + NOMBRES + '"' + ' ' + '"' + '14' + '"';
         //WinExec(LineadeComandos, SW_SHOWMINNOACTIVE);
         ShellExecute(0,'open','JAVA API_WS_SDL_SIMPLE.JAR', PChar(LineadeComandos),'API_WS_SDL_SIMPLE/',SW_SHOWNORMAL);
         while not FileExists('API_WS_SDL_SIMPLE/' + NUMERODOCUMENTO + '.fin') do
         begin
           Sleep(1);
         end;
       end
       else
       begin
         Respuesta:= 'Atención ha ocurrido un error Critico, por favor detenga todos los procesos e informe al area de sistemas el error: Not exist API_WS_SDL_SIMPLE, not connection with Deceval';
         raise Exception.Create(Respuesta);
       end;

Pero pues no me funciona esta no ejecuta el .jar, adicionalmente tengo un .bat con el siguiente comando:

java -jar API_WS_SDL_SIMPLE.jar "1" "1" "1" "1018458060" "[email protected]" "CR 78 # 1-03" "2936359" "14" "5" "150" "14" "6" "151" "08-06-2014" "08-06-2014" "C" "G" "JUAN" "14"
PAUSE

Y este si funciona, ¿Podria alguno de ustedes ayudarme a ejecutar el .jar?

jgutti 10-06-2014 15:43:37

Juan, utiliza esta rutina para firmar archivos xml utilizando rutina.jar

txtComando.Text:='c:\jdk1.7.0_13\bin\java -jar rutina.jar firma archivos.xml archivo_salida.xml ;
txtSalida.Text := ejecutarComando(txtComando.Text);

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;

saludos
jgutti

Casimiro Noteví 10-06-2014 16:31:33

Recuerda poner los tags al código fuente, ejemplo:



Gracias :)

jscubillos5 13-06-2014 16:31:51

Respuesta
 
Hola jgutti, agradezco tu ayuda, intente probar tu rutina y no me funciono, así que tuve que implementar otra solución, así que aquí se las explico, tuve que colocar un intermediario entre el ejecutable de Pacal y el Jar de Java, en este caso como lo tengo en un equipo con arquitectura Windows, construí un archivo por lotes .BAT, en el código fuente de Pascal coloque la siguiente instrucción:

Código Delphi [-]
/* Linea de comandos es una variable de tipo: LineadeComandos: PAnsiChar; en esta se encuentra el llamado al archivo .BAT y unos parametros que este recibe.*/
LineaDeComandos:= PAnsiChar(AnsiString('Enviar al JAR.bat' + ' ' + '"1"' + ' ' + '"' + ExtractFilePath(ParamStr(0)) +  NombreArchivoTemporal + '"'));
// Luego solo tuve que ejecutar el BAT por medio de WinExec, que hereda de la clase: Winapi.Windows
         WinExec(LineaDeComandos, SW_HIDE);

El código de .BAT es el siguiente:

Código:

CD "API_WS_SDL_SIMPLE"
java -jar API_WS_SDL_SIMPLE.jar %1 %2
PAUSE

Si ven el .BAT es un código simple que llama a java y activa el MAIN del .JAR, los 2 porcentajes, (%1 %2), son las variables que estoy enviando desde Pascal.


La franja horaria es GMT +2. Ahora son las 05:49:26.

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