Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Running other program in Windows with Java

  Asked By: Glenn    Date: Nov 01    Category: Java    Views: 841
  

Does anyone know how to call other program in Windows in Java program. I am
working on a program in Java and I want the program to call other executable
file in Windows, e.g notepad.exe. In delphi, that could be easily done by
function WinExec.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Scarlett Hughes     Answered 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");

 
Answer #2    Answered By: Marina Smith     Answered On: Nov 01

I will try the code you gave me. Sorry that I am beginner in java  myself, in the
past I deal mostly with delphi. Do you ever deal with API, in windows
programming I would need to deal a lot with API, such as windows  Shell API,
Active Directory API, I can do that easily with Delphi. And what site do you
recommend for java programming?

 
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: