I want to execute 4 commands using ProcessBuilder however 2nd command is not working properly.
My code:
public static void main(String[] args) {
String path_prj = "C:\\Users\\asali\\Desktop\\CallRepoCode";
String origBranch = "frontend";
ArrayList<String> paths = new ArrayList<>();
paths.add("server\\src\\main\\java\\org\\classes\\CallManager.java");
paths.add("server\\src\\main\\java\\org\\classes\\CallUtils.java");
paths.add("server\\src\\main\\java\\org\\classes\\Main.java");
String command_1 = "cd " + path_prj;
String command_2 = " & git checkout " + origBranch;
String command_3 = " & mkdir updated_cia_files ";
String command_4 = " ";
for (String path: paths) {
command_4 = command_4 + "& copy " + path + " updated_cia_files ";
}
String[] command = {"cmd.exe", "/C", command_1, command_2, command_3, command_4 };
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.start();
}
Basically, I want to go to the C:\\Users\\asali\\Desktop\\CallRepoCode
and checkout to the frontend
branch. There is a GitHub repo, so it should work. After checkout, I want to create a folder and copy 3 files to that folder.
I successfully create the folder and copy the files; however local repo does not checkout to the frontend branch.
EDIT
- My command works when I run it manually on cmd.
- When I remove the command_3 and command_4 on the code, it executes the git checkout command.