hey friends i m using servlet in order to write the output to jsp but i m getting output as something BufferedReader.io.inpitstreamreader@somenumbers
i want to return the string output to jsp back .i need help stuck i this thing

try
                  {
   
       String line;
        // String path="/home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/"+filen[0];
      Process p = Runtime.getRuntime().exec
       ("java  -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/" +filen[0]);
     BufferedReader input =
       new BufferedReader
         (new InputStreamReader(p.getInputStream()));
     line=input.readLine();
     while (line  != null) 
     {
       out.println("<html>");
    out.println("<body>");
    out.println("Output is:" +line  );

    out.println("</body></html>");
  
    out.close();
    }

                    }
      catch (Exception err)
      {
     err.printStackTrace();
    }

Recommended Answers

All 8 Replies

First of all why are you calling out.close ? And especially inside the while. What needs closing is the BufferedReader input, outside the while. You close it once you are done reading. If you close it inside the while, the next loop will execute and try to read the next line, from a closed BufferedReader.

same issue with this code i infact tried

try
                  {
   
       String line;
        // String path="/home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/"+filen[0];
      Process p = Runtime.getRuntime().exec
       ("java  -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/" +filen[0]);
   //  BufferedReader input =
    //   new BufferedReader
      //   (new InputStreamReader(p.getInputStream()));
     //line=input.readLine();
    // while (line  != null)
    // {
       out.println("<html>");
    out.println("<body>");
    out.println("Output is:" +p.getInputStream()  );

    out.println("</body></html>");
  
  //  out.close();
   // }

                    }
      catch (Exception err)
      {
     err.printStackTrace();
    }

and the output is
Output is:java.io.BufferedInputStream@bc9673

In your first code you were printing this:

line=input.readLine();
out.println("Output is:" +line  );

That should work. Have you tried it?


At the second code you are printing the object: p.getInputStream() . That is an InputStream object. It doesn't have any "printable" values.
It is not like you have a String object and you can print its value. When you call this: out.println(p.getInputStream()) with an object as argument, the toString method of the object is called.
It is like doing this: out.println(p.getInputStream().toString()) If you look at the API that object doesn't override the toString method from its super class: Object.toString(). That is why you get that, in comparison with this:
String.toString()

You need to use the BufferedReader to print each line.

yes but tht iz also not workin .i gave u the output it shows .need help tht also real fast man have to rap this up till tmrw

This has more chances of working. Compare the changes I made with your own. What do you get as ouput:

try
                  {
   
       String line;
        // String path="/home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/"+filen[0];
      Process p = Runtime.getRuntime().exec
       ("java  -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/" +filen[0]);
     BufferedReader input =
       new BufferedReader
         (new InputStreamReader(p.getInputStream()));
     line=input.readLine();
out.println("<html>");
    out.println("<body>");
     while (line  != null) 
     {
       
    out.println("<br/>Output is:" +line  );
[B]line=input.readLine();[/B]
    }
input.close();
    out.println("</body></html>");
                    }
      catch (Exception err)
      {
     err.printStackTrace();
    }

Also these need to be outside the loop: the <html> and <body> tags should be printed only once.

Also you only call the readline only once. Meaning that you will keep printing the same line and the loop will never end. It will run indefent.

thanks for taking such intrest but i tried the above code u gave .
its not returning me the output

<html>
<body>
</body></html>

it is returning this nothing else

What do you expect to get?
Try to put some System.out.println outside and inside the loop and see if it goes inside. Maybe it never goes inside the loop becuase the command: java -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/remote_compile/ filen[0] doesn't return anything. Try to print hte filen[0] and run it at a command prompt.

hey i resolved the issue the exec() was returning null n sometimes @some numbers.
so tried giving a static name like
exec("java -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/test_compile/ hello"
thn it worked
n finally converted it to
exec("java -classpath /home/muzammil/apache-tomcat-6.0.18/work/Catalina/localhost/test_compile/ "+filen[0].toString() ); for dynamic naame catching
n the issue resolved
thank u guys for ur help n showing intrest in my problem specific to javaadict .
thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.