我想自动化使用 Git 上传文件和文件夹的过程,该程序应该执行终端命令,人们会输入这些命令将其上传到 GitHub 上的存储库。
我没有收到任何错误,但我面临的问题是它什么也不做,也没有显示输出。造成这种情况的原因可能是什么?
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.System;
public class TerminalUploader{
public static void main(String[] args){
try {
System.out.println("Please input a path:\n");
Scanner myScanner = new Scanner(System.in);
//get user input
String path = myScanner.next().toLowerCase();
//Getting Repository Link from the user
System.out.println("Please Enter Repository Link that you would like to upload/push to:\n");
String RepoLink = myScanner.next().toLowerCase().toString();
//creating a File object and passing the path into it
File myFilePath = new File(path);
//assigning the File object to a File[] object to be able to listFiles() and perform other File[] operations
File[] myFiles = myFilePath.listFiles();
System.out.println(Arrays.toString(myFiles));
for(File myFileObj : myFiles){
Process myProcess = Runtime.getRuntime().exec(String.format("cmd.exe cd \"{0}\"", myFileObj.getPath()));
myProcess.waitFor();
System.out.println(String.format("Navigating to Directory",myFileObj.getPath()));
//implement something to keep track of what files were cded into
//run the command only if the previous command is successful with exitValue() of 0
if(myProcess.exitValue() == 0) {
Process myProcess2 = Runtime.getRuntime().exec("git init");
myProcess2.waitFor();
System.out.println("Executed Command: `git init` Successfully");
}
if(myProcess.exitValue() == 0) {
Process myProcess3 = Runtime.getRuntime().exec("git add .");
myProcess3.waitFor();
System.out.println("Executed Command: `git add .` Successfully");
}
if(myProcess.exitValue() == 0) {
Process myProcess4 = Runtime.getRuntime().exec("git commit -m \"Finished Tasks\"");
myProcess4.waitFor();
System.out.println("Executed Command: `git commit -m` Successfully");
}
if(myProcess.exitValue() == 0) {
Process myProcess5 = Runtime.getRuntime().exec("git branch -m main");
myProcess5.waitFor();
System.out.println("Executed Command `git branch -m main` Successfully");
}
if(myProcess.exitValue() == 0) {
Process myProcess6 = Runtime.getRuntime().exec(String.format("git remote add main %s", RepoLink));
myProcess6.waitFor();
System.out.println(String.format("Executed Command: `git remote add main %s` Successfully", RepoLink));
}
if(myProcess.exitValue() == 0) {
Process myProcess7 = Runtime.getRuntime().exec("git push --set-upstream main main");
myProcess7.waitFor();
System.out.println("Executed Command: `git push --set-upstream main main` Successfully");
}
if(myProcess.exitValue() == 0){
Process myProcess8 = Runtime.getRuntime().exec("cd ..");
myProcess8.waitFor();
System.out.println("Executed Command: `cd ..` Successfully");
}
}
}catch(Exception Ex){
Ex.printStackTrace();
}
}
}
答案 0 :(得分:1)
{0}
?那不是一回事。
一般来说,试图以自己的方式解决问题是极其复杂的:找出错误很棘手,塑造命令行也很棘手,而且您的代码现在依赖于平台。您还使安装过程变得复杂。
有一些库可以从 Java 本地执行 git 东西,例如 jgit。改用那些。
如果你坚持使用exec:
List<String>
传递,不要依赖 exec 的基于空间的拆分。{0}
问题(您可以通过创建 j.u.List: List.of("cmd.exe", "cd", myFileObj.getPath())
并将其传递给 ProcessBuilder 来实现)。myProcess1
变量创建了大量新的 myProcess8
,然后每次都检查同一个原始进程对象的退出值。如果您打算这样做,只需制作一个大的 if 块和更多的辅助方法。如果没有,请更多地校对,执行代码中的任何错误都不会特别明显。你不会得到很好的例外来指出问题。