2

I know we can run a linux terminal command in a java program like following code shows a "ls" command for the home directory.

String[] cmd1 =  {
        "/bin/sh",
        "-c",
        "cd ../.. && ls"};
        Process child1 = Runtime.getRuntime().exec(cmd1);
        child1.waitFor();

final BufferedReader is = new BufferedReader(new InputStreamReader(child1.getInputStream()));
        String line;
        while ((line = is.readLine()) != null) {

            System.out.println("output is: "+line);
        }

But I have no idea what "/bin/sh" and "-c" mean for? I searched it online and somebody used "bash" or else. And if I run the command as "cd ../.. && ls" only, I will get the same result.

So if there is a command in terminal as "txl -o output inputFile", where "txl" is a tool installed, how to write in in java? I tried

String[] cmd1 =  {
        "/bin/sh",
        "-c",
        "cd ../.. && txl -o output inputFile"};

But could not get the result. I add "cd ../.." to go to the home directory first from the working space because txl is installed in the home bin directory.

Can anyone give me some suggestions?

EDIT:

I made a stupid spelling error and the command in this format works!

tshepang
  • 12,111
  • 21
  • 91
  • 136
Eve
  • 785
  • 3
  • 7
  • 15
  • I would, personally, avoid using `Runtime.exec` directly and instead use `ProcessBuilder`. Having said that, you `cmd` array should be split so that each element represents an individual parameter that would sent to the command your are executing. You example would suggest that `cd ../.. && ls` will appear as a single argument to the command you are executing, which isn't what you want – MadProgrammer Jul 23 '13 at 11:08
  • Thanks, madprogrammer. If so, which way is correct to write cmd1 `String [] cmd1 = {"/bin/sh", "-c","cd ../..", "txl -o output inputFile"}` or `String [] cmd1 = {"cd ../..", "txl -o output inputFile"}`? Or could you tell me how to write cmd1? Thanks! @MadProgrammer – Eve Jul 23 '13 at 11:22
  • I would suggest something more like `{"/bin/sh", "-c", "cd", "../..", "&&", "txl", "-o", "output", "inputFile"}` but you may need to experiment with it – MadProgrammer Jul 23 '13 at 11:24

2 Answers2

0

you can find here what every parameter in ls do:

-c
 with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime

in order to execute a program in java and wait for it to finish you can use this post

Community
  • 1
  • 1
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

/bin/sh is a program, this is the shell, the option -c specifies that the following is a command to be executed by the shell (which is the third "line" of your cmd1: cd ../.. && ls)

EDIT:

In this case is nessesary to execute the shell, executing only cd ../.. && ls won't work, because operators like the && between cd and ls are not understood by Java but by the shell.

morgano
  • 17,210
  • 10
  • 45
  • 56
  • Thanks, morgano. If so, it should work for command "cd ../.. && txl -o output inputFile" right? Or the java program can only run some default commands like ls, cd, dir etc? @morgano – Eve Jul 23 '13 at 11:17
  • @Eve I fixed my answer, actually you won't get the same result if you try to execute only cd ../.. && ls – morgano Jul 23 '13 at 11:22