Recent

Author Topic: [Solved] Launching thread on OSX fails, parameters not passed in?  (Read 5771 times)

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Next issue:  In my threads, I'm kicking off a java application with parameters.

Works fine on windows, but on OSX I get the same Java output as if you passed in no parameters.

I've confirmed in Terminal that the string I'm using for parameters works, just not when launched through the application with TProcessThread.Create(AExecutable, AParameters, ASynEdit.Name)
« Last Edit: August 25, 2015, 11:39:20 pm by Trenatos »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Launching thread on OSX fails, parameters not passed in?
« Reply #1 on: August 25, 2015, 04:38:16 pm »
Note that if you execute something on the command line, the arguments will be parsed by the shell and the result of that will be passed to the program, while if you use sysutils.executeprocess() or the tprocess class, the shell does not get involved. I think this functionality includes some emulation for the Windows behaviour (especially regarding the interpretation of quoting characters), but it may not be identical.

It's best to use the variants of sysutils.executeprocess and the tprocess class whereby you explicitly specify each argument separately. I.e., the version of executeprocess with an "array of ansistring" to specify the arguments (http://www.freepascal.org/docs-html/rtl/sysutils/executeprocess.html ), and using the "executable" and "parameters" property of tprocess (http://www.freepascal.org/docs-html/fcl/process/tprocess.executable.html and http://www.freepascal.org/docs-html/fcl/process/tprocess.parameters.html)

This will also work on Windows. Note that this probably only will help if you are using quotes in your command line. You may want to show the command line here if that's not the case, as well as the way in which your tprocessthread class actually executes programs.

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Launching thread on OSX fails, parameters not passed in?
« Reply #2 on: August 25, 2015, 08:57:55 pm »
Sorry, haven't had much time to mess with this today, here's the parameter I'm passing in
-DSTOP.PORT=9089 -DSTOP.KEY=stop_jetty -jar /Users/demo/Desktop/Lazarus/JettyManager/jetty-runner.jar --port 8083 --path / "/Users/demo/Desktop/Lazarus/cfmlApp"

And the application is /usr/bin/java

I'm passing in the executable and parameters separately.


LProcess.Options := LProcess.Options + [poUsePipes, poStderrToOutPut];
LProcess.ShowWindow := swoHIDE;
LProcess.Executable := FExecutable;
LProcess.Parameters.Text := FParameters;
LProcess.Execute;

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Launching thread on OSX fails, parameters not passed in?
« Reply #3 on: August 25, 2015, 09:18:13 pm »
You're not adding the parameters separately though, but relying on the stringlist to parse them. However, the tstringlist class is the same on all platforms, so that by itself shouldn't make a difference. After setting the text property, maybe try writing out the individual elements of the parameters stringlist.

TProcess definitely works on OS X, it's used by Lazarus itself all the time.

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Launching thread on OSX fails, parameters not passed in?
« Reply #4 on: August 25, 2015, 10:27:17 pm »
If I only do '-version' as the parameter string, it works, I get the version.

Something is just.. weird.

If I output the exact parameter string, add the java executable to the beginning and stick it in a terminal, it works as it should...  But not when trying to run through tprocess

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Launching thread on OSX fails, parameters not passed in?
« Reply #5 on: August 25, 2015, 10:29:21 pm »
As mentioned before, try writing out the individual parameters as they have been added to the parameters property. E.g., if the double quotes of the last parameter have been added, it won't work because then the program will see the double quotes as part of the file name. On Windows, the operating itself will remove those quotes before the program sees it.

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Launching thread on OSX fails, parameters not passed in?
« Reply #6 on: August 25, 2015, 10:53:04 pm »
Yep, progress.
When I to LProcess.Parameters.Add(''); for each variable, it works, but not when passed in as a single string.

Temporary solution: Looping each word in the passed in string, and doing the Parameters.Add() for each, and it works.

I still want to find out why the other way wasn't working, but at least now I can continue   :)

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Launching thread on OSX fails, parameters not passed in?
« Reply #7 on: August 26, 2015, 02:58:07 pm »
Yep, progress.
When I to LProcess.Parameters.Add(''); for each variable, it works, but not when passed in as a single string.

Temporary solution: Looping each word in the passed in string, and doing the Parameters.Add() for each, and it works.

I still want to find out why the other way wasn't working, but at least now I can continue   :)
As mentioned twice before, try writing out the individual parameters as they have been added to the parameters property. I.e., assign your string to parameters.text and then print out all the elements of the parameters stringlist.

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: [Solved] Launching thread on OSX fails, parameters not passed in?
« Reply #8 on: August 26, 2015, 03:54:59 pm »
What I'm doing is taking string being passed in, and instead of assigning it to .Text, I'm looping over it and doing .Add() for each, and this seems to work.

Why the original way worked on Windows but not Mac is a mystery to me.

I'm not sure why you want me to print out the elements of the stringlist, unless you meant for me to do what I described above.

All the parameters are there, are correct, and work, but only if added with .Add() for each parameter item.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: [Solved] Launching thread on OSX fails, parameters not passed in?
« Reply #9 on: August 26, 2015, 04:08:27 pm »
What I'm doing is taking string being passed in, and instead of assigning it to .Text, I'm looping over it and doing .Add() for each, and this seems to work.

Why the original way worked on Windows but not Mac is a mystery to me.

I'm not sure why you want me to print out the elements of the stringlist, unless you meant for me to do what I described above.
If you assign a single string to parameters.text, the stringlist parsing code will convert it to individual parameters (string list items) internally. If it works by manually parsing the string and adding elements to parameters, but not by assigning everything directly to the parameters.text, the most likely reason is that your own parsing code works differently from the tstringlist one.

The simplest way I can think of to verify this, is by assigning your command line to parameters.text and then printing out the individual stringlist items of the parameters stringlist afterwards, and comparing this to the result of your manual parsing.

 

TinyPortal © 2005-2018