1

I want to list all the file and write the list to a .txt file by using Runtime from Java. And I did this :

File workDir = new File("/home/name/ghost/");
String cmd = "ls -l > data.txt";
Process pr = Runtime.getRuntime().exec(cmd, null , workDir);

it didn't work until I replace the cmd command by a shellscript:

String cmd = "./shell.sh";

and here is what in the shell script :

#!/bin/bash
ls -l > data.txt
exit 0

I want to ask why I can't access system command directly through Java ?

2
  • 1
    Why are you using system commands for this, which wont' be portable, when you could be using java.io instead? Sure, it's more code than the shell script one liner you are using, but you already accepted that the minute you started writing java code. Commented Feb 15, 2011 at 3:10
  • I have no idea about java.io that you said. could you be in more detail 'bout it ? Commented Feb 15, 2011 at 15:06

3 Answers 3

1

Runtime.exec doesn't do what you think it does. In your command, it invokes "ls" with the arguments "-l", ">", and "data.txt", which is what is going wrong. It's as if you had written this:

String[] cmd = new String[] {"ls", "-l", ">", "data.txt"};

If you really wanted to invoke the shell and use the shell's redirection operator, this is easy enough, invoke the shell with your command as an argument to the shell:

String[] cmd = new String[] {"sh", "-c", "ls -l > data.txt"};

Of course you could just do it in Java, as other answers suggest.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that the > operator is a feature of your shell, not of Java or the ls command.

Runtime.exec doesn't attempt to replicate any shell-like behaviour; it just attempts to execute the command you give it. Your second example works because it explicitly uses Bash to run the ls -l > data.txt command and Bash knows how to handle >.

It's probably better to use an approach like @whaley suggested if you can; otherwise using a shell script is likely to be the only way to get shell semantics.

3 Comments

You can use getOutputStream and write to the file manually. However, I agree that this makes no sense in this example.
actually I wanted to call the g++ command. I used ls -l for simplicity
@silentbang: The > is the important bit here. Generally, Java doesn't understand command > file because the > is only meaningful to the shell. I'd suggest you run with your shell script approach if you really want to redirect output to a file. It will be faster and easier than doing it from Java, although it won't be portable.
0

I'm not even going bother answering this question as it is posed, as the same thing can be done using Java only without relying on external processes:

    File dir = new File("/some/dir");
    FileWriter fw = new FileWriter(new File("/some/file"));
    for (File f : dir.listFiles()) {
        fw.write(f.getName() + "\n");
    }
    fw.close();

1 Comment

You should consider the notion that this is an example script for purposes of running more complicated scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.