Forum Stats

  • 3,816,091 Users
  • 2,259,138 Discussions
  • 7,893,390 Comments

Discussions

Global ProcessBuilder?

user2018218
user2018218 Member Posts: 5 Red Ribbon
edited Apr 16, 2018 5:37PM in Java Programming

Hey guys,

Hopefully this is not a dumb question, but here it goes.

I have a program that I will pass from OS to OS. Most of the commands usually are the same, but there are some factors that need to change depending on which OS you are on.

opening up a windows cmd prompt, as opposed to opening up a shell command prompt.

So for example the code below.

[code]

if(os1.equals("Linux")){

cmdln = cmdln;

ProcessBuilder builder = new ProcessBuilder(cmdln, c_nix, " cd " + cdtodir + " && " + entercmd + " && " + secdump1);

try {

@SuppressWarnings("unused")

Process pr = builder.start();

} catch (IOException e) {

e.printStackTrace();

}

} else if (os1.equals("Windows")) {

cmdln = cmdln_win;

ProcessBuilder builder = new ProcessBuilder(cmdln, c_win, "cd " + cdtodir + " && " + entercmd + " && " + secdump1);

try {

@SuppressWarnings("unused")

Process pr = builder.start();

} catch (IOException e) {

e.printStackTrace();

}

}

[/code]

So I created some variable to throw into the process builder.

I am not sure if it would be easier to do it this way - just do an if else, etc...

Or is there a way to pass around a global ProcessBuilder where you could do something like.

[code]

if(os1.equals("Linux")){

cmdln = cmdln_nix;

secdump1 = <unix command>

} else if (os1.equals("Windows")) {

cmdln = cmdln_win;

secdump1 = <windows command>

}

ProcessBuilder builder = new ProcessBuilder(cmdln, c_win, "cd " + cdtodir + " && " + entercmd + " && " + secdump1);

try {

@SuppressWarnings("unused")

Process pr = builder.start();

} catch (IOException e) {

e.printStackTrace();

}

[/code]

just looking for opinions on what would be the best way to go about doing this.

morgalr

Answers

  • Unknown
    edited Apr 16, 2018 5:37PM
    but there are some factors that need to change depending on which OS you are on.

    And for those you need to write code SPECIALIZED for the os.

    if(os1.equals("Linux")){cmdln = cmdln;

    It makes NO SENSE to assign something to itself.

    Or is there a way to pass around a global ProcessBuilder where you could do something like.

    No - if you read the API you will see that is a 'final' class - so you can't extend it.

    You need to have code that is SPECIALIZED for the os:

    1. commands can be different

    2. parameters to those commands can/will be different

    3. case-sensitivity can/will be different

    4. results may be different.

    The only 'if' statement you need is to decide which version of YOUR class gets loaded based on the platform you are on.

    Don't try to create a 'one size fits all' set of code that deals with OS issues. It ain't gonna work.

    morgalr
This discussion has been closed.