| | |
Run Unix Find command using java Program
![]() |
•
•
Join Date: Oct 2008
Posts: 26
Reputation:
Solved Threads: 1
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
)
,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
) Last edited by babusek; Oct 15th, 2008 at 8:23 am.
So your permissions on that folder are not correct. Can you type
in your Cygwin window and check the permissions of your folder? It's the part that looks like
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.
Java Syntax (Toggle Plain Text)
ls -l -a
Java Syntax (Toggle Plain Text)
rwxr--r--
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Oct 2008
Posts: 34
Reputation:
Solved Threads: 5
That runtime.exec stuff is a real pain to use with a lot of traps. Here's a great article that's helped me out a lot:
http://www.javaworld.com/javaworld/j...229-traps.html
It's a long read, but explains a lot.
HTH.
http://www.javaworld.com/javaworld/j...229-traps.html
It's a long read, but explains a lot.
HTH.
•
•
Join Date: Oct 2008
Posts: 26
Reputation:
Solved Threads: 1
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) .........
--------------------------------------------------------------------
In the above code What Does the "workDir" will do ?
----------------------------------------------------------------------
i hope U people got my point
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) .........
java Syntax (Toggle Plain Text)
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
Last edited by cscgal; Oct 15th, 2008 at 11:24 am. Reason: Added code tags
•
•
Join Date: Oct 2008
Posts: 34
Reputation:
Solved Threads: 5
•
•
•
•
HI brianlevine
thanx for reply, Already i gone through that link,it's awesome..
But i cant figure out this problem.
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.
•
•
Join Date: Oct 2008
Posts: 26
Reputation:
Solved Threads: 1
•
•
•
•
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.
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 ?
•
•
Join Date: Oct 2008
Posts: 26
Reputation:
Solved Threads: 1
•
•
•
•
can you post the exact error message you're getting from cygwin?
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
Last edited by babusek; Oct 17th, 2008 at 5:26 am.
![]() |
Similar Threads
- Implementing a unix shell running commands (Java)
- This Should be Easy for You Guys! (Linux Servers and Apache)
Other Threads in the Java Forum
- Previous Thread: search for keywords from set of urls
- Next Thread: Need logic to complete this program using recursion
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking bidirectional binary binarytree birt bluetooth chat chatprogramusingobjects class client code columns component database derby design eclipse encryption error errors expand fractal game givemetehcodez graphics gui guidancer homework html ide if_statement image inheritance integer intellij interface j2me java javadesktopapplications javaprojects jlabel jme jni jpanel jtextfield julia linux list map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source problem program programming project property recursion reference ria scanner search server set sms sort sourcelabs splash sql sqlite static stop string support swing testautomation threads tree ui unicode validation windows





