File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to call a program from within a program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to call a program from within a program?" Watch "How to call a program from within a program?" New topic
Author

How to call a program from within a program?

zoe goddard
Ranch Hand

Joined: Dec 12, 2000
Posts: 74
Hi I have a command line java program I can call and it prints information out. ( I can run it with : java program infile1 infile2) I'd like to create another program that can call this first program. How could this be done?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, you could take the program that you have now. Take the stuff that you have in the main() method and put it in a constructor that takes the infiles as parameters.
Write your second class and have it create an object of the the first class using those two parameters.
But something tells me that there are some design issues that you have not considered. Actually, the question sounds sort of "procedural".
Want to explain more of what you are trying to accomplish?


"JavaRanch, where the deer and the Certified play" - David O'Meara
zoe goddard
Ranch Hand

Joined: Dec 12, 2000
Posts: 74
These classes were given to me, so I'd prefer to use them as is and just do up a gui to visualize the results. I prefer not to change code that works. I tried the making a new method. That worked. I also tried.
Program program = new Program();
program.main( args );
and this worked. Is this a bad thing.
khella smith
Ranch Hand

Joined: May 06, 2002
Posts: 39
hi
from what i've read i dont think its a problem as long as it works its fine u don't need to write it in a special way,i.e if u can call the other program from within then its fine.
khella smith
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi zoe,
all the suggestions till now r valid but what if you can't combine two codes??? say u have two different programs in a obfuscated form. then what u'll do??
i would prefer using Runtime.exec(String[]) method that creates another Process in the program and we can get results by getting InputStream from the Process object and do the stuff...we can even pipe the input to the program using OutputStream of the Process object.
See, java.lang.Runtime, java.lang.Process classes...
regards,
maulin
zoe goddard
Ranch Hand

Joined: Dec 12, 2000
Posts: 74
Thanks everyone for the suggestions, they were all great.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call a program from within a program?