Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

call java compiler from my delphi application

Posted on 2004-04-08
11
Medium Priority
?
215 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:mis82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10784115
You will need to call the Java compiler using the ShelLExecute - passing it the command line parameters.

javac [ options ] [ sourcefiles ] [ @argfiles ]



procedure TForm1.Button1Click(Sender: TObject);
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, App, Parameters, Directory , SW_SHOWMINNOACTIVE);
end;

Shane
0
 
LVL 17

Expert Comment

by:geobul
ID: 10784119
Hi,

What I would do:

1. Save the text in a file (something like: RichEdit1.Lines.SaveToFile('SourceFileName'); 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(StartInfo);
  StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
  StartInfo.wShowWindow:=ShowMode;
  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);
  CloseHandle(ProcInfo.hProcess);
  CloseHandle(ProcInfo.hThread);
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.LoadFromFile('LogFileName');

Regards, Geo
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10784132

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Aliev
ID: 10784895
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(sdir+'\compiler\ce.PAS');   //saves your file to file.pas
    shellexecute(handle,'Open',pchar(sdir+'\compiler\BPC.EXE'),pchar(sdir+'\compiler\ce.PAS >'+sdir+'\compiler\CE.ERR'),pchar(sdir+'\compiler\'),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.LoadFromFile(sdir+'\compiler\CE.ERR');      // loads compiler results(hints,warnings and errors) to Listbox1
   
    listbox1.ItemIndex:=listbox1.Items.Count-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
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10785128
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
0
 
LVL 17

Expert Comment

by:geobul
ID: 10789600
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).
0
 
LVL 3

Expert Comment

by:Aliev
ID: 10790603
geobul, I said that because i had this problem.
0
 

Author Comment

by:mis82
ID: 10816734
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
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13284870
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question