Java application won't start openvpn configuration file via exec function
Java application won't start openvpn configuration file via exec function
I am trying to write a small Java application that starts an openvpn configuration file. Therefore I first changed the sudoers
file on my mac to:
sudoers
USERNAME ALL=(ALL:ALL) NOPASSWD: /usr/local/Cellar/openvpn/2.4.6/sbin/openvpn
I got the USERNAME
via the command whoami
. This part is working fine, because when I now type
USERNAME
whoami
sudo openvpn /directory/of/openvpn/file.ovpn
into the terminal on my mac. I don't have to insert any password and the openvpn configuration file starts running. After that I now want to use my Java application to do the same job. Therefore I have the following setup:
public class Connector
public static String executeCommand(String... command)
StringBuffer output = new StringBuffer();
Process p;
try
p = Runtime.getRuntime().exec(command);
System.out.println(p.waitFor());
InputStreamReader in = new InputStreamReader(p.getInputStream());
BufferedReader reader = new BufferedReader(in);
String line = "";
while ((line = reader.readLine()) != null)
output.append(line + "n");
p.destroy();
reader.close();
catch (Exception e)
e.printStackTrace();
return output.toString();
public static void main(String args)
String command = new String[3];
command[0] = "/bin/sh";
command[1] = "-c";
command[2] = "/usr/bin/sudo -S /usr/local/Cellar/openvpn/2.4.6/sbin/openvpn /directory/of/openvpn/file.ovpn";
String result = Connector.executeCommand(command);
System.out.println(result);
For command[2]
I also tried without the flag -S as well as providing the password via echo password | sudo command
. For command[0]
I also used /bin/bash
instead of /bin/sh
. But everything leads to the same result. The application runs infinitely. No output within the console. And the openvpn configuration file was not opened, because when I now type
command[2]
echo password | sudo command
command[0]
/bin/bash
/bin/sh
killall openvpn
into the terminal on my mac I get the information that there is no open connection.
No matching processes belonging to you were found
If I don't use the sudo command
command[2] = "/usr/local/Cellar/openvpn/2.4.6/sbin/openvpn /directory/of/openvpn/file.ovpn";
I get an output that says that I have not the permissions to run the configuration file. This is the same output that I get when I type the command above into the terminal on my mac manually.
Opening utun (connect(AF_SYS_CONTROL)): Operation not permitted (errno=1)
UPDATE
I changed the function executeCommand
to
executeCommand
public static String executeCommand(String... command)
try
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
catch (Exception e)
e.printStackTrace();
Now I get an error message in my console that says
<ERROR>
ifconfig: ioctl (SIOCDIFADDR): Can't assign requested address
route: writing to routing socket: File exists
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.