To compile the file, open your terminal and type

javac filename.java

To run the generated class file, use

java filename

But to do this you need to have the Java JDK installed in your computer. You can install it with the instructions in How do I install Java?.

Answer from Raja G on askubuntu.com
🌐
It's FOSS
itsfoss.com › run-java-program-ubuntu
How to Run Java Programs in Ubuntu
January 11, 2023 - You need to install JDK in order to compile and run Java programs. First, check if you already have Java Compiler installed on your system: ... If you see an error like “Command ‘javac’ not found, but can be installed with”, this means ...
Discussions

Running Java Program from Command Line Linux - Stack Overflow
I am not very experience with java and this is driving me crazy. I wrote a java program FileManagement and I need to run it from the command line. I can compile it from the command line with javac More on stackoverflow.com
🌐 stackoverflow.com
How to compile and run .java files in ubuntu? - Stack Overflow
I have Ubuntu 13.04 32-bit installed on my system. I downloaded Java SE Development Kit 7 from here with the extention .tar.gz · I used the command tar xvfz Downloads/jdk-7u51-linux-i586.tar.gz to uncompress the downloaded file in my home directory i.e /home/computer/ I am able to compile and run java program ... More on stackoverflow.com
🌐 stackoverflow.com
How to run Java application on startup of Ubuntu Linux - Stack Overflow
Is my Socket Program with no GUI is fine to be run as per my requirement. How can I write script to run jar on Linux Boot up, I got to know. init.d is meant for this. ... Ideally you should create a service wrapper for your java application and then make this service run on startup example here. Use sudo update-rc.d mytestserv defaults to run your service wrapper on startup on Ubuntu... More on stackoverflow.com
🌐 stackoverflow.com
How to run a java class on ubuntu? - Stack Overflow
That is why when you use a package in your code, that path must be the actual path of your java file (That means that your code is supposed to be in a directory called sec01 which is inside directory ch01). With that being set, when running code inside a package, you need to include the path ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Arshovon
arshovon.com › blog › run-java-program-ubuntu-18-04
Run Java Program in Ubuntu 18.04 - Ahmedur Rahman Shovon
openjdk 11.0.5 2019-10-15 OpenJDK ... If you do not have installed Java compiler, install it using: ... Go to any directory and create a demo Java program....
🌐
Learn Ubuntu
learnubuntu.com › run-java-programs-terminal
How to Run Java Programs in Ubuntu Terminal
December 15, 2023 - How to Set JAVA_HOME Variable in Ubuntu Linux Correctly · If you are running Java programs on Ubuntu using Eclipse, Maven or Netbeans etc, you’ll need to set JAVA_HOME to your path.
🌐
YouTube
youtube.com › watch
How to Run Java Program in Terminal Ubuntu Linux - YouTube
In this video I am going to show you How to install Java JDK 10 on Ubuntu Linux ( with JAVA_HOME ). also we will see Steps for compiling and running a Java p...
Published   February 25, 2021
Top answer
1 of 2
13

Open the terminal and run:

sudo apt-get install openjdk-7-jdk

and then compile your Java program as before with: javac abc.java. Then run it with:

java abc  ## The name of the class to be called is abc NOT abc.class

You can also substitute openjdk-6-jdk instead of openjdk-7-jdk in the first command. In Ubuntu 15.10 and newer, you can also substitute openjdk-8-jdk instead of openjdk-7-jdk in the first command. In Ubuntu 17.10 you can also substitute openjdk-9-jdk. In Ubuntu 17.10 and later you can also substitute openjdk-11-jdk.

In Java 9 and letter, Java has a built-in shell that can run blocks of Java code directly from the terminal without compiling the Java code first, jshell, defined in JEP 222. To start jshell from the terminal type jshell.

$ jshell
|  Welcome to JShell -- Version 11.0.7
|  For an introduction type: /help intro

jshell>

To exit from jshell type /exit.

2 of 2
3

If you prefer to install Oracle JDK, a step by step instruction on installing Oracle JDK 8 is explained in this article : Install Latest Oracle JDK in Ubuntu

Step 1: Download the latest JDK(jdk-Xuxx-linux-xXX.tar.gz) from this official link.

Step 2: Open the terminal (Ctrl + Alt + T) and enter the following command.

sudo mkdir /usr/lib/jvm

Step 3: Enter the following command to change the directory.

cd /usr/lib/jvm

Step 4: Extract the jdk-Xuxx-linux-xXX.tar.gz file in that directory using this command.

sudo tar -xvzf ~/Downloads/jdk-8u45-linux-x64.tar.gz

Step 5: Enter the following command to open the environment variables file.

sudo gedit /etc/environment

Step 6: In the opened file, add the following bin folders to the existing PATH variable.

/usr/lib/jvm/jdk1.8.0_45/bin
/usr/lib/jvm/jdk1.8.0_45/db/bin
/usr/lib/jvm/jdk1.8.0_45/jre/bin

The PATH variables have to be separated by semicolon. Notice that the installed JDK version is 1.8 update 45. Depending on your JDK version, the paths can be different. Add the following environment variables at the end of the file.

J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"

The environment file before the modification:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

The environment file after the modification:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_45/bin:/usr/lib/jvm/jdk1.8.0_45/db/bin:/usr/lib/jvm/jdk1.8.0_45/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"
Find elsewhere
🌐
Dracula Servers
draculaservers.com › home › how to run java programs in linux terminal?
How to Run Java Programs in Linux Terminal? - Dracula Servers Tutorials
June 30, 2024 - To install JDK on Ubuntu, run the following commands. sudo apt-get update sudo apt-get install default-jdk · To install JDK on Fedora, run the following commands. sudo dnf update sudo dnf install java-11-openjdk-devel · To install JDK on CentOS, ...
🌐
VITUX
vitux.com › your-first-java-program-in-the-ubuntu-terminal
Your First Java Program in the Ubuntu Terminal – VITUX
This article covers the installation of these two through the Ubuntu command line. You can then write your first program in a text editor such as gedit, nano or even some graphical text writing tool. You will then compile your program in order to create a class that you can then execute in order to run your Java program.
🌐
YouTube
youtube.com › programmingknowledge2
How to Run Java Program in Terminal Ubuntu 24.04 LTS Linux (2024) - YouTube
**How to Run Java Program in Terminal Ubuntu 24.04 LTS Linux Using Oracle Java**Welcome to our comprehensive tutorial on how to run a Java program in the ter...
Published   June 18, 2024
Views   1K
🌐
Wikihow
wikihow.com › computers and electronics › operating systems › linux › ubuntu › how to create your first java program on ubuntu linux - wikihow
How to Create Your First Java Program on Ubuntu Linux - wikiHow
January 28, 2021 - (This will fail if you don't have javac installed, if it fails, either follow the instructions in the introduction or type on your command line: sudo apt-get install openjdk-7-jdk) ... Run or execute your Java class file by issuing the following command. ... Yes. It will notify you of your programming mistakes.
🌐
Stack Overflow
stackoverflow.com › questions › 22420591 › how-to-compile-and-run-java-files-in-ubuntu
How to compile and run .java files in ubuntu? - Stack Overflow
I have Ubuntu 13.04 32-bit installed on my system. I downloaded Java SE Development Kit 7 from here with the extention .tar.gz · I used the command tar xvfz Downloads/jdk-7u51-linux-i586.tar.gz to uncompress the downloaded file in my home directory i.e /home/computer/ I am able to compile and run java program using Bluej IDE.
🌐
Sololearn
sololearn.com › en › Discuss › 227620 › how-to-run-java-program-on-ubuntu-16-0-4-lts
How to run java program on Ubuntu 16.0.4 LTS? | Sololearn: Learn to code for FREE!
February 22, 2017 - If not: sudo apt-get install openjdk-7-jdk Then to open your java projects type "java" "class name" You could just install eclipse or netbeans and run your projects in them. Hope this helps :) ... you have to edit the system path file too. follow ...
🌐
Bugtreat
bugtreat.com › blog › wp-content › themes › bugtreat › blog › how-to-run-your-first-java-program-on-ubuntu-linux
How to run your first java program on ubuntu linux
1.Once Java is installed on your system, the next step will be to create a directory to hold your Java programs. Open up a terminal on Ubuntu Linux and create your Java applications directory.
🌐
Ubuntu
ubuntu.com › tutorials › install-jre
Install the Java Runtime Environment | Ubuntu
Download JRE binaries in .tar.gz (tarball) by heading over to their website. An Oracle account is needed to download the Oracle HotSpot JRE. Oracle does not currently offer JRE packages for Java 11 or above from their website so, for this tutorial, we will use the Oracle HotSpot JRE version to be 8u291 (Java 8, update 291). ... java version "1.8.0_291" Java(TM) SE Runtime Environment (build 1.8.0_291-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
🌐
Ubuntu
documentation.ubuntu.com › ubuntu-for-developers › tutorials › java-use
Develop with Java on Ubuntu - Ubuntu for Developers
4 weeks ago - Calculating task graph as no cached configuration is available for tasks: run > Task :app:run Hello World! BUILD SUCCESSFUL in 424ms 2 actionable tasks: 1 executed, 1 up-to-date Configuration cache entry stored. Compiling a Java application directly using the javac tool.
🌐
YouTube
youtube.com › watch
How to Run Java Program in Ubuntu
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Quora
quora.com › How-do-I-compile-and-run-a-Java-program-in-a-package-from-terminal-Ubuntu
How to compile and run a Java program in a package from terminal (Ubuntu) - Quora
Answer: Hi, In Ubuntu open terminal and follow the below steps: Java programs can be compiled by [code]javac JavaFileName.java [/code]Upon compiling it will generate .Class files which you should run with java command thus java program will run. [code]java ClassName [/code]If java program is ...