Hey everyone, im currently trying to access the cmd.exe from Java. I want to execute a command from Java and obtain it's results, at the moment im trying the following command:
It works if i input the command directly from the Command Prompt, but it doesn't work when i implement it in my code.wmic /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct Get displayName,productState /Format:List
I've tried other commands before, like this one:I used it to obtain my Gateway IP, and it worked, but for some reason the "wmic" command doesn't work.ipconfig/all
Anyways, this is the code:
try { String cmd = "wmic /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct Get displayName,productState /Format:List"; StringBuilder sb = new StringBuilder(); Process pro = Runtime.getRuntime().exec("cmd.exe /c "+cmd+""); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream())); System.out.println(cmd); while (bufferedReader.ready()) { sb.append(bufferedReader.readLine()); } System.out.println(sb.toString()); } catch (IOException ex) { }
Netbeans (the IDE im using) marks the String cmd line as red and it says the following error:.ilegal escape character
Here's one thing i encountered while trying to solve this... when i print the String "cmd" it doesn't recognize the whole command, the "\\root" part is printed as "\oot"... So instead i tried using "\\\\root\\\SecurityCenter2" and it was printed as it is mean to be. I don't really know what's wrong, but i think it has something to do with Java recognizing the slashes. Anyways, i hope you guys can help me, i could really use a hand since this is an esencial part of my career project. Thanks beforehand.