How to Install Java in Termux (2026): OpenJDK 17/21 Setup Guide
How to Install Java in Termux: Complete Step-by-Step Guide
Want to run Java programs right on your Android phone? You aren't alone. Setting up a mobile development environment used to be a massive headache. Thankfully, bringing a full programming toolkit to your pocket is easier than ever.
In this guide, we'll walk through how to install Java in Termux cleanly, troubleshoot frustrating bugs, and write your first real-world application on the go.
Quick Summary: Install Java in Termux
If you need a fast answer right now, here is the quick workflow to get OpenJDK running on your terminal:
- Update your package lists using
pkg update && pkg upgrade. - Install a Java Development Kit using
pkg install openjdk-17(oropenjdk-21for the newer LTS release). - Verify the installation by typing
java -version.
What is Java in Termux?
Java is a robust, class-based, object-oriented programming language designed to have as few implementation dependencies as possible, so code written once can run on many platforms. When you run Java inside Termux, you're turning your Android device into a portable Linux development workstation without needing root access.
Whether you're compiling small .java files with javac or executing compiled applications with java, having this capability on your phone opens up real possibilities for mobile coding.
How to Install Java in Termux (Step-by-Step)
Follow these steps to set up your environment from scratch.
- Launch Termux: Open your Termux app. If you plan to save files to shared storage later, run
termux-setup-storagenow — see the Termux storage permission guide if that step gives you trouble. - Refresh Repositories: Run the update command to grab the latest package indexes.
pkg update && pkg upgrade -y - Install OpenJDK: Install the JDK package directly from Termux's official repositories.
pkg install openjdk-17 -yTermux also officially packages
openjdk-21, the newer LTS release, if you'd rather target a more current Java version — the install and usage steps are identical, just swap the package name. - Verify the Installation: Check if the binary is accessible in your PATH.
java -version
Real Use-Case Example: Compiling and Running Your First Program
Let's look at a practical scenario. Imagine you're away from your laptop, but you need to test a quick logic script or show a friend how object-oriented programming works.
First, create a new file named HelloWorld.java using your preferred terminal text editor (see the Nano and Vim editor guide if you need a refresher):
nano HelloWorld.java
Paste the following standard Java code into the editor:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello from Termux Java!");
}
}
Save and exit the editor. Now compile the source code into bytecode using the Java compiler:
javac HelloWorld.java
Finally, run your newly compiled program:
java HelloWorld
You should see the output printed directly to your mobile screen: Hello from Termux Java!
Troubleshooting: Fixing Common Errors
Even with a smooth installation, mobile environments can occasionally throw curveballs. Here's how to handle the most frequent hiccups.
1. Termux OpenJDK Install Error ("Unable to locate package" / 404)
This usually happens when your package repository mirror is outdated or temporarily unreachable. Fix it by switching mirrors:
termux-change-repo
Select a working mirror from the interactive menu, update your packages again with pkg update, and retry the installation command. If the error persists on a fresh install, it's worth double-checking against the general guide to fixing "unable to locate package" in Termux.
2. Command Not Found After Installation
If the system claims java or javac doesn't exist after a seemingly successful install, the package likely didn't finish installing cleanly. Reinstall it explicitly:
pkg install --reinstall openjdk-17 -y
3. Storage Space and Play Store Version Issues
JDK packages are relatively large — make sure you have at least 500MB of free space. Also confirm your Termux app came from F-Droid or GitHub rather than the Google Play Store version, which is deprecated and can cause package mismatches on modern installs.
Comparison: Termux Java vs. Alternative Tools
How does running Java inside a terminal emulator stack up against dedicated Android IDEs or a desktop machine?
| Feature | Termux (OpenJDK) | Dedicated Android IDEs (e.g., AIDE) | Desktop PC (Windows/Mac/Linux) |
|---|---|---|---|
| Setup Complexity | Moderate (command-line based) | Low (graphical UI) | Low to moderate |
| Resource Usage | Very low (headless/CLI) | Medium | High |
| Portability | High (in your pocket) | High (in your pocket) | Low (desk-bound) |
| Full Terminal Access | Yes | No | Yes |
While graphical IDEs offer point-and-click interfaces, Termux gives you a real Linux shell experience — a strong option for developers who want raw control over their environment.
Best Practices for Mobile Java Development
Writing code on a touchscreen isn't always comfortable. Keep these tips in mind:
- Use an external Bluetooth keyboard if you plan on writing more than a few lines of code.
- Back up your important
.javafiles to cloud storage or a local Git repository — see how to install Git in Termux if you haven't set that up yet. - Keep your Termux environment updated regularly with
pkg upgradeto receive security patches and performance improvements.
Frequently Asked Questions
Can I run heavy GUI Java applications in Termux?
Running full graphical Java apps (Swing/AWT) generally requires a separate X11 server like Termux:X11, and even then some GUI-dependent features (like splash screens) can hit compatibility errors on Android. Termux is best used for command-line tools, scripting, and backend logic testing rather than desktop GUI apps.
Do I need root access to install Java?
No. Termux runs in a sandboxed user space on Android, so you can install packages like OpenJDK entirely without rooting your device.
Which Java version should I install?
OpenJDK 17 and OpenJDK 21 are both available as official Termux packages, and both are current LTS (Long-Term Support) releases. Install 21 if you want the newer language features; stick with 17 if a project or tutorial specifically requires it.
What if I want to try a different language instead?
Termux supports plenty of others — see installing Python in Termux, installing PHP in Termux, or installing Ruby in Termux.
Conclusion
Setting up your mobile Java development environment doesn't have to be intimidating. By following the steps above, you can configure your terminal, compile source code, and troubleshoot the common bugs that come up along the way. Ready to take your mobile coding skills further? Open your terminal and try running your first program today.
Join the conversation