user14197708
user14197708

Reputation:

Getting no output

I want to automate the process of uploading the files & folders using Git, the program is supposed to execute the terminal commands that one would type in to upload to a repo on GitHub.

I am not getting any errors, but the Problem that I am facing is that it does not do anything, and there is no output shown. What might be the reason for such thing ?

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();
        }
    }
}

Upvotes: 0

Views: 95

Answers (1)

rzwitserloot
rzwitserloot

Reputation: 102814

{0}? That isn't a thing.

In general, trying to exec your way out of a problem is extremely complicated: Getting the errors out is tricky, shaping the command lines are very tricky, and your code is now platform dependent. You've also complicated the installation process.

There are libraries to do git stuff from java, natively, such as jgit. Use those instead.

If you insist on using exec:

  1. Use ProcessBuilder and pass arguments as a List<String>, don't rely on exec's space-based splitting.
  2. It is not a shell. Spawning a new process (which is what exec does), then cd-ing to another directory in that shell does nothing - each process has its own take on 'current working directory' - changing the spawned shell's doesn't change your java process's, and therefore, doesn't change the cwd of any other git call you run either. Even if you fix the {0} issue (which you'd do by making a j.u.List: List.of("cmd.exe", "cd", myFileObj.getPath()) and passing that to ProcessBuilder).
  3. Proceed very carefully. For example, you make lots of new myProcess1 through myProcess8 variables and then check the exit value of the same original process object every time. If you meant to do that, just make one big if block and more helper methods. If you didn't, proofread a lot more, any errors in exec code aren't going to be particularly obvious. You won't get nice exceptions pointing right at the problem.

Upvotes: 1

Related Questions