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
🌐
FOSS Linux
fosslinux.com › home › programming › compile and run java from the linux cli (2026 developer guide)
Compile and Run Java from the Linux CLI (2026 Developer Guide)
April 29, 2026 - To compile a Java program that references code within that JAR file, you must include it in your compilation command: fosslinux@dev:~$ javac -cp ".:external_libs/utilities.jar" Main.java · The -cp flag defines the classpath. The dot (.) represents the current directory, and the colon (:) separates multiple paths on Linux.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 11hello › linux.html
Hello World in Java on Linux
August 14, 2019 - This document instructs you on how to setup a Java programming environment under Linux. It also provides a step-by-step guide for creating, compiling, and executing your first Java program using either DrJava or the command line. We assume some familiarity with the command line.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › how to compile a java program on windows, macos, & linux
How to Compile a Java Program on Windows, macOS, & Linux
December 23, 2023 - Replace sourcecode.java with the name of your source file.[3] X Expert Source · Jessica Andzouana Software Engineer Expert Interview [4] X Research source This compiles your source code into an executable file, which ends with the .class extension. To see the name of the new file in the current directory, run the dir (Windows) or ls -a (Mac/Linux) command.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › linux-cmd.html
Java and the Linux Command Line
Assuming the file, say HelloWorld.java is in the current working directory, type the javac command below to compile it.
🌐
LinuxH2O
linuxh2o.com › home › how to compile and run java files on linux
How to compile and run java files on Linux - LinuxH2O
August 19, 2020 - In this basic guide, you will learn how to compile and run a java files on Linux using the command line. New java 11 compiling features also explained.
🌐
Explainjava
explainjava.com › compile-run-java-linux
How To Compile and Run Java Program in Linux – Java Programming Tutorials
February 20, 2019 - Today I’ll explain how to compile and run Java code on Linux environment.
Find elsewhere
🌐
It's FOSS
itsfoss.com › run-java-program-ubuntu
How to Run Java Programs in Ubuntu
January 11, 2023 - When you compile the Java program, it generates a .class file with the class name you used in your program. You have to run this class file. You do not need to specify the class extension here. Just the name of the class. And this time, you use the command java, not javac. ... This will print Hello World on the screen for my program. ... And that’s how you run a Java program in the Linux terminal.
🌐
LinuxForDevices
linuxfordevices.com › home › how to run a command-line java program on linux?
How to Run a Command-Line Java Program on Linux? - LinuxForDevices
July 19, 2021 - You can easily write, compile, and run a Java program from the command line following the steps mentioned above. To run a Java program, the Java Runtime Environment is required which can be installed using your Linux distribution’s official package manager.
🌐
Unix Community
community.unix.com › applications › programming
Using basic UNIX commands to make/compile JAVA files - Programming - Unix Linux Community
April 29, 2014 - Hello! This is my first post, and I just learned what UNIX was this week. For a JAVA programming class I am taking, I must be able to create a directory in UNIX, use the nano command to create a JAVA program, compile it, and then run it on the command prompt using the java command.
🌐
Linux Hint
linuxhint.com › run-java-command-line-linux
How to Run Java from Command-line in Linux – Linux Hint
Suppose I created a file named “testing.java” and write a simple program in it: (Keep in mind that your class name should be the same as the file name) Compile the testing.java file on the terminal using the javac command:
🌐
Oracle
docs.oracle.com › javame › config › cdc › cdc-opt-impl › ojmeec › 1.1 › developer › html › compiling.htm
2 Compiling Your Application
If you do not yet have it, follow ... http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u27-download-440405.html. Open a Linux terminal and cd into the directory where JDK1.6 has been downloaded....
🌐
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 - If you are only looking for the commands to run the program in the terminal, then use “javac [filename].java” to compile a java code into an executable program and the “java [filename]” to execute the program on the terminal. Check out More Linux Tutorials Here!
🌐
Linux Tiwary
linuxtiwary.com › 2016 › 07 › 23 › how-to-writecompile-and-run-java-program-on-linux
how to write,compile and run java program on linux – Learn Linux CCNA CCNP CEH CISSP CISA Penetration-Testing Bug Bounty IPv6 Cyber-Security Network-Security Online
September 13, 2020 - If you don’t have idea about how to configure yum server on linux watch this video. ... #vim helloworld.java public class helloworld { public static void main(String[] args) { System.out.println("Hello satish how are you?");//display the string } }
🌐
Florida State University
cs.fsu.edu › ~myers › cop3252 › howto › cmdline.html
Compiling on the Command line with Java SDK
Make sure Variable name says, "PATH". Under "Variable value", add in the expression that matches where the java installation is, along with "\bin".
🌐
LabEx
labex.io › tutorials › java-how-to-compile-and-run-a-java-program-in-the-terminal-413960
How to compile and run a Java program in the terminal | LabEx
By following the steps outlined in this tutorial, you will learn how to compile Java source code and run Java programs directly in the terminal. This knowledge will empower you to work efficiently with Java, whether you're a beginner or an experienced developer. Understanding the terminal-based workflow for Java development is a valuable skill that will serve you well throughout your programming journey. ... LinuxDevOpsCybersecurityDevOps EngineerCybersecurity EngineerDevSecOpsKali LinuxRed Hat Enterprise LinuxRHCSA TrainingRHCE in Enterprise Linux TrainingLFCS TrainingShellGitDockerKubernetesCKA TrainingCKAD TrainingCKS TrainingAnsibleRHCE in Ansible TrainingJenkinsNmapWiresharkHydraCompTIADatabaseMySQLPostgreSQLRedisMongoDBSQLitePythonGolangCC++Web DevelopmentData Science
🌐
Uwsuper
cs2.uwsuper.edu › sb › 201 › Intro › compile.html
Compiling and running Java programs on Unix/Linux
Go to cs201 folder by entering the command cd cs201. Linux machine will respond with a different prompt as it is shown below: ... If the compilation is successful you will see no warnings on the screen. Just a new prompt appears in the line following the javac instruction.