Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Is it possible to determine if a Java application was invoked from command-line?

Thread view: 
Lionel - 09 Dec 2007 08:30 GMT
As the title asks I am wondering if I can determine if my application
has been invoked from the commandline.

Alternatively, how might I test if the machine I am running on has
graphical capabilities?

Thanks

Lionel.
Andrew Thompson - 09 Dec 2007 09:15 GMT
> As the title asks I am wondering if I can determine if my application
> has been invoked from the commandline.

Why?

> Alternatively, how might I test if the machine I am running on has
> graphical capabilities?

<http://java.sun.com/javase/6/docs/api/java/awt/
GraphicsEnvironment.html#isHeadless()>

--
Andrew T.
PhySci.org
Lionel - 09 Dec 2007 10:59 GMT
>> As the title asks I am wondering if I can determine if my application
>> has been invoked from the commandline.
>
> Why?

Because.

>> Alternatively, how might I test if the machine I am running on has
>> graphical capabilities?
>
> <http://java.sun.com/javase/6/docs/api/java/awt/
> GraphicsEnvironment.html#isHeadless()>

Thanks. Now I know what headless means :). It all makes sense now!

Lionel.
Lew - 09 Dec 2007 16:40 GMT
>>> As the title asks I am wondering if I can determine if my application
>>> has been invoked from the commandline.
>>
>> Why?
>
> Because.

No, really, Lionel, why?

How can you expect help when you are rude about answering questions yourself?

Signature

Lew

Lionel van den Berg - 11 Dec 2007 08:25 GMT
>>>> As the title asks I am wondering if I can determine if my application
>>>> has been invoked from the commandline.
[quoted text clipped - 7 lines]
> How can you expect help when you are rude about answering questions
> yourself?

I wasn't intending to be rude, it was tongue in cheek. The reason for me
wanting to do it was irrelevant. Perhaps I should have added one of
these: :)
Andrew Thompson - 11 Dec 2007 09:13 GMT
>>>>> As the title asks I am wondering if I can determine if my application
>>>>> has been invoked from the commandline.
...
>> How can you expect help when you are rude about answering questions
>> yourself?
>
>I wasn't intending to be rude, it was tongue in cheek. The reason for me
>wanting to do it was irrelevant.

Perhaps not to a help-desk (though they might be able
to provide a better answer, cheaper, knowing the
ultimate goal - as opposed to the strategy the questioner
thinks might achieve the goal), but this is a discussion
forum, so it is every bit as relevant and open for
discussion as the original question.  OTOH, should
you choose the 'help-desk' in future, don't forget..

>..Perhaps I should have added one of these: :)

..lots of these: $

Signature

Andrew Thompson
http://www.physci.org/

Lionel - 11 Dec 2007 12:12 GMT
>>> How can you expect help when you are rude about answering questions
>>> yourself?
[quoted text clipped - 8 lines]
> discussion as the original question.  OTOH, should
> you choose the 'help-desk' in future, don't forget..

Well, normally I do add a "I'm trying to achieve foo", but this really
was a straight forward question and I was trying to spare the
reader/helper from excess un-needed information. To the point and
concise so as to not waste time.

Now that we got this far I may as well say why :). A small app that I'm
writing is to run on a server. It can either be a double-clicked jar, or
it can be invoked from the command-line. Providing the argument "-noui"
on the command-line forces user interaction via the prompt. In graphical
mode there is a single dialog with fields and buttons.

Since many servers may not run a graphical server I wanted to make sure
that in such cases I handle the problem correctly. In such cases the
user obviously won't be double clicking the jar file, so a quick check
of isHeadless() will allow me to force the command-line user-interaction
as though the argument "-noui" was provided.

>> ..Perhaps I should have added one of these: :)
>
> .lots of these: $

lol, I'll give you 0% of those and 200% of these :):).
Piet Blok - 11 Dec 2007 13:41 GMT
> Now that we got this far I may as well say why :). A small app that
> I'm writing is to run on a server. It can either be a double-clicked
> jar, or it can be invoked from the command-line. Providing the
> argument "-noui" on the command-line forces user interaction via the
> prompt. In graphical mode there is a single dialog with fields and
> buttons.  :):).

Well, if that is the case, you may try System.console(). As far as I have
tested, it returns a non null object only when started from the console
(with java.exe) and null for other invocations.

(Sorry if this was mentioned previously, but I can't reread the history of
this thread)

Piet.
voorth - 11 Dec 2007 14:50 GMT
> Well, if that is the case, you may try System.console(). As far as I have
> tested, it returns a non null object only when started from the console
> (with java.exe) and null for other invocations.

Since this is a new Java 6 feature, this might not be available to
everyone.

Henk
RedGrittyBrick - 11 Dec 2007 14:49 GMT
>> Perhaps not to a help-desk (though they might be able to provide a
>> better answer, cheaper, knowing the ultimate goal - as opposed to the
[quoted text clipped - 8 lines]
> reader/helper from excess un-needed information. To the point and
> concise so as to not waste time.

One reason for stating your goal is that people like me are often
interested in solutions to such goals. I'd probably not be able to
benefit so much from the proferred solutions if the goal is hidden. For
me it is a question of recognising a similar situation when faced with
it and recalling prior discussion here (or being able to find it using a
Google Groups search).

Which I suppose is just repeating that most of us treat this as a
discussion group for the benefit of all, rather than a help desk for
only the benefit of the OPs individually.
Lionel van den Berg - 12 Dec 2007 08:49 GMT
> Which I suppose is just repeating that most of us treat this as a
> discussion group for the benefit of all, rather than a help desk for
> only the benefit of the OPs individually.

I assure you, I do occasionally make a point of reading through
unanswered messages to try to help others. But very often there is
someone else with a better answer so I restrict my answers to cases in
which I know very well.
Lew - 12 Dec 2007 15:26 GMT
>> Which I suppose is just repeating that most of us treat this as a
>> discussion group for the benefit of all, rather than a help desk for
[quoted text clipped - 4 lines]
> someone else with a better answer so I restrict my answers to cases in
> which I know very well.

RGB's point was about answering questions about *your* problem, not helping
others with theirs.

Signature

Lew

Andrew Thompson - 11 Dec 2007 14:49 GMT
...
(snip tech. answer to 'problem spec.')

Thanks for the technical clarification.  I suspected it
was something along those lines, but it is interesting
to have that confirmed.

>>> ..Perhaps I should have added one of these: :)
>>
>> .lots of these: $
>
>lol, I'll give you 0% of those and 200% of these :):).

.. I think we've reached understanding.  ;-)

Signature

Andrew Thompson
http://www.physci.org/

Martin Gregorie - 11 Dec 2007 18:54 GMT
> Now that we got this far I may as well say why :). A small app that I'm
> writing is to run on a server. It can either be a double-clicked jar, or
> it can be invoked from the command-line. Providing the argument "-noui"
> on the command-line forces user interaction via the prompt. In graphical
> mode there is a single dialog with fields and buttons.

You may be able to spiff it up a bit past the simple Q&A prompt
dialogue. Take a look at the Jcurses package. Its on Sourceforge. I
haven't used it, but I did look at its documentation. It seems to be
designed to let a Java application present a curses-type screen
interface when a graphical interface, e.g. Xterm, isn't available.

I didn't do more than read its docs and save a reference to it because
it wouldn't do what I needed, which was to bang a cursor round in a 24 x
80 JPanel so I could emulate a terminal as part of a Swing graphical
display.

I don't have guaranteed a solution to autodetecting a graphical
environment, but would reading the terminal type using
System.getenv("TERM") tell you anything useful?

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Stefan Ram - 12 Dec 2007 17:39 GMT
>As the title asks I am wondering if I can determine if my application
>has been invoked from the commandline.

 Possible the behavior of java.lang.System.in.available() might
 differ. When a console is available, this seems to be a number
 larger than -1.

 Without a console, the behavior might differ. It seems as if
 an exception is raised sometimes. However, I had trouble
 finding the details, but possibly you can find them yourself.
Roedy Green - 13 Dec 2007 21:58 GMT
>Alternatively, how might I test if the machine I am running on has
>graphical capabilities?

see http://mindprod.com/jgloss/headless.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lionel van den Berg - 14 Dec 2007 08:19 GMT
>> Alternatively, how might I test if the machine I am running on has
>> graphical capabilities?
>
> see http://mindprod.com/jgloss/headless.html

Better late than never. We came to this conclusion anyway, but I'm only
responding because the wording/grammar on your link could be
improved/fixed a bit:

"When Java is implemented on a very small device mouse or AWT graphics
support may not be available."

You might want a comma between device and mouse, or even restructure the
sentence completely. At first I read it as java being implemented on a
small device OR java being implemented on a mouse OR java being
implemented on AWT graphics support - huh. I had to read twice more.

"You code cane determine if keyboard, mouse and screen are availble with:"

I probably don't need to say anything about the above but I will:

"You" should be "Your"
"cane" should be "can"
"availble' should be "available".

Hope this helps.

Lionel.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.