Solved

Detecting launch method/operating environment?

Posted on 2009-06-30
6
294 Views
Last Modified: 2013-11-23
Hello all!

Just wondering how to detect the method used to launch a java application (not applet) -- or just a way to detect if I'm running in a GUI or terminal environment.

basically if it's launched from the GUI, i want to display the GUI

if the app is launched from the command line/terminal i want to run in terminal mode if no flags are passed "myApp", or launch the GUI if a flag is passed "myApp -gui"

so far the only real solution i've found would be to use the args array and check to see if the user specifies that they want to run it from the command line .... from the command line "maApp -terminal" ... i consider this solution, at most, a hack around my ignorance and would prefer something a little more passive / auto-magical if possible.

Thanks!
0
Comment
Question by:zabman
  • 3
6 Comments
 
LVL 17

Expert Comment

by:Thomas4019
Comment Utility
What do you mean by if your "program is launched form the GUI"? By this do you mean whether someone runs your program by executing a batch or clicking on a shortcut.

If this is the case, make the batch or shortcut file always send the "gui" command line flag. Then make your program always default to the terminal mode.
0
 

Author Comment

by:zabman
Comment Utility
"Launched from the GUI" being if the user just double-clicked on the JAR file.
0
 
LVL 6

Accepted Solution

by:
jwenting earned 125 total points
Comment Utility
Detection of whether or not you're running in a graphical environment is easy. The Java language provides a method for that.
Call GraphicsEnvironment.isHeadless().
If it returns true, you're not running in a graphical environment.

Detection of how the JVM was launched is impossible and should not be needed anyway. All you really care about is that it launched and that you're now running :)
0
 

Author Comment

by:zabman
Comment Utility
@jwenting

Thanks for the idea, however, i ran into a limitation when using GraphicsEnvironment.isHeadless(); in earlier tests.

If the user is in a terminal within a GUI environment, (not headless) it still reports (correctly) that I'm in a GUI environment -- even though it was launched from a command line.

Hence the post :-D

As can be expected, the inverse of that doesn't work either (GraphicsEnvironment.getLocalGraphicsEnvironment();)

So i guess a more succinct question is:  How do i detect when the user is launching the program from a command line/terminal when within a windowed environment.
0
 

Author Comment

by:zabman
Comment Utility
@jwenting:

I also like asking for the impossible -- if no one has a clever work-around, i've no real problem reverting to a flag/.isHeadless alternative .... but it's hard to learn anything if you don't push the boundaries of possibility every now and again. ;-)

... as for that type of detection not being needed ... well, i will admit it's not *absolutely* needed, however, i hope i can get you to agree that it would be a convenience feature -- and one that i care about so i can present the user with a contextually appropriate interface ;-D

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now