tags:

views:

395

answers:

5

Since I've been learning Java, I've been able to create GUI programs. However, even simple "Hello World!" programs will not work, if they require a console. My research on the topic has lead me to the conclusion that my Java Runtime Environment does not have a console associated with it.

Is it possible to call the Win32 "AllocConsole" method somehow, like is possible in C#?

Edit:

I mean the console like you are describing, a console just like C# programs can have. I forgot to mention that I'm using the Eclipse IDE. From what I've discovered Eclipse runs a program using "javaw.exe" instead of "java.exe" which causes the program not to have a console. Also, the windows "command prompt" is a program that uses the type of console I mean.

Thanks for any answers.

Euphoria83 is on the right track as to what I am trying to do. By the way his method works, but I want the console to automatically appear when I click on the ".jar" file of a program I create. Basically, I need the console to act like it does in C# at runtime. Also, Eclipse does have a console window (I think they call it the console perspective, but I am used to using Visual Studio so I am not exactly sure) which will display the program output inside Eclipse only. I want the console to work at runtime, not just inside the integrated development environment.

+2  A: 

What exactly do you mean by console?

Typing the regular

System.out.println("Hello world!");

On a simple Java program and running it from command line should give you the results you want. Unless your definition of console is different in which case you'll need to explain yourself better.

Yuval A
A: 

As Yuval said, System.out.println("...") prints your text to a console window.

As a little explanation, System.out is the "standard" output stream which is automatically provided by the System class (you may know something like that from C or C++).

You may create your own output stream (there are several different kinds of output stream classes, e.g. to write to a file or to a string) and use the print() and println() methods of these as well.

Ridcully
A: 

Are you getting an error message while executing the code or are you simply not able to see a result even if your code executes successfully ?

It seems that you are using an IDE. It is possible that the output is being directed to a console window that is invisible yet. You have to make it visible by selecting from the menus.

Try this:

Save the following in a text file called "Test.java"

public class Test {
     public static void main(String[] args) {
          System.out.println("This is a print from my code") ;
     }
}

Then open the console by typing "cmd" at the run prompt (which you can choose in the Start Menu). Go to the folder where you have stored Test.java and type this.

> javac Test.java

followed by

> java Test

If you see "This is a print from my code" being printed, then your code is producing output to the console.

euphoria83
+3  A: 

Either associate 'java.exe' with the .jar extension (by default it will be 'javaw.exe', which has no console), in which case you'll get a console with every jar, or create a shortcut/.exe in the language of your choice/batch script to run 'java -jar myjar.jar' when you click on it.

Pete Kirkham
The latter is the usual way to do this in Java - I usually create a batch and shell script for command line programs - you can never tell how your users environment is set up, and you wouldn't want to force users to associate java.exe with jars rather than javaw...
Ant
Yes, but the OP is talking about fixing the machine s/he's developing on, rather than distributing anything for users (where I usually either use a shortcut, or a .exe stub, which looks in the registry to find where the runtime is).
Pete Kirkham
A: 

Rather than muck around with file associations, which is one way of changing the default, you could create a batch file jarconsole.bat in your C:\Documents and Settings\User\SendTo directory:

@ECHO OFF
java -jar %1
PAUSE

You can then launch a jar by right-clicking and choosing Send To > jarconsole.bat.

See also this question on working with certain console features from Eclipse.

McDowell