Open Other applications using java

Sometime in our application we need to open any other 3rd party applications like notepad, music applications, etc. We can do the same using java by using Process class which is already available in java.lang package. Here is the simple program for the opening a notepad application.

public class OpenApplication 
{
public static void main(String[] args) throws Exception 
{
try
{
Process p = Runtime.getRuntime().exec("notepad.exe");
p.waitFor(); //waits for the application to close
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

In above example whatever we write in the exec() method it will execute if it is executable. This method will only except the executable file having extension of .exe if the file is not having the .exe extension it will throw the java.io.IOException.

Now write & save the program, compile it and run it.

No comments:

Post a Comment