Search code examples
javapathruntimeexecute

Within Java, how can I execute an external app without its full path


If I use the Windows run utility (Windows Key + R), and I type in "chrome.exe" it launches Chrome. Same thing for other applications that have their path in the PATH variable, of course.

However, in java if I try this:

Runtime.getRuntime().exec("chrome.exe");

It doesn't work. It only works if I use the full path. How can I make it execute chrome without needing the entire path? Thank you!


Solution

  • use cmd/c.you can use cmd option to specify that you are running a cmd command otherwise java try to run a chorm.exe from the folder where your .class exist so it will throw a error.

    Runtime.getRuntime().exec("cmd /c start chrome.exe");