Solved

Program error!!

Posted on 2001-07-06
6
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

752 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