Quiero ejecutar el command "git checkout" usando Java. Estoy usando bash script para hacerlo y no puedo pasar arguments al script? ¿Alguien puede sugerir una solución?
Código que tengo:
public static void main(String[] args) throws IOException, Exception { String path="path/to/repo"; String branch="newBranch"; ProcessBuilder pb = new ProcessBuilder("C:/revert.bat",path,branch); Process p = pb.start(); p.waitFor(); System.out.println("Script executed successfully"); }
La secuencia de Bash es:
cd $1 git checkout $2
¿Cuál es la forma correcta de hacer esto?
Tu código de Java parece estar bien. Hice una testing en mi sitio, pasé "hello" y "world" a test.bat, y los imprimí en un file test.txt, funciona.
public static void main(String[] args) { ProcessBuilder pb = new ProcessBuilder("C:/test.bat","hello","world"); Process p; try { p = pb.start(); p.waitFor(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Script executed successfully"); }
El código de test.bat está a continuación:
@echo off echo %0 > C:\test.txt echo %1 >> C:\test.txt echo %2 >> C:\test.txt @echo on
Y el test.txt será como bajo:
test.bat hello world
Así que sugiero que pueda imprimir los arguments en su file bat y verificar qué file recibió el bat, y también verificar si está bien ejecutar el file bat con esos arguments en CMD directamente. FYI.