Running perl program and get the output in java program

We have a perl program which can't be converted to java without significant developer time which i want to execute in java and get the output in a string. For simplicity sake i was first trying with ls command to see if i can get the output. However nothing is displayed in the console


Process p=Runtime.getRuntime().exec("/usr/bin/ls");
			OutputStream output=p.getOutputStream();
			p.waitFor();
			output.flush();

Open in new window

angelblade27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You need to consume stdout and stderr. See the process streams methods in the class mentioned here:

http://technojeeves.com/joomla/index.php/free/51-copying-streams

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Mick BarryJava DeveloperCommented:
read the output (from output) *before* waiting for the process to complete

http://helpdesk.objects.com.au/java/runtime-exec-locks-up
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Mick BarryJava DeveloperCommented:
Process p=Runtime.getRuntime().exec("/usr/bin/ls");
InputStream in=p.getOutputStream();
// read output line by line
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while (null!=(line=in.readLine())) {
   System.out.println(line);
}
angelblade27Author Commented:
is it possible to have the command execute in a new shell? Seems that the perl program doesn't run correctly because of some of the environment variables which eclipse may be setting.
CEHJCommented:
It will be a new shell. Can you post the perl?
angelblade27Author Commented:
Unfortunately i can't since it is company specific. All i could say it that it works fine when i run the script from my desktop but fails to find certain modules when executed from the java program
Mick BarryJava DeveloperCommented:
> is it possible to have the command execute in a new shell?

sure, just specify the shell in the command line

sh myscript
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.