Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Detecting launch method/operating environment?

Posted on 2009-06-30
6
Medium Priority
?
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

715 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