Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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    
0
jason101799
Asked:
jason101799
  • 2
  • 2
  • 2
1 Solution
 
OviCommented:
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) {}
  }
}
0
 
OviCommented:
Save this code in a file called OutputCapture.java (case sensitive), compile'it like : javac OutputCapture.java, and runnit like java OutputCapture.

I see that you are using the ls command (Linux specific). If you are Windows user, change the line : String cmd = "ls -al"; into String cmd = "dir *.*";
0
 
jason101799Author Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jason101799Author Commented:
ovi,

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

Thanks


Cheers
Jason
0
 
NetminderCommented:
jason@lc,

These questions are still open. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days.

http://experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20167319
http://experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20153401
http://experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20151128
http://experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20146743
http://experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20150594
http://experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20245522
http://experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20239352
http://experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20166221
http://experts-exchange.com/jsp/qShow.jsp?ta=xml&qid=20243323
http://experts-exchange.com/jsp/qShow.jsp?ta=exchangesvr&qid=20240796

EXPERTS: Please leave your thoughts on this question here.

Thanks,

Netminder
Community Support Moderator
Experts Exchange
0
 
NetminderCommented:
Admin notified of User neglect. Force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now