Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Glenn Duncan   on Nov 01 In Java Category.

  
Question Answered By: Scarlett Hughes   on Nov 01

To call  an executable file in windows  from a java program  you need to execute
the file using the java  Runtime.
i.e. you need to call the exec() function of the Runtime defined in the API.
So for example if you would want to open internet explorer from you java program
you could just do
public class MyRuntime {
public static void main (String[] args) {
try{
Process p = Runtime.getRuntime().exec("\"C:/Program Files/Internet
Explorer/IEXPLORE.EXE\"");
p.waitFor();
} catch(java.io.IOException e) {
System.out.println(e.toString());
e.printStackTrace();
} catch(java.lang.InterruptedException ie) {
System.out.println(ie.toString());
ie.printStackTrace();
}
}
}


Now if you want to call notepad  just replace the path in the exec wite
notepad.exe 's path. You need the escape sequence \" because the path contains a
white space.

Since notepad.exe is in the Path environment variable in windows you could just
do

Process p = Runtime.getRuntime().exec("notepad.exe");

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Running other program in Windows with Java Or get search suggestion and latest updates.


Tagged: