943,513 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9604
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 15th, 2008
0

Run Unix Find command using java Program

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Oct 15th, 2008
0

Re: Run Unix Find command using java Program

So your permissions on that folder are not correct. Can you type
Java Syntax (Toggle Plain Text)
  1. ls -l -a
in your Cygwin window and check the permissions of your folder? It's the part that looks like
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Oct 15th, 2008
0

Re: Run Unix Find command using java Program

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

i have given 777 permission to that directory.
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Oct 15th, 2008
1

Re: Run Unix Find command using java Program

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.
Reputation Points: 37
Solved Threads: 5
Light Poster
brianlevine is offline Offline
36 posts
since Oct 2008
Oct 15th, 2008
0

Re: Run Unix Find command using java Program

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

But i cant figure out this problem.
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Oct 15th, 2008
0

Re: Run Unix Find command using java Program

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) .........
java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Oct 15th, 2008
0

Re: Run Unix Find command using java Program

Click to Expand / Collapse  Quote originally posted by babusek ...
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.
Reputation Points: 37
Solved Threads: 5
Light Poster
brianlevine is offline Offline
36 posts
since Oct 2008
Oct 16th, 2008
0

Re: Run Unix Find command using java Program

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 ?
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008
Oct 16th, 2008
0

Re: Run Unix Find command using java Program

can you post the exact error message you're getting from cygwin?
Reputation Points: 37
Solved Threads: 5
Light Poster
brianlevine is offline Offline
36 posts
since Oct 2008
Oct 17th, 2008
0

Re: Run Unix Find command using java Program

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
babusek is offline Offline
28 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: search for keywords from set of urls
Next Thread in Java Forum Timeline: Need logic to complete this program using recursion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC