gudii9
asked on
standard output java
Hi,
I am trying below example
http://www.avajava.com/tutorials/lessons/how-do-i-run-another-application-from-java.html?page=2
when i run this example i do not see any output at console. How to fix it.
please advise
Any links resources ideas highly appreciated. Thanks in advance
I am trying below example
http://www.avajava.com/tutorials/lessons/how-do-i-run-another-application-from-java.html?page=2
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class RuntimeExecTest2 {
public static void main(String[] args) {
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("java -classpath C:\\tutorials test.TestOutput");
InputStream inputStream = process.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
InputStream errorStream = process.getErrorStream();
InputStreamReader esr = new InputStreamReader(errorStream);
int n1;
char[] c1 = new char[1024];
StringBuffer standardOutput = new StringBuffer();
while ((n1 = isr.read(c1)) > 0) {
standardOutput.append(c1, 0, n1);
}
System.out.println("Standard Output: " + standardOutput.toString());
int n2;
char[] c2 = new char[1024];
StringBuffer standardError = new StringBuffer();
while ((n2 = esr.read(c2)) > 0) {
standardError.append(c2, 0, n2);
}
System.out.println("Standard Error: " + standardError.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
when i run this example i do not see any output at console. How to fix it.
please advise
Any links resources ideas highly appreciated. Thanks in advance
Looks right. What happens when you run it?
Well, there will be some output - an error message if you haven't got
java -classpath C:\\tutorials test.TestOutput
in your classpath.
java -classpath C:\\tutorials test.TestOutput
in your classpath.
ASKER
java -classpath C:\\tutorials test.TestOutput
in your classpath.
Do i supposed to give that in double quotes as i am giving. What is 'test' here.
Do i supposed to create that folder under C directory before executing this program. please advise
ASKER
i see my console surprisingly empty and also i do not wsee TestOutput file being created. I wonder if this example worked for you? Please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I changed my program as below
I still did not see output. How to fix it. please advise.
OutputEclipse.jpg
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class RuntimeExecTest2 {
public static void main(String[] args) {
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("java -classpath C:\\tutorials test.TestOutput");
InputStream inputStream = process.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
InputStream errorStream = process.getErrorStream();
InputStreamReader esr = new InputStreamReader(errorStream);
int n1;
char[] c1 = new char[1024];
StringBuffer standardOutput = new StringBuffer();
while ((n1 = isr.read(c1)) > 0) {
standardOutput.append(c1, 0, n1);
}
System.out.println("Standard Output: " + standardOutput.toString());
int n2;
char[] c2 = new char[1024];
StringBuffer standardError = new StringBuffer();
while ((n2 = esr.read(c2)) > 0) {
standardError.append(c2, 0, n2);
}
System.out.println("Standard Error: " + standardError.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
I still did not see output. How to fix it. please advise.
OutputEclipse.jpg
Must be an Eclipse thing. I use NetBeans for Java, and it works fine.
Why don't you just open a command prompt and run it from there manually? That way Eclipse can't get in your way.
Why don't you just open a command prompt and run it from there manually? That way Eclipse can't get in your way.
ASKER
I see output when i an at home laptop as below
Standard Output:
Standard Error: Error: Could not find or load main class test.TestOutput
how to fix it. please advise
Standard Output:
Standard Error: Error: Could not find or load main class test.TestOutput
It calls the exec method with "java -classpath C:\\projects\\workspace\\testing\\bi n test.TestOutput". We need to set the classpathW so that 'java' can find the directory where our TestOutput class is located. Once again, this example requires 'java' to be on the system path.
The exec call executes the TestOutput class, and the standard output from TestOutput is read (which here is an input stream since we are reading it... the output from TestOutput is the input to RuntimeExecTest2). Additionally, the standard error from TestOutput is read by RuntimeExecTest2.what above sentences means. How to set it from eclipse. What and where i should put in TestOutput, is it is a text file
how to fix it. please advise
The error you are getting means that, based on the classpath and package information that you've provided, java cannot find the TestOutput.class file at runtime.
java -classpath C:\\projects\\workspace\\t esting\\bi n test.TestOutput
Means:
Look in the classpath (C:\\projects\\workspace\\ testing\\b in) for a folder called "test" (the package name).
And execute TestOutput.class.
To make this work:
1) Your TestOutput.java source file must contain a "package test;" statement.
2) Compile TestOutput.java, and you will get TestOutput.class.
3) Put TestOutput.class into a folder called "test", to reflect the package structure.
If your classpath is C:\projects\workspace\test ing\bin\, then this folder needs to be:
C:\projects\workspace\test ing\bin\te st
So the full path to your class file will be:
C:\projects\workspace\test ing\bin\te st\TestOut put.class
Then you are ready to execute this:
java -classpath C:\\projects\\workspace\\t esting\\bi n test.TestOutput
java -classpath C:\\projects\\workspace\\t
Means:
Look in the classpath (C:\\projects\\workspace\\
And execute TestOutput.class.
To make this work:
1) Your TestOutput.java source file must contain a "package test;" statement.
2) Compile TestOutput.java, and you will get TestOutput.class.
3) Put TestOutput.class into a folder called "test", to reflect the package structure.
If your classpath is C:\projects\workspace\test
C:\projects\workspace\test
So the full path to your class file will be:
C:\projects\workspace\test
Then you are ready to execute this:
java -classpath C:\\projects\\workspace\\t
Thanks. ;