• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling Java from Java

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to launch a 3rd party Java command line tool from within my program.

It would seem that my choices are to call its main method or use Runtime.exec()

Is there any advantage to using one over the other?

Thanks,

Drew
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call its main method, then it's in the same process. That means if it misbehaves, then it will bring down your program too. If it calls System.exit(), then your program will exit too. If it ties up the GUI thread, then your GUI will be unresponsive, too.

So unless this program was designed to be used via an in-process from another program, it's probably smarter to use Runtime.exec().
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thanks Ernest.

I still haven't decided if my program is going to be a standalone Java app or a web application.

Will I still be better off using Runtime.exec() to launch the command line app if I use a Servlet? Any reason that won't work?

Drew
[ May 21, 2006: Message edited by: Drew Lane ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Drew, a web application and a standalone Java application work completely differently.

A web application (of which a servlet would be a part) runs on the server. If you would start a 3rd party application from a servlet, the 3rd party application will be running on the server and not on the client. Is that what you want?

If you write a standalone Java application, it's most likely going to run on a client computer. If you start a 3rd party application, it will be running on the same client computer.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand.

I was just trying to figure out if there are any special considerations I should be aware of if I were to call Runtime.exec() from a Servlet.

For example, what kind of memory issues might I run into when starting a new process from a servlet? Is the container going to have security hissy-fit calling Runtime.exec()? If I don't use Runtime.exec() and just call the main method of the command line program, will the container shut down if the app calls System.exit(), etc.

Maybe I should ask this in the servlets forum instead.

Drew
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to look at third third-party app and see if you can duplicate some of its main() methods functionality by using the other classes in the app directly; if it's well-factored code, this would make it far easier to fit it into your own software without some of the same concerns.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For example, what kind of memory issues might I run into when starting a new process from a servlet? Is the container going to have security hissy-fit calling Runtime.exec()? If I don't use Runtime.exec() and just call the main method of the command line program, will the container shut down if the app calls System.exit(), etc.


Probably starting a process from a servlet using Runtime.exec() won't upset the application server, but you must ofcourse take care that if there are 100 users using your application, they don't start up the process at the same time.

If you just call the main() method of the command line program and it does System.exit(), you're very likely to get problems. I've seen stupid third-party libraries which had a System.exit() in them somewhere, and using them from a web app just killed the whole application server...!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic