Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How can I run Runtime.exec command in Java??

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sir:

I found a program very close to my project, see below:

I met a problem when I want to use a Java program to run other java processes by Runtime.exec command,
How can I run Runtime.exec command in Java To invoke several other java processes??

see code below,
I want to use HelloHappyCall to call both HappyHoliday.java and HellowWorld.java,

[1]. main program,


package abc;

import java.util.*;
import java.io.*;

class HelloHappyCall
{
public static void main(String[] args){
try
{
Runtime.getRuntime().exec("java -version");
Runtime.getRuntime().exec("cmd /c java HelloWorld "); // here start will create a new process..

System.out.println("Never mind abt the bach file execution...");
}
catch(Exception ex)
{
System.out.println("Exception occured: " + ex);
}
}
}



[2]. sub 1 program



package abc;

import java.util.*;
import java.io.*;

class HelloWorld
{
public static void main(String[] args){

System.out.println("Hellow World");
}
}



[3]. Sub 2 program:


package abc;

import java.util.*;
import java.io.*;

class HappyHoliday
{
public static void main(String[] args){

System.out.println("Happy Holiday!!");
}
}



When I run, I got following:

Never mind abt the bach file execution...


I cannot see both Java version and Hellow World print, what is wrong??

I use eclipse3.2



Can any guru help how to solve it??

Regards
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since those sub-processes have no console attached to them, your program needs to handle their I/O for them. THis is explained in this article.

Wouldn't it be easier to run those programs by calling "HelloWorld.main(new String[] { })" and "HappyHoliday.main(new String[] { })", respectively, assuming that their code is in the classpath?
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic