Question

So I'm creating a Java program and I want to make it so that you can ask it to open a program. But, here's the catch, I want the program it opens to be taken from the user input, right now I'm trying to change this

try{Process p = Runtime.getRuntime().exec("notepad.exe");}
catch(Exception e1){}

Into something that opens a program that you asked it to open.

Here's an example of what I want:

User: Can you open chrome? Program: Of course, here you go! chrome opens

Could anyone tell me how I would be able to do this?

Était-ce utile?

La solution

You can do it in two ways:

1.By Using Runtime: Runtime.getRuntime().exec(...)

So, for example, on Windows,

Runtime.getRuntime().exec("C:\application.exe -arg1 -arg2");

2.By Using ProcessBuilder:

ProcessBuilder b = new ProcessBuilder("C:\application.exe", "-arg1", "-arg2");

or alternatively

List<String> params = java.util.Arrays.asList("C:\application.exe", "-arg1", "-arg2");
ProcessBuilder b = new ProcessBuilder(params);

or

ProcessBuilder b = new ProcessBuilder("C:\application.exe -arg1 -arg2");

The difference between the two is :

Runtime.getRuntime().exec(...) takes a single string and passes it directly to a shell or cmd.exe process. The ProcessBuilder constructors, on the other hand, take a varargs array of strings or a List of strings, where each string in the array or list is assumed to be an individual argument.

So,Runtime.getRuntime.exec() will pass the line C:\application.exe -arg1 -arg2 to cmd.exe, which runs a application.exe program with the two given arguments. However, ProcessBuilder method will fail, unless there happens to be a program whose name is application.exe -arg1 -arg2 in C:.

Autres conseils

You can try it with like. Pass whole path of where you install chrome.

try{
        Process p = Runtime.getRuntime().exec("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
        }
    catch(Exception e1){

    }

When using exec, it is essentially the same as if you were using the command line on windows. Open Command Prompt, type open, and see if it gives details as to how it opens files. If not, find the opener. Usually when dealing with command line operations, there are multiple parameters that are required for opening files/applications. An example of this would be for opening the "TextEdit.app" application on a mac.

Process p = Runtime.getRuntime().exec("open -a TextEdit.app");

Terminal(for mac) would open the app using the -a flag, meaning "application." You could open a file doing:

Process p = Runtime.getRuntime().exec("open filename.file_ext -a TextEdit.app");

The second one will tell the computer to find the application named <app_name>.app and open the file filename.file_ext

I know this is not going to work for a windows machine, but it's only to show how to use the command line operations for opening files and applications. It should be similar for windows though.

Hope this helps

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top