Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 395 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
Global ProcessBuilder?

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 {
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 {
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 {
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.
Answers
-
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.