Solved

Detecting launch method/operating environment?

Posted on 2009-06-30
6
298 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Alipay 10 94
Java array 21 149
Strange router problem - can't access hotmail.com 14 108
How does initial implicit objects  are set up in JSP? 7 40
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

732 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