Java console (program)

I was wondering how to make a Java console program. I use Eclipse as IDE. Looking for something similar to C#’s version of a console program.

Answer 1;

/*
 * put this in a file named CommandLineExample.java
 *
 */

class CommandLineExample
{
    public static void main ( String [] arguments )
    {
        System.out.println("Hello, world");
    }
}

type the following from the command line to compile it:

$ javac CommandLineExample.java

then you can run it from the command line like this:

$ java CommandLineExample

Answer 2; How to make a console application in Eclipse.

  1. File – New – Project
  2. Select “Java Project”
  3. Name it “MyApp”
  4. Right click on MyApp/src
  5. New – Class
    • package name, like “com.sample”
    • Class name “MainClass”
    • check box “public static void main()”
  6. add this line System.out.println("hello"); to main()
  7. Right click “MyApp”, Run As – Java Application

 

Answer 3;

Click eclipse.exe file (Windows) to start the IDE. You will see the splash screen of EcliNeo:Eclipse Neo splash

That’s it! You have successfully installed Eclipse IDE. Next, let’s see how to create a workspace.

2. Choose a Workspace Directory

Eclipse organizes projects by workspaces. A workspace is a group of related projects and it is actually a directory on your computer. That’s why when you start Eclipse, it asks to choose a workspace location like this:Choose workspaceBy default, Eclipse created a workspace directory at your USER_HOME\workspace. If you want to choose another directory, click Browse. Here I chose a different workspace:Choose different workspace

Check Use this as the default and do not ask again if you don’t want to be asked whenever you start Eclipse. You can always change workspace when Eclipse is running.Click OK. You should see the welcome screen:Eclipse welcome screenNow, we are ready to create a Java project.

3. Change Perspective

Before creating a new Java project, let familiarize yourself with Perspective. Imagine a perspective is a predefined configuration specialized for a specific aspect of development process such as Java, Java EE, Debug, Database Development, Web, etc. Depending on your need, you can switch back and forth among different perspectives during a development session.Since we installed Eclipse IDE for Java EE Developers, the default perspective is Java EE. To change perspective, go to Window > Perspective > Open Perspective > Other… You will see a small dialog listing all available perspectives:Eclipse Perspectives

Here we choose Java perspective. Click OK. Here’s how the Java perspective would look like:Java perspective

4. Create a Java Project

To create a new Java project in Eclipse, go to File > New > Java Project. The New Java Project wizard dialog appears let you specify configurations for the project:New Java Project Dialog

Enter project name: HelloWorld. Leave the rest as it is, and click Finish.You should see the HelloWorld project is created in the Package Explorer view as following:HelloWorld project

It’s recommended to create a package for your project. Right click on the project, and select New > Package from the context menu:Menu New Package

In the New Java Package dialog, enter the name your package. Here I enternet.codejava:New Java Package

Click Finish. You should see the newly created package appears:Newly created package

Now, it’s time to create a Java class for your hello world application.

5. Write Your First Java Program

To create a new Java class under a specified package, right click on the package and select New > Class from the context menu:Menu New Class

The New Java Class dialog appears, type the name of class as HelloWorld and choose the option to generate the main() method:New Java Class

And click Finish. The HelloWorld class is generated like this:New Class Generated Code

Now, type some code in the main() method to print the message “Hello World” to the console:HelloWorld Class Code

That’s it. We have created a Java hello world program using Eclipse IDE.

6. Compile, Build and Run Your First Java Program

By default, Eclipse compiles the code automatically as you type. And it will report compile errors in the Problems view at the bottom like this:Problem compile error

If you want to disable automatically build feature, click the menu Project and uncheckBuild

Automatically:Menu Project Build

However, it’s strongly recommended to keep the auto build mode for it helps you detect errors instantly.Now, let’s run the hello world application. Click menu Run > Run (or press Ctrl + F11), Eclipse will execute the application and show the output in the Console view:Run Hello World ProgramThat’s it! The HelloWorld program has run and printed the output “Hello World” and terminates.We hope this tutorial help you get started with Eclipse and Java easily. Thank you for reading.Watch this tutorial in video:

Share this article:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s