0

I have a class file say, abc.class which got compiled from abc.java While trying to execute this java program, I am not getting the output in my console. I have executed this file in linux environment. the file contains one of the line which draws importance :

Runtime rt = Runtime.getRuntime();
String a = "ls";

Process proc = null;
try {
    proc = rt.exec(a);
}

Note : While executing the program, the prompt is not there and i think that might be the reason for 'ls' not getting executed through my program. I am not getting any error though. I am concern that as soon as the file get execute I will get the list of file in the prompt. Hope i make to clear you all guys about my issue. Please can I have any input on this issue of mine. your valuable

M A
  • 71,713
  • 13
  • 134
  • 174
Abuzar
  • 13
  • 5
  • I think it would be of some help – Udit Mishra Jul 02 '14 at 09:08
  • [Here](http://www.coderanch.com/t/419192/java/java/Runtime-getRuntime-exec-String-command) you have an example. – Jens Jul 02 '14 at 09:09
  • How do you know it's not getting executed? Are you handling the output in some way? – Robby Cornelissen Jul 02 '14 at 09:11
  • You have to make a difference between commands provided by some shell and other commands. If you want to execute a shell command you have to execute the shell first. The same is also true for Windows, therefore in several examples you can see runtime.exec("cmd.exe ") for the windows case – mschenk74 Jul 02 '14 at 09:14

2 Answers2

0

Your command should be executed ,but your output is not printed as by default output will be not directed to your console , for getting output on your console, you just need to take input stream from your process and need to read it.

Runtime rt = Runtime.getRuntime();
String a = "ls";

  Process proc = null;
  try
  {
    proc = rt.exec(a);
    proc.waitFor();
    BufferedReader reader =  new BufferedReader(new InputStreamReader(proc.getInputStream())); 

    String line=" ";
    while((line=reader.readLine())!=null)
    {
       output.append(line+"\n");
    }
  }
Abhijeet Panwar
  • 1,837
  • 3
  • 26
  • 48
0

This works:

Runtime rt = Runtime.getRuntime();
String a = "/bin/ls";

    Process proc = rt.exec(a);
    proc.waitFor();
    InputStream is = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader( is );
    BufferedReader br = new BufferedReader( isr );
    String line;
    while( (line = br.readLine() ) != null ){
    System.out.println( line );
    }
    br.close();
    // to see errors:
    System.out.println( "*** error output ***" );
    InputStream isErr = proc.getErrorStream();
    InputStreamReader isrErr = new InputStreamReader( isErr );
    BufferedReader brErr = new BufferedReader( isrErr );
    String line;
    while( (line = brErr.readLine() ) != null ){
    System.out.println( line );
    }
    brErr.close();
laune
  • 31,114
  • 3
  • 29
  • 42
  • Apology Laune, I have not checked it yesterday. – Abuzar Jul 03 '14 at 06:08
  • however the string whatever you have given is not getting executed becuase I am in different path. I cant understand why this is happening . If the sting I am passing as "ls" only, it works but "/bin/ls" is not working. FYI I am in ZSH.... Could you please suggest where I am faulting – Abuzar Jul 03 '14 at 06:09
  • Check the return value of `which ls` in your shell. The absolute path returned from this command is where the binary is stored. But if you are happy with `String a = "ls";` then leave it. (It may not work on someone else's system, though.) – laune Jul 03 '14 at 06:16
  • it is an inquisition.. ls is working fine but what if I required to list out all the files and directories from a different path say bin, using "/bin/ls" is not working at all. Please suggest/help – Abuzar Jul 03 '14 at 07:19
  • Add reading of the error stream - see my edited answer. You'll have to answer my questions precisely. 1) What happens if you use `String a = "/bin/ls";`? 2) What happens if you use `String a = "ls /bin";`? – laune Jul 03 '14 at 07:29
  • Hi Laune,1) is not giving any ouput. 2) will list out the files. I can use the second option but is not quite ok suppose for ls it works fine by giving ls /bin but what if I want to give another command like mkdir in different path. The main concern for me is to have a xterm display and first we will change the path and then comes the command execution ... say from usr, I change the path to /usr/bin and there i need to create one dir using mkdir... how will this gonna be happens...to simplify I ask about ls... Hope it makes me clear – Abuzar Jul 03 '14 at 07:39
  • Well, if you want to have a shell so that you can type in arbitrary commands (ls, mkdir, rm,...) you'll have to start `xterm` from your Java application, and xterm will run a shell... See here: http://stackoverflow.com/questions/17483199/how-to-run-another-java-process-with-console-in-java-in-linux?rq=1 (But that wasn't you initial question?!) – laune Jul 03 '14 at 07:53