Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used a VPN service and used java code for connecting to it and running it on Ubuntu.Whenever the sudo openvpn command runs completely the console gets stuck and doesn't go to the code after the while loop.But if I include the code after the sudo command inside the while loop it runs the code after that but not the sudo command then. I tries adding "&" so that the command runs in the background but no use.Please suggest a method to do this as i have tried various solutions but all in vain.Below is my code that i have written.

What I have tried:

Java
public class curl {

  void sudo() throws IOException {
    String command1 = "sudo openvpn --config /etc/openvpn/configFile ";
    System.out.println(command1);

    Process curlProc1;

    curlProc1 = Runtime.getRuntime().exec(command1);
    DataInputStream curlIn1 = new DataInputStream(curlProc1.getInputStream());

    String outputString1;

    while ((outputString1 = curlIn1.readLine()) != null) {
        System.out.println(outputString1);
    }

    String urly = "MyURL";
    URL obj = new URL(urly);
    HttpURLConnection con1 = (HttpURLConnection) obj.openConnection();

    con1.setRequestMethod("GET");
    con1.setDoOutput(true);

    int responseCode = con1.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    BufferedReader iny = new BufferedReader(
    new InputStreamReader(con1.getInputStream()));
    String output;
    StringBuffer response = new StringBuffer();

    while ((output = iny.readLine()) != null) {
      response.append(output);
    }

    iny.close();
    System.out.println(response.toString());
  }

  public static void main(String args[]) throws IOException, ClassNotFoundException, SQLException, JSONException{
    curl brc= new curl();
    brc.sudo();
  }
}
Posted
Updated 12-Jul-18 22:56pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900