Run Unix Find command using java Program

Reply

Join Date: Oct 2008
Posts: 26
Reputation: babusek is an unknown quantity at this point 
Solved Threads: 1
babusek babusek is offline Offline
Light Poster

Run Unix Find command using java Program

 
0
  #1
Oct 15th, 2008
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 )
Last edited by babusek; Oct 15th, 2008 at 8:23 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Run Unix Find command using java Program

 
0
  #2
Oct 15th, 2008
So your permissions on that folder are not correct. Can you type
  1. ls -l -a
in your Cygwin window and check the permissions of your folder? It's the part that looks like
  1. 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.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 26
Reputation: babusek is an unknown quantity at this point 
Solved Threads: 1
babusek babusek is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
0
  #3
Oct 15th, 2008
HI darkagn,
ya i changed the permissions of the folder , but still getting same problem.

i have given 777 permission to that directory.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 34
Reputation: brianlevine is an unknown quantity at this point 
Solved Threads: 5
brianlevine brianlevine is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
1
  #4
Oct 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 26
Reputation: babusek is an unknown quantity at this point 
Solved Threads: 1
babusek babusek is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
0
  #5
Oct 15th, 2008
HI brianlevine
thanx for reply, Already i gone through that link,it's awesome..

But i cant figure out this problem.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 26
Reputation: babusek is an unknown quantity at this point 
Solved Threads: 1
babusek babusek is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
0
  #6
Oct 15th, 2008
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) .........
  1. public class RunSystemCommand {
  2. public static void main(String args[]) {
  3. String s = null;
  4. // system command to run
  5. String cmd = "ls > fred.txt";
  6. // set the working directory for the OS command processor
  7. File workDir = new File("/dir1/dir2");
  8.  
  9. try {
  10. Process p = Runtime.getRuntime().exec(cmd, null, workDir);
  11. int i = p.waitFor();
  12. if (i == 0){
  13. BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  14. // read the output from the command
  15. while ((s = stdInput.readLine()) != null) {
  16. System.out.println(s);
  17. }
  18. }
  19. else {
  20. BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  21. // read the output from the command
  22. while ((s = stdErr.readLine()) != null) {
  23. System.out.println(s);
  24. }
  25.  
  26. }
  27. }
  28. catch (Exception e) {
  29. System.out.println(e);
  30. }
  31. }
  32. }
--------------------------------------------------------------------
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 34
Reputation: brianlevine is an unknown quantity at this point 
Solved Threads: 5
brianlevine brianlevine is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
0
  #7
Oct 15th, 2008
Originally Posted by babusek View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 26
Reputation: babusek is an unknown quantity at this point 
Solved Threads: 1
babusek babusek is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
0
  #8
Oct 16th, 2008
Originally Posted by brianlevine View Post
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 ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 34
Reputation: brianlevine is an unknown quantity at this point 
Solved Threads: 5
brianlevine brianlevine is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
0
  #9
Oct 16th, 2008
can you post the exact error message you're getting from cygwin?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 26
Reputation: babusek is an unknown quantity at this point 
Solved Threads: 1
babusek babusek is offline Offline
Light Poster

Re: Run Unix Find command using java Program

 
0
  #10
Oct 17th, 2008
Originally Posted by brianlevine View Post
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
Last edited by babusek; Oct 17th, 2008 at 5:26 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC