0

Hi i tried to execute the following command from java code in linux, ls > out.txt

here is my code

try 
            { 
                Process p=Runtime.getRuntime().exec("ls > out.txt"); 
                p.waitFor(); 
                BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
                String line=reader.readLine(); 
                while(line!=null) 
                { 
                System.out.println(line); 
                line=reader.readLine(); 
                } 

            } 
            catch(IOException e1) {} 
            catch(InterruptedException e2) {} 

            System.out.println("Done"); 

I checked output file was not generated. However if I leave the output file part only run ls command it successfully executes without error and I can see the output.

P basak
  • 4,874
  • 11
  • 40
  • 63
  • Try `e1.printStackTrace()` to see if any exception is occuring. – Bhesh Gurung Oct 16 '12 at 03:59
  • Where do you expect the output file to be created? Do you have write access to the directory where the file is to be created? Did you try specifying an absolute path to the output file? – Sergey Kalinichenko Oct 16 '12 at 04:06
  • 1
    Nice error handling. Also you should output the process's ErrorStream so as to see any errors generated by the OS. Also, you may need to invoke the operating system's command interpreter when making system or shell command calls. – Hovercraft Full Of Eels Oct 16 '12 at 04:07
  • 1
    In my tests, it seems that it didn't like the redirection modifier – MadProgrammer Oct 16 '12 at 04:10
  • Check out http://stackoverflow.com/questions/1410741/want-to-invoke-a-linux-shell-command-from-java for more suggestions – MadProgrammer Oct 16 '12 at 04:12
  • @Mad: the redirection modifier should work as long as the command explicitly invokes a linux shell when making shell commands as per my previous comment. – Hovercraft Full Of Eels Oct 16 '12 at 04:20
  • @HovercraftFullOfEels Yep, picked that up via the supplied link in mine...I can make it work under windows using `cmd /c` – MadProgrammer Oct 16 '12 at 04:24
  • Hi the problem was fixed when I used the command like ("sh -c \"ls > out.txt\""); – P basak Oct 16 '12 at 06:19
  • 1
    @pbasak: please show your final fix as an edit to your original question or as an answer to your question. For the sake of future readers of this question, please add reasonable error handling, please make sure you are reading the displaying the error stream as well. – Hovercraft Full Of Eels Oct 16 '12 at 12:03
  • Hi actually I learned the way to execute a shell script and getting the out put as well. – P basak Oct 16 '12 at 18:27
  • I just omitted the exception handling to focus on the main part. – P basak Oct 16 '12 at 18:33

2 Answers2

1

I think it's because the pipe character > is a shell operator and your exec isn't being created with a shell.

Maybe what you are trying to do is more complex but for listing the directory you can just use a File. Then you can iterate those and save them to a file using a PrintWriter.

File dir = new File("/some/path");
PrintWriter writer = new PrintWriter("output.txt");
for(File file : dir.listFiles()){
   writer.println(file.getPath());
}
writer.close();
Hiro2k
  • 5,254
  • 4
  • 23
  • 28
0

What i did is that I opened a new terminal with specific command, in this way I can handle commands whose output will be a continuous stream of events, like adb logcat of android. I did it according to this tutorial. Linux users will find something similar to this.

Community
  • 1
  • 1
P basak
  • 4,874
  • 11
  • 40
  • 63