0

i got two questions. Where is the directory the Method Runtime.getRuntime().exec() gets its resources from?

if i am calling Runtime.getRuntime().exec("notepad.exe"), why does it start the windows editor? Where does java gets the .exe source from?

based on this question, i have to let the user choose, if he wants to open a file in an editor, which editors he prefers, and wants to use. He only writes in something like notepad.exe or ultraedit.exe and the choosen file will be opened in the editor written down here. At the moment, i am opening a file with this Method

public void open(String path) {
    try {
        if(new File(path).exists())
            Runtime.getRuntime().exec("notepad.exe " + path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

So as you can see every file will be opened within the notepad. But i need to have something like this :

public void open(String program, String path) {
    try {
        if(new File(path).exists())
            Runtime.getRuntime().exec(program + " " + path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

So is there any possibility to open txt files in different editors, by just calling their .exe file?

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • `if(new File(path).exists()) Runtime.getRuntime().exec("notepad.exe " + path);` I believe that will fail if there is a space in the `path`. But overall, it is extremely fragile code. Be sure to visit the Java World article linked from the [`exec` info. page](http://stackoverflow.com/tags/runtime.exec/info) and implement the recommendations in order to save future pain. – Andrew Thompson Dec 12 '12 at 07:47

2 Answers2

1

Runtime.exec() gets its information from PATH. Any program found in there can be executed like you showed.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
1
Where does java gets the .exe source from?

Its not about java. check the PATH environment variables in you Operating systems. It has the path for all the exe files. Try this

1) open cmd

2) type c:\> echo %PATH%

the second will tell you the values of PATH variable

So is there any possibility to open txt files in different editors, by just calling their .exe file?

yes edit the PATH variable to include the path of you other editor's exe file ( use semicolon and then append the path to environment don't replace the existing string ), and the java program remains the same

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40