0

I am trying to programmatically call from my java program a shell script that in turn executes a command depending of the parameter sent.

the command to be executed inside the connectvpn.sh shell script is:

echo myrootpassword | sudo -S /usr/local/Cellar/openvpn/2.3.8/sbin/openvpn --config /usr/local/etc/openvpn/1.opvn

or

echo myrootpassword | sudo -S /usr/local/Cellar/openvpn/2.3.8/sbin/openvpn --config /usr/local/etc/openvpn/2.opvn

and so on from a long list, where the filename number I want to be variable depending on the value of the parameter received

So I want my java program to be able to use always the same shell script but use different .ovpn file depending on the parameter sent.

I believe that on my java program I have to call it something like this:

server_number = 1;    

ProcessBuilder pb = new ProcessBuilder("./connectvpn.sh", server_number);
Process proc = pb.start();

What would go in the shell script so that the filename called is variable and for the example shown it uses 1 but other times it uses whatever number is sent as parameter?

Thank you very much!

dodecafonico
  • 195
  • 1
  • 10
  • 2
    http://stackoverflow.com/questions/3468987/executing-another-application-from-java – Ravindra babu Nov 17 '15 at 17:04
  • ok, thanks, but that does not answer my question about the parameter – dodecafonico Nov 17 '15 at 17:10
  • You can pass parameters in array list. List args = new ArrayList(); args.add ("script.bat"); // command name args.add ("-option"); // optional args added as separate list items ProcessBuilder pb = new ProcessBuilder (args);. – Ravindra babu Nov 17 '15 at 17:14
  • If I got it correct - you are trying to pass a variable parameters to your Java program? You can do that by calling the "main" class of your Java program with the appropriate parameters. Please see https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html – TR1 Nov 17 '15 at 17:15

1 Answers1

0

In the shell script, use $1 to denote the first parameter passed to it.

Fix your java as below,

ProcessBuilder pb = new ProcessBuilder("./connectvpn.sh", String.valueOf(server_number));
TK8
  • 125
  • 6
  • thanks, so inside the shell script I would have: echo myrootpassword | sudo -S /usr/local/Cellar/openvpn/2.3.8/sbin/openvpn --config /usr/local/etc/openvpn/$1.opvn ? – dodecafonico Nov 17 '15 at 17:19
  • yes. Try it out and see. – TK8 Nov 17 '15 at 17:37
  • My java program is giving me this error that I must solve first so if you know what's the problem please tell me: "The error message was: class bsh.EvalError (line 18): new ProcessBuilder ( "./connectvpn.sh" , server_number ) --Sourced file: inline evaluation of: ``//import java.io.IOException; //import java.util.*; import java.lang.ProcessBuil . . . '' : Typed variable declaration : Constructor error: Can't find constructor: java.lang.ProcessBuilder( java.lang.String, int ) in class: java.lang.ProcessBuilder". First time I use ProcessBuilder so might have forgotten to include something – dodecafonico Nov 17 '15 at 17:39
  • As the error says, the constructor cannot be found. You are calling `ProcessBuilder(String, int)`. The acceptable constructors are `ProcessBuilder(List)` or `ProcessBuilder(String...)`. Check my edited answer above. – TK8 Nov 17 '15 at 17:52