TProcess Java program lazarus / 61-Beginners ---------------------------------------------------------------------- Trenatos · 2015-08-10 19:23 I'm trying to find a way to run a java program through TProcess. I found an example here:  http://wiki.freepascal.org/Executing_Externa l_Programs#Reading_large_output But if I change the dir to anything else, I just get the Invalid command, please fix the IFDEFS. message. I've tried mucking with it, but no success. I can't even get java -version to run and return output. If someone could help me with just java -version that would be a lot of help ---------------------------------------------------------------------- derek.john.evans · 2015-08-10 20:40 Here is some code Im working on for my app. Im running GCC via TProcess and displaying output in a TSynEdit control. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function StreamReadChunk(const AStream: TStream): String;var  LBuffer: array[byte] of Char;begin  LBuffer[0] := #0;  SetString(Result, LBuffer, AStream.Read(LBuffer, SizeOf(LBuffer)));end; // **EDIT** Added BeginUpdate/EndUpdate.procedure StringsWrite(const AStrings: TStrings; const ALine: String);var  LLastLine: String;begin  AStrings.BeginUpdate;  try    if AStrings.Count = 0 then    begin    LLastLine := EmptyStr;    end else begin      LLastLine := AStrings[AStrings.Count - 1];      AStrings.Delete(AStrings.Count - 1);    end;    AStrings.AddText(LLastLine + ALine + LineEnding);  finally    AStrings.EndUpdate;  end;end;  procedure SynEditWrite(const ASynEdit: TSynEdit; const ALine: String);begin  StringsWrite(ASynEdit.Lines, ALine);  while ASynEdit.Lines.Count > 500 do  begin    ASynEdit.Lines.Delete(0);  end;  ASynEdit.CaretY := MaxInt;  Application.ProcessMessages;end; procedure ProcessExecute(const AProcess: TProcess; const ASynEdit: TSynEdit);begin  with AProcess do  begin    Options := Options + [poUsePipes, poStderrToOutPut];    PipeBufferSize := 1024;    ShowWindow := swoHIDE;    Execute;    while Running do    begin      while Running and (Output.NumBytesAvailable = 0) do      begin        Sleep(400);      end;      SynEditWrite(ASynEdit, StreamReadChunk(Output));    end;    repeat      SynEditWrite(ASynEdit, StreamReadChunk(Output));    until Output.NumBytesAvailable = 0;  end;end; I tested it with this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for LIndex := 1 to 10 do  begin    Process1.Executable := 'java';    Process1.Parameters.Text := '-version';    ProcessExecute(Process1, SynEdit1);    Process1.Executable := 'gcc';    Process1.Parameters.Text := '--h';    ProcessExecute(Process1, SynEdit1);  end; ---------------------------------------------------------------------- marcov · 2015-08-10 20:55 It is hard to guess what goes wrong. And example I could give here is also in the fact. Why don't you show some code? Don't forget to specify the path to the java executable ---------------------------------------------------------------------- derek.john.evans · 2015-08-10 21:11 Ha, I get it. You see the string 'invalid command, please fix the IFDEFS.' That example looks all hacked up. It looks like someone put that message there to make sure the command was set correctly for different platforms. Then, someone else took it out because CommandLine is deprecated. Which is fine, but somehow that string is still ending up being used for something. I'd remove the var OurCommand, and let the compiler find the issue. ---------------------------------------------------------------------- Trenatos · 2015-08-10 21:49 derek, when I try your code, I get an external SIGSEGV I'm on Windows btw, not sure that matters. ---------------------------------------------------------------------- Trenatos · 2015-08-10 21:53 Bah, forgot to initialize the process. With that done, I'm now getting output! It's weird output, but output nonetheless! ---------------------------------------------------------------------- Trenatos · 2015-08-10 22:03 If I do: Process1.Executable := 'java'; Process1.Parameters.Text := '-version'; I get no output to SynEdit If I do: Process1.Executable := 'cmd.exe'; Process1.Parameters.Text := 'java -version'; I get a console window that never goes away, and locks everything up, but I do get console output: Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation.  All rights reserved. C:\someFolder> ---------------------------------------------------------------------- Trenatos · 2015-08-10 22:11 Even weirder, GCC help does give output, but Java does not...? ---------------------------------------------------------------------- Trenatos · 2015-08-10 22:14 The plot thickens. I'm getting some Java output now, but... I'm getting: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' has value '1.8', but '1.7' is required. Error: could not find java.dll Error: Could not find Java SE Runtime Environment. .Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' has value '1.8', but '1.7' is required. Error: could not find java.dll Error: Could not find Java SE Runtime Environment. ---------------------------------------------------------------------- Trenatos · 2015-08-10 22:20 Ok, I'm well aware that I'm rambling like a madman now. But I'm getting Java output  I had to enter the complete path to a java binary, I could not use just 'java' for the reason above. Not very portable, but I might have  away around that. ---------------------------------------------------------------------- Trenatos · 2015-08-10 22:23 And it works, I'm now able to launch an instance of Jetty on demand  You guys rock, without this thread there's no way I would have gotten this far. On to the next problem, when I hit that button, the whole thing locks up. I get the output, but it locks up the entire GUI, any simple way to fix that? ---------------------------------------------------------------------- derek.john.evans · 2015-08-10 22:55 Eck. Try TAsyncProcess Comment out some lines from ProcessExecute. Code: [Select]procedure ProcessExecute(const AProcess: TProcess; const ASynEdit: TSynEdit); begin   with AProcess do   begin     Options := Options + [poUsePipes, poStderrToOutPut];     PipeBufferSize := 1024;     ShowWindow := swoHIDE;     Execute;     (* <<<<<< REMOVE THE OLD streaming code.     while Running do     begin       while Running and (Output.NumBytesAvailable = 0) do       begin         Sleep(400);       end;       SynEditWrite(ASynEdit, StreamReadChunk(Output));     end;     repeat       SynEditWrite(ASynEdit, StreamReadChunk(Output));     until Output.NumBytesAvailable = 0;     *) <<< END COMMENT   end; end; Add a OnReadData event to TAsyncProcess. Code: [Select]procedure TForm1.AsyncProcess1ReadData(Sender: TObject); begin   repeat     SynEditWrite(SynEdit1, StreamReadChunk(AsyncProcess1.Output));   until AsyncProcess1.Output.NumBytesAvailable = 0; end; But, you still can only run one process at a time, so you need to make sure you check Running before calling Execute again: Code: [Select]  if not AsyncProcess1.Running then   begin     AsyncProcess1.Executable := 'gcc';     AsyncProcess1.Parameters.Text := '--h';     ProcessExecute(AsyncProcess1, SynEdit1);   end;  Bad news is, I now cant get java running. Gcc, yes ---------------------------------------------------------------------- Nicola Gorlandi · 2015-08-11 04:33 You can use this component http://maxxdelphisite.free.fr/doscmd.htm is for Delphi but it should run always on Lazarur (for me on windows it works) ---------------------------------------------------------------------- Trenatos · 2015-08-11 12:49 I have it working and launching Java software, next step is to see if I can make it handle multiple processes and how to thread things so it doesn't lock up the GUI