0

so I am a new programmer, I have been programming for about, probably a little over one month now. I am developing a fun program that asks for a username and password, once correct, the program allows the user to enter a command that can do something. I have found that using the Process class, and using the code Runtime.getRuntime().exec(""); opens a application, but is static in the sense that it the directory may not be the same for every person who uses it. How can I make this dynamic? How can I make it so the user can input their directory or even the program can detect the directory by itself?

Some code from my main class:

while (caseValidation.caseCheckFlag == true) {
        caseValidation.SetCase();
            if (caseValidation.GetCase().equalsIgnoreCase("internet")) 
            {
                System.out.println("Command executed!");
                intCMD.fetchInternet();
            }
            else if (caseValidation.GetCase().equalsIgnoreCase("spotify")) 
            {
                System.out.println("Command executed!");
                musCMD.fetchSpotify();
            }
            else if (caseValidation.GetCase().equalsIgnoreCase("changeUsername")) 
            {
                //code here
            }
            else if (caseValidation.GetCase().equalsIgnoreCase("changePass")) 
            {
                //code here
            }
            else if (caseValidation.GetCase().equalsIgnoreCase("pictures")) 
            {
                //code here
            }
            else if (caseValidation.GetCase().equalsIgnoreCase("videos")) 
            {
                //code here
            }
            else if (caseValidation.GetCase().equalsIgnoreCase("music")) 
            {
                //code here
            }
            else if (caseValidation.GetCase().equalsIgnoreCase("documents")) 
            {
                //code here
            }
            // more cmds here in the future

InternetCMD Class:

public class InternetCMD {

public void fetchInternet() throws IOException {
    Process internet = Runtime.getRuntime().exec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
}

}

Thanks for any advice in advance! :D

Joey S
  • 11
  • 1

1 Answers1

0

You can use ProcessBuilder instead of Runtime.getRuntime().exec. Process builder has a directory method, which sets the working directory for the process.

From Java Doc:

Sets this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory. The argument may be null -- this means to use the working directory of the current Java process, usually the directory named by the system property user.dir, as the working directory of the child process.

Somnath Musib
  • 3,548
  • 3
  • 34
  • 47