• 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

Calling one Java Prog from another - Code

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I wrote a Hello World Java program and tried to invoke it from another program.
public class RunExample
{
public static void main(String []args)
{
Runtime rtime = Runtime.getRuntime();
try
{
rtime.exec("java HelloWorld");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The program compiles fine. The path and class path are set properly. But when i execute it, the second program (i.e. HelloWorld) does not print the output -- HelloWorld on the screen.
What could be wrong?
Madhesh

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
change like this
import java.io.*
_______________
Process p=rtime.exec("java HelloWorld");
use p.getInputStream() method
read from InputStream till it is empty
Print it on to the System.out with the help of OutputStream
____________________________
hope this is what you wanted
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or alternatively run it directly in the same VM as your other program:

Is this what you want?
 
madhesh raj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank,
Thanks for the suggestion. I could not get what the new String[0] does. Is it to pass the arguments to the called program? (in this case HelloWorld)?
IF not, Supposing i want to pass some arguments to the second program what do i need to do? Can i just form my own String array of arguments and pass it.
Regards
Madhesh
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main() method expects a String[] argument of some sort. The new String[0] creates a zero-length array of Strings - it's a way of saying "here's the list of arguments - there aren't any". Alternately you could pass null instead - as long as HelloWorld.main() does not attempt to use the args variable, there's no problem.
If you do want to pass arguments to HelloWorld.main(), then yes, you would need to create a non-empty String array to hold the arguments. E.g.
<code><pre> String[] myArgs = {"first arg", "second arg"};
HelloWorld.main(myArgs);
</pre></code>
 
madhesh raj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Does reading from the process's InputStream work the other way round also. That is, suppose the process is expecting some keyboard input from user then can i map it using the the process's OutputStream?
Pls give a sample code. It would be really useful
Madhesh
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic