1

This morning when I executed a simple hello world Java application from the command line on a Windows 7 machine, the response appeared below the command. Something like this:

java HelloWorld Hello world

Now the same command pops up a new window and immediately disappears. I put in a System.console().readLine("enter to continue"); and that appears as expected. Upon pressing enter the new window closes down.

How do I prevent this from happening? (1.7_21)

Additional clarity:

The code:

public class HelloWorld {
    public static void main() {
        System.out.println("Hello World");
    }
}

Steps taken:

  1. open a DOS window by Start/Run... cmd
  2. in the DOS window I cd to the proper folder
  3. in the DOS window I enter: java HelloWorld
  4. A new "java" window opens for about 1/10 of a second (The icon on the task bar is the Java icon.
  5. I issue the command: java -verbose HelloWorld
    The same window opens and I can see all the classes loading but then immediately closes
    If I select (very quickly) some of the text about loading classes, I can see, through task manager, that Java is running.
  6. issue the command: java
    I would expect to get "usage" back. The same window appears then disappears right-a-way.

My question is, "Why is the java executable opening this java window?" On other machines it puts it's output right in the same DOS window.

As another note Netbeans doesn't display the output in the output window either, in this case it appears to be totally lost. I can step through programs, but the output is not displayed in the output window.

P.S. I'm not a new-be, I've been running Java from the command line for years and this just started to happen. (and no I don't know what changed, I believe the machine received some sort of security patch from our corporate security staff. I'm unable to get from them what changed.)

Community
  • 1
  • 1
Dan477
  • 11
  • 1
  • 1
  • 3
  • 3
    We don't know what your program looks like, nor what you want it to achieve. It's hard to answer such a vague question. Please read http://tinyurl.com/so-hints – Jon Skeet May 29 '13 at 21:32
  • 3
    Jokes aside: when you run a command line executable in Windows and it finishes, that's pretty much what *should* happen. Easiest way to diagnose what really's happening is to open an actual command prompt window (Start->cmd) and run the command in that window – DigCamara May 29 '13 at 21:33
  • 1
    Try `-Djava.awt.headless=true -Djava.awt.headlessLib=true` at the command line? – fge May 29 '13 at 21:39
  • 1
    Do you have your JRE configured so it opens the Java console? You can configure this feature here: _Control Panel -> Java -> Advanced -> Java console_ – jahroy May 31 '13 at 03:15
  • 1
    (on rereading, @jahroy seems to have the right tack. I didn't know about the Java console). Another possibility is that for some reason your "java" command has become polluted with the "start" command. (The behavior you describe can be reproduced by writing "start java HelloWorld". Can you execute your java.exe verbosely? This would be: write out the whole path to your java.exe (for instance: c:\java_is_here\java.exe HelloWorld) – DigCamara May 31 '13 at 03:15
  • When this new Java window pops up, does your program still write "_Hello, world_" to standard out? I don't think you've definitively said whether or not it does. – jahroy May 31 '13 at 03:55
  • I would also like a solution to this problem. My java will write to STDOUT from the command line just fine. But if I run it from the "Run" menu of Notepad++ on windows, it opens in a Java window which closes too fast to see the output. Sure would be nice to have a way to force it to stay on screen until closed manually. – ziggurism Jul 23 '20 at 17:39

3 Answers3

1

When you run a command line executable in Windows and it finishes, that's pretty much what should happen. Easiest way to diagnose what really's happening is to open an actual command prompt window (Start->cmd) and run the command in that window .

DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • Maybe I was not clear. I'll clarify. I do run it from a DOS window. The program is a simple one class with a main method that has "System.out.println("Hello World"); You know a standard Hello World program. I open a DOS window. Then issue the DOS command from within the DOS window: java HelloWorld What I just noticed is the new window is not a DOS window but a Java window. The icon is a Java symbol on the window and on the task bar. (I also tried the -D... options suggested above - no help – Dan477 May 30 '13 at 01:40
  • @Dan477 - What you're describing sounds a little bit like the way the Java console works. I just added a comment under your question that shows how to check if that's what you're seeing. – jahroy May 31 '13 at 03:18
0

I started to experience the same problem after installing JDK 8.0.77. I am positive the new version has nothing to do with the problem. I just solved it by removing 'C:\ProgramData\Oracle\Java\javapath;' from the PATH environment variable. Hope this works for you too (although my answer might be too late). BTW, I have not experienced issues with other tools (Maven, Tomcat, Eclipse, etc.) I have and that depend on Java configuration.

0

try running by administrator, it works for me.

  • A simple Java application like this should work without admin rights. This is not an answer to the problem – slfan Aug 22 '19 at 04:31