0

I have a shell script a.sh file with this command and I want to run it from my Java program but can't figure it how.

I usually run it as either ./a.sh or by typing the command in the terminal

yarn jar target/ww-SNAPSHOT.jar path/to/file.properties

How will i be able to run it from Java along with being able to change .properties file location?

** EDIT**

I can actually simply set the .properties file location but still can't seem to run the command if it is not in .sh script?

bjoshi
  • 105
  • 1
  • 9
  • ...same way as you run any other executable or command from Java? See [java.lang.ProcessBuilder](https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html). – Charles Duffy Jun 27 '18 at 22:32
  • `Runtime.exec()`? Take a look at this http://www.baeldung.com/run-shell-command-in-java – Sergei Sirik Jun 27 '18 at 22:32
  • Assuming you gave your script a shebang and set the executable bit, running it (programmatically) isn't different from any other executable on UNIX just because it's a script under-the-hood. Any answer that tells you how to run any other UNIX program will apply to your script just as much -- and if that's *not* true, the right answer is to fix your script. – Charles Duffy Jun 27 '18 at 22:33
  • Process P = new ProcessBuilder("a.sh","setting/r.properties").run(); Do you think something like this would work? – bjoshi Jun 27 '18 at 22:36
  • @bjoshi No, because [`ProcessBuilder`](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) doesn't have `run` method. Also, beware, the working directory isn't always the same as the location of the executable, so resources it's expecting (like the `setting/r.properties`) either need to be relative to the current working directory or you need to specify the working directory when you setup the `ProcessBuilder` – MadProgrammer Jun 27 '18 at 22:41
  • how about .start() instead or .run()? – bjoshi Jun 27 '18 at 22:44

0 Answers0