Solved

Detecting launch method/operating environment?

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

636 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