0

I have a method that launches cmd through Process

public void startCmd(){
  try {
       Process p = Runtime.getRuntime().exec("cmd");
       BufferedReader stdInput = new BufferedReader(newInputStreamReader(p.getInputStream()));
        while ((s = stdInput.readLine()) != null)
           System.out.println(s);
     } catch (IOException e) {
       e.printStackTrace();
   }
}

My task is to create a way to pass a command and based on response, pass another command. For instance:

foo
 result: bar, baz

if the result is baz, execute A, and if A returns C execute D in the same process, I need to do it all in java, the result of the whole operation will be processed further down the pipeline.

Nodir Nasirov
  • 1,488
  • 3
  • 26
  • 44

2 Answers2

0

You probably want to have a Map which contains your Result and some representation of the Actions you want to do, if you receive such an result.

Then you just do the following:

  1. You run your command
  2. You receive the result of your command
  3. You lookup what to do depending on the result

and so on ....

So for your example:

Map<UnqiueResult, Command> registry = new HashMap<>();
registry.add(UniqueResult.BAZ, new Command(A));
registry.add(UniqueResult.C, new Command(D));

If you also need to distinguish who the executor was for that result add a method to define the key for the registry map.


Edit:

If you want to do in the same process you have to use a Writer on the output stream. See the link I attached.

(So you might not need to do that if you view each step as independant process. Your code needs to be aware of the context in this case, but this could be part of your Command definition as previously mentioned.)

If your first process has a result of Y / N, the you can just start your next process depending on the result.

If you have further requirements why it needs to be one process please add them to your question)

second
  • 4,069
  • 2
  • 9
  • 24
  • lets say I know what to do next based on result, how do i execute the next step on the same process? – Nodir Nasirov Jun 25 '19 at 17:00
  • That what the Command object is for. Its an representation of the steps you need to do next. It could contain an independet registry as well, if you don`t want a global registry. – second Jun 25 '19 at 17:02
  • https://stackoverflow.com/questions/31081973/sending-message-to-a-running-process – second Jun 25 '19 at 17:08
0

This is probably available as a tool somewhere.

Your approach is somewhat pointing in the right direction, but you are far, far away from reaching your goal.

Before you start thinking about the structure, I suggest you have a look on how to properly execute one external application.

In modern java it is suggested to use ProcessBuilder for that. See some sample code here: https://www.mkyong.com/java/java-processbuilder-examples/

You will find for example a complete example to call ping from java and read the output.

When you have mastered that, you need to wire the results with your configuration. The configuration should be read into a tree structure, where you get to the next node over the return conditions you mentioned. This part can actually pretty easy if done well.

thst
  • 4,592
  • 1
  • 26
  • 40