Best way to run java on Android | Using Termux

Termux is an Android terminal emulator.

Answer of the question " How to run java program in Android device ? " is here.

To run java in android using Termux requires the installation of two termux packages.

  • ecj : - (Eclipse compiler for java) this compile your java program and create .class file.
  • dx : - it take .class file and redevelop .dex.
java, termux ,run java, java on termux, termux java

Before we Learn How to run java on android, lets take a look at Some Important Termux command we must remember:

- To clear all screen content run the command.
$ clear

- To find current working directory.
$ pwd

- To change our current working directory to another directory. Here I’m moving in /java directory/folder.
$ cd /java

- If you want one directory/folder to move
$ cd..

- If you want to back two directories use.
$ cd ../../

- To Create New File: To Create New Directory/Folder use mkdir commad Stands for make a new directory.
$ mkdir <new-directoy-name>

- To Delete Directory/Folder in Termux
$ rmdir <directory-name>    

- To delete File
$ rm <file-name>

Here is How You Can Write and Run Java Programs on Your Android Devices 

Step 1: Install Termux from Google Play 

Update: Play Store Version of Termux is Deprecated. Due to Android 10 issues, Termux and its plugins are no longer updated on the Google Play Store and have been deprecated. The most recent version for Android >= 7 was v0.101. It is strongly advised not to install Termux apps from the Play Store any longer. 
Download From:
- F-Droid : F-Droid is a robot with a passion for Free and Open Source Software (FOSS) on the Android platform.
Click here to dowload F-Droid to get Termux.
 
-Github :
Click here to get termux from Github

Step 2: Now Update and Upgrade Termux
- Open Termux
- To Update Termux type command 
        $ apt update
- To Upgrade Termux type command
        $ apt upgrade

Step 3: To install ecj and dx run the following commands

        $ apt install ecj
        $ apt install dx

Step 4: To Create java file with .java extension.
- Type command
    $ nano hello.java

- This will create hello.java file and open in nano text editor.
- Now write simple java program in nano text editor as given below
    public class hello
      {
         public static void main(String args[])
         {
         System.out.println(“ Welcome to Java Learning Word           With computer bits daily”);
         }
      }

- Note down that  file_name and class name must be same to avoid error and confusion.
- Ctrl + s to save file and Ctrl + x to close nano text editor

Step 5: To create hello.class file type command
    $ ecj hello.java

- This command will compile java file and create  .class

Step 6: We need to convert .class file to .dex file for that type following command:
    $ dx --dex --output=hello.dex hello.class

- This will create hello.dex file
 
Step 7: To run the program execute command
    $ dalvikvm -cp  hello.dex  hello

- You will get the output 
Welcome to Java Learning Word With computer bits daily

Step 8: To exit Termux type command 
$ exit

Learn by Watching Video Tutorial on how to create and run Java On Termux. 




Now you’re ready to Run Java program On Termux . Happy Coding | Keep Learning | |Keep Sharing | Stay Safe!