Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Program error!!

Posted on 2001-07-06
6
Medium Priority
?
176 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 120 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

670 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