Console input and output in Java - How to generate executable jar

Often, programs need to communicate with a user. Users are the people who interact with programs.

This communication has two directions; input and output:

Let's see how to do this in a command line application.

Console Output

An easy way to create a new class with Eclipse is right-click on src - New - Class

Salida por la consola

Name the class with "ConsoleOutput" and select the main method. Click finish.

Remove the TODO comment in the main method and write "syso", then press the Ctrl key+the space bar. If everything went well, "syso" became "System.out.println ()". Ctrl-space gives help on many occasions.

package com.edu4java.javatutorials;

public class ConsoleOutput {
	public static void main(String[] args) {
		System.out.println("Hello World");
	}
}

The above code prints Hello World to the console. Right-click on the class - Run As - Java Application and we will be able to see "Hello World" in Console View.

Console Input

Just as we print on the console, you can also enter data through the console.

Create a class called ConsoleInput with a main method in the same way as before. Replace the comment with the following code;

		InputStream stream = System.in;
		Scanner scanner = new Scanner(stream);
		System.out.println("Name:");
		String input = scanner.next();
		System.out.println("Hello " + input);
		scanner.close();

We will see errors marked in red because there are missing imports:

import java.io.InputStream;
import java.util.Scanner;

If we press Ctrl + Shift and the letter "o", imports will be added automatically. If Eclipse has access to more than one class with the same name, it will give you the option to choose the package for import.

Code explanation

InputStream stream = System.in;

In the first line we store in a variable called stream, an object maintained by Java. This variable belongs to the class in of the System class.

This InputStream object, is like a stream of data coming from the keyboard.

Scanner scanner = new Scanner(stream);

In the second line we create a Scanner object, using as parameter a data stream. This is possible because Scanner has a constructor that accepts InputStream objects.

System.out.println("Name:");

Prints "Name" on the console.

String input = scanner.next();

This sentence locks the execution, waiting for a data input ended with a return key. The next () function, returns the string entered before the return key.

System.out.println("Hello " + input);

Prints "Hello", followed by anything we insert through the console.

scanner.close();

The Scanner object has a close () method to be informed when they are no longer needed. Normally, close () methods are used internally to release resources held by the object.

We can test the class right-clicking on the class; - Run As - Java Application. Click on the console to gain focus and write a name and click on the return key.

Create an executable jar with Eclipse

To test the MS-DOS console, we will generate a jar file that runs the ConsoleInput class. It is important to run before Run As - Java Application so that a launch configuration is created.

Right-click on the project - export - Java - Runnable JAR File

Exportar a archivo JAR ejecutable

In Launch configuration choose the class ConsoleInput. In Export destination choose where you want to place the jar file and click Finish.

To open a new MS-DOS window with cmd.exe, navigate to the directory where the jar file is and run it with java -jar console.jar

Entrada por la consola en Java

Main method arguments

Finally, let's create a class to test the parameters received by the main method. String [] args is a String array, where we locate the arguments added when we run a java class. In our example, the argument will be the name of the user, which will be stored in the first position of the array args [0] .

package com.edu4java.javatutorials;

public class ConsoleParameters {
	public static void main(String[] args) {
		System.out.println("Hello "+args[0]);
	}
}

We run the program with right-click on the class - Run As - Java Application and on the console we will see:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at com.edu4java.javatutorials.ConsoleParameters.main(ConsoleParameters.java:6)

We get an error because it it is trying to get to the first position of the array and Eclipse didn´t send any arguments.

Let's create the executable jar for ConsoleParameters. Do not forget to choose the class ConsoleParameters in the launch configuration. Execute java -jar console.jar edup. This sends edup as an argument that will be received as parameter in the first position of args. If everything is ok, the result should be:

Parametros por consola