[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

standard output java

Posted on 2014-01-31
11
Medium Priority
?
450 Views
Last Modified: 2014-03-07
Hi,

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();
		}
	}
}

Open in new window


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
0
Comment
Question by:gudii9
[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
  • 4
  • 4
  • 3
11 Comments
 
LVL 35

Expert Comment

by:Gary Patterson
ID: 39827094
Looks right.  What happens when you run it?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39827433
Well, there will be some output - an error message if you haven't got

java -classpath C:\\tutorials test.TestOutput

in your classpath.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39829851
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 39829878
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
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 1000 total points
ID: 39829907
>java -classpath c:/ee_q_code KeyReact

Just like that, no quotes, etc.

. . .  where "c:/ee_q_code" is where your .class file resides, and "KeyReact" is that class.
0
 
LVL 35

Accepted Solution

by:
Gary Patterson earned 1000 total points
ID: 39830537
I created this and ran it and it worked for me on Win 7.  

"test" is the package name.  So, put your compiled classes in c:\tutorials\test

Then run:

java -cp c:\tutorials test.RuntimeExecTest2

- Gary Patterson
0
 
LVL 7

Author Comment

by:gudii9
ID: 39851770
I changed my program as below
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();
		}
	}
}

Open in new window


I still did not see output. How to fix it. please advise.
OutputEclipse.jpg
0
 
LVL 35

Expert Comment

by:Gary Patterson
ID: 39851806
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39878532
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




It calls the exec method with "java -classpath C:\\projects\\workspace\\testing\\bin 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
0
 
LVL 35

Expert Comment

by:Gary Patterson
ID: 39880849
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\\testing\\bin test.TestOutput

Means:

Look in the classpath (C:\\projects\\workspace\\testing\\bin) 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\testing\bin\, then this folder needs to be:

C:\projects\workspace\testing\bin\test

So the full path to your class file will be:

C:\projects\workspace\testing\bin\test\TestOutput.class

Then you are ready to execute this:

java -classpath C:\\projects\\workspace\\testing\\bin test.TestOutput
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39913964
Thanks. ;
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

656 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