Categories
java

How to run an Exe from Java program

There sometimes we get a need to run a exe file from the java program. Now to run an exe type this
[java]
try
{
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(“notepad.exe”);
} catch(Exception e)
{
System.out.println(e);
}
[/java]
Here wer are running notepad.exe from the program. If you want to run another file you have to enter the program name as your_program.exe . If the exe is not in the current directory you need to give the absolute path.
You can also get the error value of the program using p.exitValue();

if you need to run another java program or a jar file use
[java]
java -jar path_to_java_file/file.jar
[/java]