File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Can I Call A Seperate Program Inside Another? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I Call A Seperate Program Inside Another?" Watch "Can I Call A Seperate Program Inside Another?" New topic

Can I Call A Seperate Program Inside Another?

Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Ok So I Have two programs, One program to write to a new document a list of variable values I have captured. And another to do something similar with another set of values.

I realise that I can simply call two seperate programs within the command prompt window but is there a way by writing a method and stipulating a path (like a css regeference to html) where I can call the second program.

I'd rather do this than add the class from one program to the other and then fiddle around with it.
This may sound silly to some but is it possible?

regards S
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

You can load the class and call its static main method. If you know the class at compile time it's simply a matter of calling If you don't know the class at compile time you can use reflection to execute the main method:

How To Ask Questions How To Answer Questions
David Newton

Joined: Sep 29, 2008
Posts: 12617

Note that this may not really be what you want to do.

Depending on how things are written and used it might be just as easy to pipe the output of one program (instead of writing it directly to a file as you do now) into the input of another. This is *much* more versatile than writing to a file, because you can still get the file output by redirecting to a file. So instead of:

$ foo outputFileName

you'd enter:

$ foo > outputFileName

This lets you do things like:

$ foo | bar > outputModifiedByBarFileName

I don't see any advantage of calling one program from within another: you've already introduced a dependency, so you might as well do it right and just have its classes on the classpath and do it all using normal Java, with the caveat that your code as it stands is not really set up for any sort of re-use or extensibility.
I agree. Here's the link:
subject: Can I Call A Seperate Program Inside Another?
It's not a secret anymore!