📜 ⬆️ ⬇️

We launch console Java applications on Android



It will be a question of a utility project, which can be useful to anyone who studies Java and does not always have a PC at hand to view the work of the example code in the field (somehow - in the subway, shuttle bus, cafe, etc.).

The need to create this division arose when I was preparing for OCJP certification. After successful completion of this, I decided to provide the fruits of my ideas for review to the respected participants of the community.
')
I don’t have a laptop, but it’s convenient to run code samples on a smartphone while sitting in the kitchen with a cup of tea.

It so happened that most of the examples for training are usually given in the format of console programs. The wonderful AIDE tool allows you to build an Android application based on Eclipse projects. The opportunity, alas, is not enough to run console examples using copy-paste.

Things are easy - we redirect the input / output streams to the UI controls:

private static OutputStream mOutputStream = new OutputStream() { @Override public void write(int b) { MainActivity.getInstance().write((byte)b); } }; @Override public void onCreate(Bundle savedInstanceState) { ... System.setOut(new PrintStream(mOutputStream)); System.setErr(new PrintStream(mOutputStream)); ... } 


The console program starts in a separate thread from the activity onResume() handler:

  @Override public void run() { try { // //     // Program.main(new String[0]); } catch (Throwable ex) { //      setErrorColor(); ex.printStackTrace(); } } 


Here you can set the command line arguments - good old args . Example - run BankTellerSimulation for 10 seconds:

 BankTellerSimulation.main(new String[] { "10" }); 


The work of several threads is supported ( SimpleThreadsTest ):



HorseRace and BankTellerSimulation are examples of more complex multi-threaded applications (taken from the book Thinking in Java, 4th Ed by Bruce Eckel ).

Runtime errors

ErrorTest example:

 class FuuuuuuException extends RuntimeException { public FuuuuuuException(String message) { super(message); } } ... throw new FuuuuuuException("!!!TROLLFACE!!!"); 


We get an exception:



Input

Finally, for the sake of completeness, console input was added. So far, my knowledge of the Android user interface is not great. I managed to shove the EditText , but the result is not perfect :)

The prompt to enter a string ( ReadingInputTest ):



Further:



Menu functions

Exit disables the process through System.exit(0) .

The program is "disposable." There are no repeated restarts (I didn't want to bother with clearing the values ​​of static fields).

What makes Clear is a big secret.

What does not work

assert 's java. Those. operator

 assert "Cat".equals("Dog"); 

exception java.lang.AssertionError will not throw. Not to be confused with JUnit asserts - everything is fine with them!)
Console class.
• The input field does not always pick up the focus.

Project files

You can pick up the sources from GitHub , or download a zip archive with projects for Android Studio and Eclipse.

The examples described are in the com.examples package. All examples can be run unchanged on a regular PC (run everywhere!).

Source: https://habr.com/ru/post/162741/


All Articles