1

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?

user3231227
  • 67
  • 1
  • 8
  • so you are wanting it to be just speech input, or enter the input itself? – user2277872 Feb 10 '14 at 16:22
  • I want to enter the input – user3231227 Feb 10 '14 at 16:30
  • You can try creating final String(s) of application names and then their locations on the computer. Then, check the user input for the and if that is within the text, open the app by the certain final String. Is this going to be only windows? or both mac/windows? – user2277872 Feb 10 '14 at 16:34
  • For now it's going to be just windows. But if I use the piece of code I have now it just runs notepad, do I really have to give the location of the installation? – user3231227 Feb 10 '14 at 16:37

3 Answers3

2

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:.

Pankaj
  • 592
  • 7
  • 19
  • Thank you so much! I managed to get it working! I created a thing called EXT and it cuts off the "can you open " part and puts .exe behind whatever comes after the can you open part and it opens that now! Thanks! – user3231227 Feb 10 '14 at 16:58
  • 1
    actually,I've worked on this in my previous company.So,that's why had an idea about it.We need to start an insurance calculator (written in C) we used to call it BI engine on server after a user form submission gets completed. – Pankaj Feb 10 '14 at 17:01
1

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){

    }
NFE
  • 1,147
  • 1
  • 9
  • 22
  • Yeah I know but chrome was just an example, I want to be able to ask it to open a certain program and I want that certain program to open – user3231227 Feb 10 '14 at 16:30
1

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

user2277872
  • 2,963
  • 1
  • 21
  • 22