i am trying to run this java code that calls the shell script on runtime.
when i run the script in terminal i am passing argument to script
code:
./test.sh argument1
java code:
public class scriptrun
{
public static void main(String[] args)
{
try
{
Process proc = Runtime.getRuntime().exec("./test.sh");
System.out.println("Print Test Line.");
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
How to pass argument for script in java code?
Here is something very simple you can try
The preferred way to create processes in recent versions of Java is to use the
ProcessBuilder
class, which makes this very simple:But if you do want to/need to use
Runtime.exec
for whatever reason, there are overloaded versions of that method that allow the arguments to be specified explicitly: