mis82
asked on
call java compiler from my delphi application
hello
how to call the java compiler to compile the text that in a text area in my application in delphi?
and how can i get the errors log file?
thx
how to call the java compiler to compile the text that in a text area in my application in delphi?
and how can i get the errors log file?
thx
Hi,
What I would do:
1. Save the text in a file (something like: RichEdit1.Lines.SaveToFile ('SourceFi leName'); if your text area is rich edit control)
2. Call your (command line?) compiler passing all necessary parameters including the source file and a file for errors:
function ExecApplication(APPName, CmdLine: String; ShowMode: DWord; WaitToExit: Boolean): DWord;
var StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
begin
FillChar(StartInfo, SizeOf(StartInfo), 0);
StartInfo.cb:=SizeOf(Start Info);
StartInfo.dwFlags:=STARTF_ USESHOWWIN DOW;
StartInfo.wShowWindow:=Sho wMode;
if AppName<>''
then CreateProcess(PChar(APPNam e), 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(ProcIn fo.hProces s, INFINITE);
GetExitCodeProcess(ProcInf o.hProcess , Result);
CloseHandle(ProcInfo.hProc ess);
CloseHandle(ProcInfo.hThre ad);
end;
// usage:
ExecApplication('', 'your command line with parameters here', SW_SHOWNORMAL, true);
3. Load the errors log in another TMemo or TRichEdit control:
RichEdit2.Lines.LoadFromFi le('LogFil eName');
Regards, Geo
What I would do:
1. Save the text in a file (something like: RichEdit1.Lines.SaveToFile
2. Call your (command line?) compiler passing all necessary parameters including the source file and a file for errors:
function ExecApplication(APPName, CmdLine: String; ShowMode: DWord; WaitToExit: Boolean): DWord;
var StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
begin
FillChar(StartInfo, SizeOf(StartInfo), 0);
StartInfo.cb:=SizeOf(Start
StartInfo.dwFlags:=STARTF_
StartInfo.wShowWindow:=Sho
if AppName<>''
then CreateProcess(PChar(APPNam
nil, nil, StartInfo, ProcInfo)
else CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0,
nil, nil, StartInfo, ProcInfo);
if WaitToExit
then WaitForSingleObject(ProcIn
GetExitCodeProcess(ProcInf
CloseHandle(ProcInfo.hProc
CloseHandle(ProcInfo.hThre
end;
// usage:
ExecApplication('', 'your command line with parameters here', SW_SHOWNORMAL, true);
3. Load the errors log in another TMemo or TRichEdit control:
RichEdit2.Lines.LoadFromFi
Regards, Geo
Oops sorry, i didn't see this
"compile the text that in a text area in my application "
if its in a memo, save it to file first
Memo1.Lines.SaveToFile....
Then use the code above to execute and compile that file
If its not in a Memo, create aTStringList, add the text to that, and then call its SaveToFile
Shane
Dear experts you forget one situation.
If source file contains error and any warnings your programs will not work.
You need get compiler warnings, hints , errors etc.
I have created procedure for compiling and getting result with Pascal Compiler because i have not Java. But idea is same. You can change and make it for java compiler.
procedure TForm1.Run1Click(Sender: TObject);
var
fname:string;
sDir: String;
erri: Integer;
begin
memo1.Lines.SaveToFile(sdi r+'\compil er\ce.PAS' ); //saves your file to file.pas
shellexecute(handle,'Open' ,pchar(sdi r+'\compil er\BPC.EXE '),pchar(s dir+'\comp iler\ce.PA S >'+sdir+'\compiler\CE.ERR' ),pchar(sd ir+'\compi ler\'),0);
//executes compiler and saves compiler results(hints,warnings and errors) to ce.err file
if fileexists(sdir+'\compiler \CE.ERR') then begin
listbox1.Items.LoadFromFil e(sdir+'\c ompiler\CE .ERR'); // loads compiler results(hints,warnings and errors) to Listbox1
listbox1.ItemIndex:=listbo x1.Items.C ount-1; //scrools listbox1 down
end;
end;
All is fine. If your HDD`s speed is low or your file system not works quick you will be problems on opening err file.
Best regards, Adil Aliev
If source file contains error and any warnings your programs will not work.
You need get compiler warnings, hints , errors etc.
I have created procedure for compiling and getting result with Pascal Compiler because i have not Java. But idea is same. You can change and make it for java compiler.
procedure TForm1.Run1Click(Sender: TObject);
var
fname:string;
sDir: String;
erri: Integer;
begin
memo1.Lines.SaveToFile(sdi
shellexecute(handle,'Open'
//executes compiler and saves compiler results(hints,warnings and errors) to ce.err file
if fileexists(sdir+'\compiler
listbox1.Items.LoadFromFil
listbox1.ItemIndex:=listbo
end;
end;
All is fine. If your HDD`s speed is low or your file system not works quick you will be problems on opening err file.
Best regards, Adil Aliev
I am told that the compiler sends error messages to the standard ERROR
STREAM and NOT to the standard OUTPUT STREAM.
see this link for 'Capture the output from a DOS Command'
http://delphi.about.com/cs/adptips2001/a/bltip0201_2.htm
Stderror is not assigned in that exmample but, just add something like this to test it:
.
.
.
{start.cb := SizeOf(start);
start.hStdOutput := WritePipe;
start.hStdInput := ReadPipe;}
start.hStdError := WritePipe; // add this line, or create another pipe
for the errror messages
start.dwFlags := STARTF_USESTDHANDLES +
.
Shane
STREAM and NOT to the standard OUTPUT STREAM.
see this link for 'Capture the output from a DOS Command'
http://delphi.about.com/cs/adptips2001/a/bltip0201_2.htm
Stderror is not assigned in that exmample but, just add something like this to test it:
.
.
.
{start.cb := SizeOf(start);
start.hStdOutput := WritePipe;
start.hStdInput := ReadPipe;}
start.hStdError := WritePipe; // add this line, or create another pipe
for the errror messages
start.dwFlags := STARTF_USESTDHANDLES +
.
Shane
If the compiler can write the results of comiling to a file and you pass the correct command line then my example will work while Aliev's won't. It doesn't matter how fast is your computer. ShellExecute doesn't wait for the started program to finish so you'll either not find the results file or open an old one (from a previous compilation).
geobul, I said that because i had this problem.
ASKER
Hello;
i got an error after running the application(Could not find the main class)
i think the error in the parameter i think the syntax is error in shaneholmes code(in parameter line)
acctually i dont know how to use the pararmeter
anybody can help ??? and what about the error log for java?
thanks all
i got an error after running the application(Could not find the main class)
i think the error in the parameter i think the syntax is error in shaneholmes code(in parameter line)
acctually i dont know how to use the pararmeter
anybody can help ??? and what about the error log for java?
thanks all
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
javac [ options ] [ sourcefiles ] [ @argfiles ]
procedure TForm1.Button1Click(Sender
var
App: Pchar;
Parameters: Pchar;
MyHandle: Hwnd;
Directory: Pchar;
begin
MyHandle := Application.Handle; {Could be Form1.Handle}
App := 'Javac'; {Any application you want to run}
Parameters := [[ options ] [ sourcefiles ] [ @argfiles ]} {Whatever params you need here}
Directory := 'C:\Java\'; { the directory of the app}
ShellExecute(MyHandle,nil,
end;
Shane