tags:

views:

309

answers:

13

Just downloading eclipse (finally!), and curious if Java has the concept of a console application like c# so I can quickly test my skills out etc.

+2  A: 

Sure. Just run the following in your console:

java -cp {classpath} {main class}

and that will write to stdout/err and read from stdin. See the documentation for System (in particular System.out, System.err, System.in)

Brian Agnew
Well, he's using Eclipse so he can just click the green play button if he wants ;)
Kristopher Ives
Point taken. Clarified. Thanks.
Brian Agnew
A: 

Of course. Use System.out and System.err to write to stdout and stderr respectively.

Matt Greer
A: 

yes java does have the "concept of console apps".

If you start any java book, and write code, compile and run it without the use of an IDE, you will thoroughly understand how it works.

PS: still laughing at "concept of console app"! (almost) all (popular) (programming) languages have that!

Here Be Wolves
I thought logo programs were written on a console.. don't remember rightly.. i used to play with logo in 6th grade.. but I edited my answer anyway :)
Here Be Wolves
PPS: Not the Javascript/Flash/Flex languages of the web world.
Michael Brewer-Davis
A: 

yes, abosulutely, and Eclipse will provide one for you automagically when you invoke Run from the eclipse menu.

GregS
A: 

Just create a new class in Eclipse

public class MyTestClass{

  //hint type "main" in eclipse to get autocompletion
  public static void main(String[] args){
     System.out.println("Hello world!"); //hint: type sysout as a shortcut in Eclipse

  }

}

Have fun

Juri
+6  A: 

Any Java application can write to the console using System.out, but where the output goes depends on how it's invoked.

If you invoke the application using a gui launcher (like javaw.exe) then console output is lost. If you run it from the terminal, you can see it. It can also be directed somewhere else, like a file, using operating-system specific techiques (such as shell redirect)

MarkR
+3  A: 

Yes. Here's a tutorial on how to create one.

Eric J.
A: 

There's not a "New Console Project" or similar in Eclipse, but you'd just make a new Java project and read/write to System.out/System.in/System.err and not use a GUI framweork such as Swing/AWT to be a console application.

nos
+2  A: 

I'm not sure what the console is in the C# world but, with Java, you can output things to the "standard" output using System.out. Quoting its javadoc:

The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.

UPDATE: Having googled a bit about C# console application (and read this page), my understanding is that Java doesn't have a real equivalent of this, you won't have the same control than with C#.

However, for quick testing, I'd suggest to check Eclipse Scrapbook pages that allow to execute Java expressions without having to create a new Java program.

Pascal Thivent
+1 for the scrapbook pages
Bozho
Scrapbook pages are really undervalued as stated in the link I provided. But they are nice.
Pascal Thivent
If I recall correctly the Console applications under Windows, can open their own cmd.exe window if they are not run from a command line.
Thorbjørn Ravn Andersen
@Thorbjørn Ohhh, ok, I see. Thanks for this clarification.
Pascal Thivent
+1  A: 

Yes below is an example that prints your first argument.

import java.io.*;

    class ConsoleApp {
        public static void main(String args[]){
            if (args.length > 1){
               System.out.println("First Arg:"+args[0]);
            }
        }
    }

Potentially any class file with a public static void main method can be a console application with parameters passed from with Eclipse or from the command line. Within Eclipse right click on the file, select Run As -> Run Configurations -> Use the Arguments tab to pass in your arguments to the class, Hit Run and have fun :-).

VDev
+2  A: 

It really depends what you mean by a "console app."

A console is simply another program your code interfaces with. To be called a console, it has to provide a way for your program to output text to the user and to read in text from the user (System.in, .out). But consoles (especially on Unix) can do a lot more. In a typical Linux console, you can:

  • read in passwords without displaying (echoing) what the user types in
  • move the cursor around like a typewriter
  • clear the screen
  • change text colors (and maybe the font)
  • bind actions to function keys (F1-12, CTRL+x, ALT+x)
  • get (and sometimes set) the dimensions of the console
  • etc

Consoles tend to be system-specific. Since Java is platform independent, it only provides the lowest common denominator of capabilities (which isn't very much).

Tac-Tics
It should be noted that Java 6 has Console.readPassword, which does not echo the password.
dmeister
+2  A: 

Java does not have the concept of a Console application as such where a CMD.EXE window is created if the application is started by double clicking [1]

It can, however, run inside an existing CMD.EXE session with a "java -jar foobar.jar" command or similar where text written to System.out is displayed.

[1] Latest Java 6 allows shortcuts with Java Web Starts which can do something close but then you have all the security precautions meaning it is not just a simple application.

Thorbjørn Ravn Andersen
A: 

It's not strictly speaking 'only Java', but Groovy has a nice console from which you can easily try and play with Java libraries. You can even try it out on the web here.

There are probably other dynamic languages that run on the jvm that allows you to do the same.

HeDinges