I am working on a solution that retrieves data from an external system. This system is on the remote server and for connection, it uses a locally installed CLI.
the CLI exe itself doesn't seem to accept all the parameters during the initial call, so I cannot perform the initial call with all parameters I need to put there.
When CLI is called on the machine manually, it jumps into its own environment and accepts its own inputs.
So when called manually, it looks something like this:
PS C:\Program Files (x86)\SW> Cli.exe
CLI> config -s thisIsMyServer
Server: thisIsMyServer
CLI> config -f thisIsMyFile
Server: thisIsMyServer
File: thisIsMyFile
CLI>
I need to perform this from Java application, that calls the CLI.
The original code I came to was:
Configuration configuration = Configuration.getInstance();
Process process = Runtime.getRuntime().exec(configuration.getCLICommand() + " config -s " + configuration.getPlatformURL());
process.waitFor();
process = Runtime.getRuntime().exec(configuration.getCLICommand() + " config -f " + configuration.getPlatfomFile());
process.waitFor();
process = Runtime.getRuntime().exec(configuration.getCLICommand() + " login " + configuration.getUsername() + " " + configuration.getPass());
process.waitFor();
Many things are hidden behind the methods, but I know for sure that:
- the data in the config file ARE correct and if the data is used manually through CMD, in the end, the CLI throws back successful login and all I need.
- The methods are returning the data correctly (tried all the logs) so it is down to how the Java is calling it.
- This example throws out incorrect login, my suspicion is, the parameters are not propagated correctly.
Also - and this I don't know, it is weird, that after each wait, when we want to write to open process, the configuration.getCLICommand() is called every time. This method returns the path to the CLI executable. That suggests (for me) that we are trying to execute new instance of the executable with completely invalid parameters.
So I tried to alter the code to:
Configuration configuration = Configuration.getInstance();
Process process = Runtime.getRuntime().exec(configuration.getCLICommand());
process.waitFor();
process = Runtime.getRuntime().exec("config -s " + configuration.getPlatformURL());
process.waitFor();
process = Runtime.getRuntime().exec("config -f " + configuration.getPlatfomFile());
process.waitFor();
process = Runtime.getRuntime().exec("login " + configuration.getUsername() + " " + configuration.getPass());
process.waitFor();
Where the naive idea would be to pass the command, wait for the response, and then send the follow-up command to the process that is somewhere waiting for us running.
This is even worse, from invalid login, I get directly into a timeout, where the program just "does nothing" and seems to be hanging somewhere in between the commands. (logs are also problematic, this runs inside a larger java server, but at least there were no java runtime errors).
Any idea then, how to successfuly pipe additional commands to the running executable?
Thank you!