Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a program from within a program?

 
zoe goddard
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
zoe goddard
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the suggestions, they were all great.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic