I am executing a shell script from Java program. I have implemented it using Runtime class. Below is the code I implemented
final StringBuilder sb = new Stri
Use this:
final StringBuilder sb = new StringBuilder("test.sh");
sb.append(" \"/path to/my/text file\"");
To recreate the command you run in shell manually, test.sh "/path to/my/text file"
, you will need to include the quotes.
final StringBuilder sb = new StringBuilder("test.sh");
sb.append(" \"/path to/my/text file\""); //notice escaped quotes
final Process p = Runtime.getRuntime().exec(sb.toString());
Your approach is correct you just need to add a space (" ")
before parameters and escape the "/" and " "
characters in the parameters
Use ProcessBuilder , it's what it's designed for, to make your life easier
ProcessBuilder pb = new ProcessBuilder("test.sh", "/path", "/my/text file");
Process p = pb.start();