• 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

How to execute command line or open an external application without return values from Java?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody, this question seems to be common on the Internet, but I am in a situation that I still have not found a response.
I'm developing a Java application for Windows environment that allows the user to open programs that are not directly related to this application. i. e., part of my GUI is used as generic shortcuts repository. This actually increases operator productivity!

What do I need:
A simple Java command to open external programs, with or without parameters on the command line, WITHOUT capture the return values. i. e., I want to open general applications without worrying about what they do or what they are for, return values will never be used for anything and I do not want to treat them. This in Delphi is very easy to do (ShellExecute or WinExec), but Java is becoming an odyssey!

What I've tried:
Runtime.getRuntime().exec() -. Works, but hangs the applications after some time of use because of the buffer that is filled with the return values. There are Java documentation and forums around the world with ways to solve this problem, however, in my application the return values are never used and I do not want to treat them. Also because, many external applications gonna be opened and will be opened several times a day, so it is not appropriate to create numerous threads only to treat return useless values.

ProcessBuilder() - similar to the previous one, but I could not open more than one application. It must also treat the return values.

Desktop.open(file) - opens only files registered in Windows, do not open programs and does not accept command line with parameters.

Any tips or any command that I have not tried?

Thank you in advance
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gustavo Hennemann wrote:
Runtime.getRuntime().exec() -. Works, but hangs the applications after some time of use because of the buffer that is filled with the return values.




"Return values" are the stdout and stderr output?
If yes, could you exec the thing in a modified environment: with stdout and stderr kind of redirected to a data sink (like /dev/null in Un*x) to ignore those?
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code I use to start an external application that remains active after I close the Java is illustrated by this



Class SyncPipe is a Runnable that just copies the InputStream to the OutputStream; should be very easy for you to write.

Using 'start with cmd.exe' in effect decouples the firefox from the Java program and the stdout and stderr output from the Process is that from cmd.exe and not firefox. You MUST MUST MUST read the Process stdout and stderr stream or you risk a deadlock. You should read the Process return code .

The only program I cannot start and remain running using this method is Notepad ! For that I have to do something significantly more complex.

Don't even think about not purging the Process stdout and stder. You might get away without the purge for a bit but in the end you will just get grief. You don't have to do anything with the stdout and stderr output though it is advisable to dump it somewhere to aid in debugging.

Note - Runtime.exec() is just a wrapper round Process Builder .

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More details about emptying Streams in the classic article, which might be 14 years old but is still worth reading.
Search for
When Runtime.exec() won't Michael Daconta.
Don't even think about using Runtime.exec or similar until you understand that article.
 
Gustavo Hennemann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
As pointed by Ivan, the return values are the stdout and stderr!

About the solution, I liked the tip indicated by Ivan, redirecting the stdout and stderr, so, I got 2 solutions that works well:

Solution 1 - redirection by the SO in command line (pseudo code):
or


Solution 2 - using the features provided by ProcessBuilder(), most modern:


Thanks again!!
 
Gustavo Hennemann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard Tookey,
I understood what you mean about purging the Process stdout and stderr, but, how about the redirecting this outputs to null devices or to null files? What do you think?
 
Gustavo Hennemann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell Ritchie,
I'm already familiar with the article "When Runtime.exec() won't".
Thanks
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gustavo Hennemann wrote:I understood what you mean about purging the Process stdout and stderr, but, how about the redirecting this outputs to null devices or to null files? What do you think?


Do you need them? Are you ever likely to? - for example: if the command fails, and you don't know why?

My policy from my sysadmin scripting days: NEVER direct syserr to a sink. But whether it holds in this case, I have absolutely no idea.

Winston
 
Gustavo Hennemann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Gustavo Hennemann wrote:I understood what you mean about purging the Process stdout and stderr, but, how about the redirecting this outputs to null devices or to null files? What do you think?


Do you need them? Are you ever likely to? - for example: if the command fails, and you don't know why?

My policy from my sysadmin scripting days: NEVER direct syserr to a sink. But whether it holds in this case, I have absolutely no idea.

Winston



Perfect!
In my case, I'm opening general programs, like calc, text editor, spread sheet editor, calendar, etc. Just to be the life easyer. If these applications fails, I don't care, there is no interaction between them and my program, is not a big problem.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gustavo Hennemann wrote:If these applications fails, I don't care, there is no interaction between them and my program, is not a big problem..


You might not, but somebody might. If this is just for yourself, you can decide; but if you work for a company, and things start going wrong and YOU were the one who "decided" that error messages weren't worth saving, you might find yourself in a very warm pot.

Winston
 
Ranch Hand
Posts: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In meantime, Congrats Campbell for your 40k post..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Gustavo Hennemann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congrats Campbell Ritchie, this is a very respectful mark!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only 9978 to go and thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic