• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!