Program to Execute Shell script / batch file using Java
In normal we Execute Java program using shell script . But here we are creating java program to execute shell script / batch file.
This can be done by using executing the system process using "Process proc = Runtime.getRuntime().exec("./execute_shell_using_java.sh");". This is a simple program that will just execute the sh/ shell script file execute_shell_using_java.sh.
Note: sh file will not run in windows and bat file will not run in linux
public class Sh
{
public static void main(String[] args)
{
try
{
Process proc = Runtime.getRuntime().exec("./execute_shell_using_java.sh");
System.out.println("Print Test Line.");
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
