0

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

  1. My command works when I run it manually on cmd.
  2. When I remove the command_3 and command_4 on the code, it executes the git checkout command.
firemancer
  • 25
  • 7

1 Answers1

0

Maybe i can suggest you an other solution, You could use jgit and java directly to do these tasks from java directly

For example (not tested)

FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.setMustExist( true );
repositoryBuilder.setGitDir( ... );
Repository repository = repositoryBuilder.build();


Ref ref = repository.checkout().
    setCreateBranch(true).
    setName("branchName").
    setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
    setStartPoint("origin/" + branchName).
    call();
Tokazio
  • 516
  • 2
  • 18