0
 import java.lang.Process; 
 import java.io.*; 
 import java.io.InputStream; 
 import java.io.IOException; 
 public class prgms{ 
 public static void main(String[] args) { 
 try { 
 // Execute a command without arguments 
 String command = "java JavaSimpleDateFormatExample"; 
 Process child = Runtime.getRuntime().exec(command); 
 // Execute a command with an argument 
 // command = "java JavaStringBufferAppendExample"; 
 //child = Runtime.getRuntime().exec(command); 
 } catch (IOException e) { 
 } 
 InputStream in = child.getInputStream(); 
 int c; 
 while ((c = in.read()) != -1) { 
 process((char)c); 
 } 
 in.close(); 
 } 
} 

I have modified this way... but the following error occurs,

prgms.java:17: cannot find symbol 
symbol : variable child 
location: class prgms 
InputStream in = child.getInputStream(); 
 ^
prgms.java:20: cannot find symbol 
symbol : method process(char) 
location: class prgms 
 process((char)c); 
 ^ 
2 errors 
2
  • If you add four spaces before each line of code, StackOverflow will format it nicely. Commented Aug 3, 2010 at 17:44
  • child is not in scope, extend that catch , compare your code with the one in my answer. Commented Aug 3, 2010 at 19:04

3 Answers 3

8

You're indeed ignoring the stdout and stderr streams of the Process returned by Runtime#exec().

This is going to be a long story, so here's just a link: When Runtime.exec won't. Read all the four pages.

answered Aug 3, 2010 at 17:44
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks :) Anyhow i could not reach the solution.. :(
That was quick. Did you read all the 4 pages? What do you have as far now and what happens now?
4

There is no problem with that code.

What is does, is to execute another Java program inside.

The class Process has a method to get the output of the program, you have to redirect that output to your own, if you want to see the result.

Here's a sample using a "modern" alternative to Runtime.exec

// Hello.java says Hello to the argument received.
class Hello {
 public static void main ( String [] args ) {
 System.out.println( "Hello, "+args[ 0 ] );
 }
}
// CallHello.java 
// Invokes Hello from within this java program 
// passing "world" as argument.
import java.io.InputStream;
import java.io.IOException;
public class CallHello {
 public static void main( String [] args ) throws IOException {
 Process child = new ProcessBuilder("java", "Hello", "world").start(); 
 // read byte by byte the output of that progam.
 InputStream in = child.getInputStream();
 int c = 0;
 while( ( c = in.read() ) != -1 ) {
 // and print it
 System.out.print( (char)c);
 }
 }
}

Output:

Hello world
answered Aug 3, 2010 at 17:48

Comments

2

Child is declared inside the try...catch block so its scope is local to that block. You're trying to access it outside of the block. You should declare it before the block, something like

Process child;
try {
// code
child = Runtime.getRuntime().exec(command);
// code
}
catch(/*blah blah*/) {}
// more code

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.