Search code examples
javaunixcommandexecute

Need help running a recursive touch from Java JSP


Is there any way in Java to use the exec method to perform a recursive touch?

The goal is to setup a simple webpage that when reloaded will touch the dir for the site so that design can guarentee caching is no longer happening. Any help please!!!

Here is what I have so far, and not working in my jsp:

<%@ page import="java.io.BufferedReader,java.io.File,java.io.FileWriter, java.io.IOException, java.io.InputStreamReader, java.util.Map" %>

<%


String s = null;
// system command to run
String cmd = "find /home/abcdefg/ -exec touch {} \\;";
// set the working directory for the OS command processor
File workDir = new File("/home/ss/public_html/ss");

try {
Process p = Runtime.getRuntime().exec(cmd, null, workDir);
int i = p.waitFor();
if (i == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
}
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdErr.readLine()) != null) {
System.out.println(s);
}

}
}
catch (Exception e) {
System.out.println(e);
}


%>

Solution

  • All along the same path - I figured out the answer and it is close to what you guys posted:

    <% String[] cmd = {"/bin/sh", "-c", "cd /xx/xx/xx/xx; find . -exec touch {} \;"}; Process p = Runtime.getRuntime().exec( cmd ); %>

    Thank you guys for the quick response!!

    Daniel