A replacement for java.io.Console with a System.in and System.out based fallback.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Marc Nause 1a8003ce0b chore(changelog): add entry for next release 1 week ago
badges chore(badges): remove superfluous file 1 month ago
config/checkstyle chore(checkstyle): remove rule 1 month ago
src docs(javadoc): improve comments 1 week ago
.gitignore chore(gitignore): add Eclipse files 5 months ago
CHANGELOG.md chore(changelog): add entry for next release 1 week ago
LICENSE Initial commit 3 years ago
README.md docs(readme): update version to 1.1.3 1 week ago
pom.xml chore: update version to 1.1.4-SNAPSHOT 1 week ago

README.md

Apache 2.0 License API Reference Issue Tracker Status: Active Distribution: Maven Central

Console with Fallback

Java 1.6 introduced the java.io.Console class which provides methods to access the character-based console device, if any, associated with the current Java virtual machine.

The class has many advantages over using System.out, but unfortunately using Console in an IDE leads to ugly NullPointerExceptions (see Stackoverflow for examples). This is the reason many developers avoid Console (if they even know about the class).

Console with Fallback provides a drop-in replacement for java.io.Console which falls back to System.out and System.in in case it is used in an environment where the system does not provide a native console. It was inspired by the blog article Java: System.console(), IDEs and testing.

Usage

The Java implementation of the Console class can easily be replaced by simply replacing the imports, and the way the object is created.

Example code with Java implementation:

package de.audioattack.example;

import java.io.Console;

public class Example {

    public static void main(String... args) {
        Console console = System.console();
        console.printf("Hello world!");
    }
}

Example code with Console with Fallback:

package de.audioattack.example;

import de.audioattack.io.Console;
import de.audioattack.io.ConsoleCreator;
import de.audioattack.io.ConsolePrintDecorator;

public class Example {

    public static void main(String... args) {
        Console console = ConsoleCreator.console();
        console.printf("Hello world!");

        // Use ConsolePrintDecorator for more convenient output methods.
        ConsolePrintDecorator printDecorator = new ConsolePrintDecorator(console);
        printDecorator.println("Hello world!");
    }
}

Releases

The latest release is available on Maven Central.

Maven dependency:

<dependency>
  <groupId>de.audioattack.io</groupId>
  <artifactId>console</artifactId>
  <version>1.1.3</version>
</dependency>

Gradle dependency:

implementation("de.audioattack.io:console:1.1.3")

Documentation

The API documentation can be viewed online on javadoc.io.

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.