Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

How to call a program from within a program?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the suggestions, they were all great.
 
this is supposed to be a surprise, but it smells like a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic