Solved

Program error!!

Posted on 2001-07-06
6
165 Views
Last Modified: 2010-04-16
<%@ 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
Comment
Question by:jason101799
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6258944
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
 
LVL 9

Accepted Solution

by:
Ovi earned 30 total points
ID: 6258957
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
 

Author Comment

by:jason101799
ID: 6258972
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:jason101799
ID: 6263623
ovi,

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

Thanks


Cheers
Jason
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6853434
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
 
LVL 5

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now