Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I Call A Seperate Program Inside Another?

 
Nick Rowe
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic