Link to home
Start Free TrialLog in
Avatar of mis82
mis82

asked on

ShellExecute and javac parameters

This my code:

  MyHandle := Form1.Handle;
  App := 'javac.exe';
  Parameters :='C:\hello.java';
  Directory := 'C:\jdk1.3.1_09\bin';
  ShellExecute(MyHandle,nil, App,Parameters, Directory , SW_SHOW);



i want to write this on shellexecute to get the error compilation log file

javac hello.java 2> err.lst

how can i use the that code to do this? (i think something in the parameters)
and how can i keep the dos screen  from disappearing quickly?
Avatar of mokule
mokule
Flag of Poland image


Create bat file

C:\jdk1.3.1_09\bin\javac.exe C:\hello.java 2>err.lst
pause

and execute it in place of javac.exe
Avatar of Ferruccio Accalai
Yes, you have to add it to parameters:

 Parameters :='C:\hello.java 2> err.lst'
-->how can i keep the dos screen  from disappearing quickly?
I suggest to use the JCL SHellExecuteAndWait version as the following:

uses
WIndows, SHellApi

function ShellExecAndWait(const FileName: string; const Parameters: string;
  const Verb: string; CmdShow: Integer): Boolean;
  function PCharOrNil(const S: AnsiString): PAnsiChar;
  begin
    if Length(S) = 0 then
      Result := nil
    else
      Result := PAnsiChar(S);
  end;
var
  Sei: TShellExecuteInfo;
  Res: LongBool;
  Msg: tagMSG;
begin
  FillChar(Sei, SizeOf(Sei), #0);
  Sei.cbSize := SizeOf(Sei);
  Sei.fMask := SEE_MASK_DOENVSUBST  or SEE_MASK_FLAG_NO_UI  or SEE_MASK_NOCLOSEPROCESS or
    SEE_MASK_FLAG_DDEWAIT;
  Sei.lpFile := PChar(FileName);
  Sei.lpParameters := PCharOrNil(Parameters);
  Sei.lpVerb := PCharOrNil(Verb);
  Sei.nShow := CmdShow;
  Result := ShellExecuteEx(@Sei);
  if Result then
  begin
    WaitForInputIdle(Sei.hProcess, INFINITE);
    while (WaitForSingleObject(Sei.hProcess, 10) = WAIT_TIMEOUT) do
    begin
      repeat
        Res := PeekMessage(Msg, Sei.Wnd, 0, 0, PM_REMOVE);
        if Res then
        begin
          TranslateMessage(Msg);
          DispatchMessage(Msg);
        end;
      until (Res = False);
    end;
    CloseHandle(Sei.hProcess);
  end;
end;

and call it as follows:
ShellExecAndWait('C:\jdk1.3.1_09\bin\javac.exe ','C:\hello.java 2>err.lst','Open', SW_SHOW);
Avatar of mis82
mis82

ASKER

hello Ferruccio68
i tried it with err.lst   ...not working
and i tried without it  its oky.
and also the dos window disappearing ...
and what about using
 
Winexec('javac hello.java 2> err.lst',SW_SHOW);

or with the full path

WinExec('C:\jdk1.3.1_09\bin\javac.exe C:\hello.java 2>err.lst',SW_SHOW);

Avatar of mis82

ASKER

Also not working with the 2>err.lst

What is wrong with my suggestion? Didn't You follow it?
Avatar of mis82

ASKER

everything is ok  , except the err.lst creation
and also the cmd screen hide quickly
Avatar of mis82

ASKER

i meen when i use ur code without err.lst it works
and with it the compilation fails
and also the cmd screen pop up and close quickly
Avatar of mis82

ASKER

mokule
acctually i dont know how to create a batch file
Open a notepad type

C:\jdk1.3.1_09\bin\javac.exe C:\hello.java 2>err.lst
pause

and save as java.bat

or do You like do it from program?
Avatar of mis82

ASKER

ok but how can i pass the file name everytime to the bat file?
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just one question: how do you write the command line usually into the dos prompt?
Ferruccio:
If it was question to me. I didn't understand it. Maybe it's my poor English. Please clarify.
Sorry mokule, my question was directed to mis82 :)
Avatar of mis82

ASKER

javac hello.java 2>err.lst

hello.java on c:
and the javac path included in the system path to run it from any level
Avatar of mis82

ASKER

mokule
also the err.lst ....didnt created
So try using createprocess directly....

a function from a my application:

function ExecApplication(APPName, CmdLine: String; ShowMode: DWord; WaitToExit: Boolean; Title: PCHAR): DWord;
var StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
begin
      try
         FillChar(StartInfo, SizeOf(StartInfo), 0);
              StartInfo.cb:=SizeOf(StartInfo);
              StartInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESIZE or STARTF_USEPOSITION;
                                StartInfo.wShowWindow:=ShowMode;
              startinfo.lpTitle := title;
                               
       
      if AppName <> '' then
            CreateProcess(PChar(APPName), PChar(CmdLine), nil, nil, False, 0,
                         nil, nil, StartInfo, ProcInfo)
              else
            CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0,
                         nil, nil, StartInfo, ProcInfo);
           
              if WaitToExit then
            WaitForSingleObject(ProcInfo.hProcess, INFINITE);
       
        GetExitCodeProcess(ProcInfo.hProcess, Result);
     
      finally
   
      CloseHandle(ProcInfo.hProcess);
      CloseHandle(ProcInfo.hThread );
 
       end;
end;

And call as ExecApplication('','javac hello.java 2>err.lst',SW_SHOWNORMAL,true,'');
mis82
I've tested my solution. It works. Maybe You've got some problems with folders.

This is my code:

    ShellExecute(Form1.Handle,nil,'simple.bat','hello.java',nil,SW_SHOW);

This is my simple.bat file:

C:\j2sdk1.4.0_02\bin\javac.exe %1 2>err.lst
pause

This is my err.lst:

error: cannot read: hello.java
1 error
Avatar of mis82

ASKER

Hello
mokule u r right thx
just another small question

how can i run the compiled java application

i use

c:\java  hello

how can i do it by the batch file?
Hi

ShellExecute(Form1.Handle,nil,'simple.bat','hello.java hello',nil,SW_SHOW);

simple.bat

C:\j2sdk1.4.0_02\bin\javac.exe %1 2>err.lst
pause
if errorlevel 1 goto _error
C:\j2sdk1.4.0_02\bin\java %2
:_error
pause

----------------------------------------
In command prompt You can enter help to have some help for batch commands