Link to home
Start Free TrialLog in
Avatar of jason101799
jason101799

asked on

Program error!!

<%@ page import="java.io.*";%>                        
<%!                                                    
public void display(){                                
   String rtn="";                                      
   Runtime rt = Runtime.getRuntime();                  
   try {                                              
      Process d = rt.exec("ls");                      
      InputStream in = d.getInputStream();            
      DataInputStream din = new DataInputStream(in);  
      String inline = new String("start");            
      while (inline != null) {                        
         inline = din.readLine();                      
         if (inline != null ) {                        
            rtn = inline;                              
            System.out.println(rtn);                  
         }                                            
       }                                              
       din.close();                                    
    }                                                  
    catch(IOException e) {                            
    System.out.println("IOException");                
    }                                                  
        //return rtn;                                  
}                                                
%>                                              
<html><head><title>test</title></head><body>    
<%= display()%>                                  
</body></html>                              


__________________________________________________________



Whats worng with the above code? How can I print out all the lines when I execute the ls command? How do I change directory from the above code?

Please help.

Cheers
Jason    
Avatar of Ovi
Ovi

import java.io.*;

public class OutputCapture {
  public static void main(String[] args) {
    String cmd = "ls -al";
    try {
      Process process = Runtime.getRuntime().exec(cmd);
      InputStream is = process.getInputStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      String line = "";
      while ((line = reader.readLine()) != null) {
        System.out.println("Line : " + line);
      }
    } catch(Exception e) {}
  }
}
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jason101799

ASKER

ovi,

I wanted it to be in JSP. Can u help? One more qs is how do i execute a file which contains a string like "echo hello".

CHeers
Jason
ovi,

I need that file to be in JSP, perhaps you can advice.

Thanks


Cheers
Jason
Admin notified of User neglect. Force-accepted by
Netminder
Community Support Moderator
Experts Exchange