Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running find command in Solaris using Runtime.exec

Status
Not open for further replies.

viclap

Programmer
Oct 12, 2000
39
0
0
PH
I'm very new in java and trying to figure out why i'm getting a standard error output of "/usr/bin/find: incomplete statement" --->"find /export/home -type f -mtime +30 -exec rm {} \;" } using java program but executing manually the "find" script command using CLI on solaris Unix os it works fine.

Here is the code:

import java.io.*;
public class PurgeLog
{
//script to remove files older than 30 days
public static String CmdList = { "find /export/home -type f -mtime +30 -exec rm {} \;" };

public static void main (String[] args)

{
System.out.println("You are about to purge files older than 30 days ....... [Y/N]: ");
String s = null;
String name = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
name = br.readLine();
if (name.equalsIgnoreCase("Y"))
{
System.out.println("Starting to purge Old LogFiles, Pls Wait........\n");
System.out.print("");
System.out.println("Executing--->"+CmdList);
Process p = Runtime.getRuntime().exec(CmdList);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
//System.out.println("Exit Value " + exitval);
// read any errors from the attempted command
System.out.println("Standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
//int exitval = p.waitfor();
}
int exitIn = p.waitFor();
int exitOut = p.waitFor();
}
else
{
System.out.println("Not continuing......Thanks");
System.exit(-1);
}
} //try
catch (IOException e)
{
System.out.println("Error!--->"+e.getMessage());
System.exit(-1);
}
catch (Throwable t) { t.printStackTrace();}
}
}

Any help would be gladly appreciated.Thanks in advance.
 
String CmdList = { "find /export/home -type f -mtime +30 -exec rm {} \;" };

You call it a list, but it isn't magically converted to a list, not is it a list in itself.

On the shell, a command and its parameters are divided by whitspace, so that would be a list, consisting of "find", "/ecport/home", "-type", "f" and so on.

You know, that to avoid the splitting, you mask the the blanks on the commandline, so
Code:
find "/export/home -type"
would mean that find shall search for a starting directory "/expoert/home -type".

Similarly you advice Runtime to execute a command called "find /export...".
Instead, you have to split it in a String array
Code:
String [] cmdList = {"find", "/export/home", "-type", ... };

Then, java does not inherit a path from the invoking shell or environment, so you have to give the full name /usr/bin/find, not just find, and "-exec" "/bin/rm" - but instead of -exec rm you would like to call -delete (maybe not available for sun/find, just Linux:Gnu/find?).

For further information read:

don't visit my homepage:
 
Thank you very much. This such a big help for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top