Hi Folks:) ,

Problem: how to run unix "find" Command in Java Program using Cygwin Environment.

I have been using the Runtime & Process classes to run the unix commands in java program,i can able to run the grep ,cat command etc.. But i Couldn't able to run the "find" Command
and Redirection(>>) Operator , i dont know the Reason ..

Anyone here plz help me on this ..

Here is my code :

String s =null;
:'( line 1:  String command2 = "find . -name "+search_file+ " -print";:'(
( "search_file " be the argument).
line 2: Process proc =rt.exec(find);


line 3: BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while(   ( s=br.readLine()   )!=null  )
{ System.out.println(" File Found : " + s); }


result after run this program is
(and the return value of "find" command is "2 " seems like Permission denied:( )

So your permissions on that folder are not correct. Can you type

ls -l -a

in your Cygwin window and check the permissions of your folder? It's the part that looks like

rwxr--r--

The permissions relate to your permission, your group's permission and other permissions. If you are trying to run the find command from a program, you will need the 'other' permissions to be set to at least read, if not read write execute. To change the permission, use the chmod command.

HI darkagn,
ya i changed the permissions of the folder , but still getting same problem.

i have given 777 permission to that directory.

HI brianlevine
thanx for reply, Already i gone through that link,it's awesome..

But i cant figure out this problem.

Hi folks,

Process p = Runtime.getRuntime().exec(cmd, null, workDir);

what's the " workDir " Represents here

Does it related to command(unix)?

------------------Please go through this code(it is not main) .........

public class RunSystemCommand {
public static void main(String args[]) {
String s = null;
// system command to run
String cmd = "ls > fred.txt";
// set the working directory for the OS command processor
File workDir = new File("/dir1/dir2");

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);
}
}
}

--------------------------------------------------------------------
In the above code What Does the "workDir" will do ?
----------------------------------------------------------------------
i hope U people got my point

HI brianlevine
thanx for reply, Already i gone through that link,it's awesome..

But i cant figure out this problem.

Did you look at page 4? From the article:
The incorrect assumption here is that the exec() method acts like a shell interpreter; it does not. Instead, exec() executes a single executable (a program or script). If you want to process the stream to either redirect it or pipe it into another program, you must do so programmatically, using the java.io package.

Did you look at page 4? From the article:
The incorrect assumption here is that the exec() method acts like a shell interpreter; it does not. Instead, exec() executes a single executable (a program or script). If you want to process the stream to either redirect it or pipe it into another program, you must do so programmatically, using the java.io package.

Well , did u look at listing 4.4 in the page 4 there the ouput of the file "BadExecWinDir.java"
shown as file not found error but as i executed in my program the same command it listing me the current files in the Directory ..


What Does it mean ?
Do u agree with output of the Program ?

can you post the exact error message you're getting from cygwin?

can you post the exact error message you're getting from cygwin?

HI
This is the Command :
:'( String command2 = "find . -name "+search_file+ " -print";

Where "search_file" is the argument ....

Error Message : : I have Taken The Return Value from the Command through
proc.exitValue();
And it is returning the value "2",i know that any non zero value represents Error..

.............if u want i can post Entire Code for u .............

Thankq waiting for u'r reply

Hi, sorry for the delay in responding...

And actually sorry, but I don't know how to help. The fact that you can run cat and grep really is weird. The only thing I can think of is running find from the command line to make sure it works there. I don't know, maybe someone else can help...

Hi, sorry for the delay in responding...

And actually sorry, but I don't know how to help. The fact that you can run cat and grep really is weird. The only thing I can think of is running find from the command line to make sure it works there. I don't know, maybe someone else can help...

Thankq ,

i found that using java Program through " Cygwin(Environment) " we can't run "find' , "awk" and 'redirection opertaior".so i changed my platform , now i have written the java program in linux machine itself ,using the Netbeans ide.Eventhough i cant able to run the Redirection Operator.
:( Command: ls >> newfile.txt :( it doesn't create the newfile

i dont know y?

can any one plz help me on this....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.