Solved

Detecting launch method/operating environment?

Posted on 2009-06-30
6
296 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
ID: 24751641
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
ID: 24751679
"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
ID: 24751711
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
ID: 24751791
@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
ID: 24751828
@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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

777 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