When I try to execute a set of commands through Runtime.exec()
in Java, some of them don't work. Nevertheless, when I manually execute the same commands in the terminal, they all work fine. I made sure to copy the exact same commands and they only work when executed manually. What can be causing the problem?
Here's my code:
rt = Runtime.getRuntime();
rt.exec("sudo wpa_cli -i " + ifName + " remove_network 0"); //Always removes network 0, in case it already exists
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " add_network 0");
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " set_network 0 ssid '\"" + SSID + "\"'");//This command only works when executed manually
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " set_network 0 psk '\"" + Pass + "\"'");//This command only works when executed manually
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " select_network 0");
The commands where I input the SSID and Password are the ones causing trouble. ifName
is the name of the NIC I am using.
Note that I added several Thread.sleep()
because I wasn't sure if the commands had time to finish executing before I called the next one (as this is configuring a network and connecting to it, I really didn't know). Maybe they are completely useless, but I added them just in case.
EDIT: I know this is not related to quote duplication because I already tried removing the quotes and the code still doesn't work. I keep getting the same FAIL
message from the console.