Today I encountered a strange problem when making a Java call to the Windows compression command. The code is as follows:
Copy the code code as follows:
String cmd = "C:/Program Files (x86)/WinRAR/rar.exe ac:/test.rar c:/test.log";
//System.out.println(cmd);
Process proc = Runtime.getRuntime().exec(cmd);
The above code executes completely normally on Xp, win7, and windows server2003. After the code is transferred to Windows Server2008, a java exception is directly prompted.
After two hours of research, the above code was modified as follows:
Copy the code code as follows:
String[] cmd ={"C:/Program Files (x86)/WinRAR/rar.exe", "a","C:/test.rar" ,"c:/test.log"};
//System.out.println(cmd);
Process proc = Runtime.getRuntime().exec(cmd);
It's normal. Another advantage of writing this way is that it can solve the problem of spaces in the file path, etc.