Use Java to start a Windows exe

2019-09-22 06:11发布

问题:

I want to find out how to open any exe in Windows using Java code. I have searched Google before and they only show me part of the code that they use, I think, because it doesn't seem to compile.

I have downloaded JDK 7 to compile. I don't use Eclipse at the moment and also explaining what I had to do to get it to work in detail would help a lot.

to what Sri Harsha Chilakapati said: would i need to create a class for the code?

Thanks to those who answered but i didn't quite get what you meant but i did however manage to find a website which had what i was after: http://www.rgagnon.com/javadetails/java-0014.html

public class Test {
  public static void main(String[] args) throws Exception {
    Process p = Runtime.getRuntime().exec(
    "\"c:/program files/windows/notepad.exe\"");
p.waitFor();
  } 
}

the above was what i was after but thanks again anyway to the people who answered.

回答1:

Try this.

String myExe = "C:\\MyExe.exe";
String args  = "";

Runtime.getRuntime().exec(myExe + " " + args);

Hope this helps.



回答2:

I would recommend the ProcessBuilder, especially for additional arguments.