Cannot empty linux cache via java

cache-control, java

Solution

Well, your command is using features of the shell (the && and the > redirection) , so you need to run that command in a shell. i.e. you need to run

/bin/sh -c "sync && echo 3 > /proc/sys/vm/drop_caches"

Which you can do by using this as a command

String[] commands = {"/bin/sh", "-c" , "sync && echo 3 > /proc/sys/vm/drop_caches"};

Problem

ok here is my code: ``` String[] commands = {"sync && echo 3 > /proc/sys/vm/drop_caches"}; Process pr = Runtime.getRuntime().exec(commands); pr.waitFor(); ``` And I get: ``` Exception in thread "main" java.io.IOException: Cannot run program "sync && echo 3 > /proc/sys/vm/drop_caches": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) at java.lang.Runtime.exec(Runtime.java:615) at java.lang.Runtime.exec(Runtime.java:483) at MyTest.main(MyTest.java:896) Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.<init>(UNIXProcess.java:135) at java.lang.ProcessImpl.start(ProcessImpl.java:130) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021) ``` I copy paste the same string at the command line and I think works fine. What is the problem? I am using Ubuntu 11.10. Thanks

Original source