Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

How to Import Java Console

104 12
    • 1). Click "Start" in the Windows Desktop and then click "Java JDK."

    • 2). Click "File," select "Open" and then double-click on the Java program in which you want to import characters from the system console.

    • 3). Add the following code after the "public void main()" line in your program:

      import java.io.Console;

      import java.nio.charset.Charset;

      import java.lang.reflect.Constructor;

      import java.lang.reflect.Field;

      import java.lang.reflect.InvocationTargetException;

      import static java.lang.System.out;

      // store Console character set in the private field

      public class ConsoleCharset {

      public static void main(String... args) {

      Constructor[] ctors =

      Console.class.getDeclaredConstructors();

      Constructor ctor = null;

      for (int i = 0; i < ctors.length; i++) {

      ctor = ctors[i];

      if (ctor.getGenericParameterTypes().length == 0)

      break;

      }

      // obtain the internal character set used by java.io.Console

      try {

      ctor.setAccessible(true);

      Console c = (Console)ctor.newInstance();

      Field f = c.getClass().getDeclaredField("cs");

      f.setAccessible(true);

      out.format("Console charset : %s%n", f.get

      (c));

      out.format("Charset.defaultCharset(): %s%n",

      Charset.defaultCharset());

      // intercept the system input exceptions

      } catch (InstantiationException x) {

      x.printStackTrace();

      } catch (InvocationTargetException x) {

      x.printStackTrace();

      } catch (IllegalAccessException x) {

      x.printStackTrace();

      } catch (NoSuchFieldException x) {

      x.printStackTrace();

      }

      }

      }

    • 4). Click "File" and then click "Save" to add console access to your Java program.

Source...

Leave A Reply

Your email address will not be published.