Launch program using cmd from Java

3

I want to launch a program from the execution of my program in java. The command I use in the cmd would be changing user, password and url:

"start / B / D" c: \ Program Files (x86) \ Common Files \ Juniper Networks \ Integration "pulselauncher -u 'user' -p 'password' -url link -r pepe"

I get an error and I do not know how to try it, could you give me a cable? Thank you And the code I use is:

public static void abrirConexionPulseSecure() {
    try {

        String [] cmd = {
                "start", 
                "/B",
                "/D",
                "\"c:\Program Files (x86)\Common Files\Juniper Networks\Integration\"",
                "pulselauncher", 
                "-u", 
                USER, 
                "-p", 
                PASSWORD, 
                "-url", 
                "https://url.com", 
                "-r", 
                "pepe" };

        Runtime.getRuntime().exec(cmd);
        System.out.println("Conexion establecida.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And the error that I get is the following:

    java.io.IOException: Cannot run program "start": CreateProcess error=2, El sistema no puede encontrar el archivo especificado
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at utils.ConexionBBDD.abrirConexionPulseSecure(ConexionBBDD.java:78)
    at test.TestGestionFicheros.main(TestGestionFicheros.java:30)
Caused by: java.io.IOException: CreateProcess error=2, El sistema no puede encontrar el archivo especificado
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 5 more
    
asked by ZiPoTaTo 30.10.2017 в 15:28
source

1 answer

8

I do not use windows so I can not 100% ensure this solution works, but something like the following should work:

String [] cmd = {
            "cmd.exe", //añadiendo primero el ejecutable que lanza la consola         
            "/c",
            "start", 
            "/B",
            "/D",
            "\"c:\Program Files (x86)\Common Files\Juniper Networks\Integration\"",
            "pulselauncher", 
            "-u", 
            USER, 
            "-p", 
            PASSWORD, 
            "-url", 
            "https://url.com", 
            "-r", 
            "pepe" };

    Runtime.getRuntime().exec(cmd);

Explanation: The problem is that "start" is an internal order of the command interpreter, such as "dir" or "cd". That is, there is no executable "start.exe" or a "cd.exe" to call, you have to ask to cmd.exe to perform that action.

    
answered by 30.10.2017 / 16:01
source