I suggest you simply construct your command+parameters list beforehand using List.add
and .addAll
:
List<String> commandAndParams = new ArrayList();
commandAndParams.add("command");
commandAndParams.add("-p");
commandAndParams.addAll(pParametersList);
commandAndParams.addAll(Arrays.asList(new String[]{"-v", "-t", "-s"}));
commandAndParams.add("-o"); commandAndParams.add(outputfile);
// a varargs method would need a .toArray() cast before being called,
// but ProcessBuilder has a List<String> constructor.
ProcessBuilder pb = new ProcessBuilder(commandAndParams);
I've mixed different styles to let you chose which helps understanding the most, but you should avoid mixing them all like that.
I prefer recreating the list to prepending the needed element to the existing one simply because I suppose your existing list has a precise meaning which is represented through its variable's name and differs from "a command and its list of parameters I'm going to feed to ProcessBuilder". You should chose a variable name more meaningful that commandAndParams
if you can of course.