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

L

Lionel

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.
 
L

Lionel van den Berg

Lew said:
No, really, Lionel, why?

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: :)
 
A

Andrew Thompson

Lionel said:
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: $
 
L

Lionel

Andrew said:
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..

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.
.lots of these: $

lol, I'll give you 0% of those and 200% of these :):).
 
P

Piet Blok

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.
 
R

RedGrittyBrick

Lionel said:
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.

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.
 
V

voorth

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
 
M

Martin Gregorie

Lionel said:
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?
 
L

Lionel van den Berg

RedGrittyBrick said:
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.
 
L

Lew

Lionel said:
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.

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

Stefan Ram

Lionel said:
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.
 
L

Lionel van den Berg

Roedy said:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top