如何在 Ubuntu 中运行 Java 程序
Java
![](/data/attachment/album/202109/16/163803pn2dja7ajcx8jjw2.jpg)


听说，你已经开始学习 Java 编程了？很好。


你想在你的 Linux 系统上运行 Java 程序？那就更好了。


让我告诉你如何在 Ubuntu 和其他 Linux 发行版的终端中运行 Java。


### 在 Ubuntu 中运行 Java 程序


让我们在这里按正确的步骤进行。


#### 第一步：安装 Java 编译器


要运行一个 Java 程序，你需要先编译该程序。为此你需要 Java 编译器。


Java 编译器是 [JDK](https://jdk.java.net/)（<ruby> Java 开发工具包 <rt>  Java Development Kit </rt></ruby>）的一部分。你需要安装 JDK，以便编译和运行 Java 程序。


首先，检查你的系统上是否已经安装了 Java 编译器：



```
javac --version

```

如果你看到类似 “Command ‘javac’ not found, but can be installed with” 的错误，这意味着你需要安装 Java 开发工具包。


![Check if Java compiler is already installed or not](/data/attachment/album/202109/16/163812d4ygbbg0tnu99t2f.png)


在 Ubuntu 上安装 JDK 的最简单方法是使用 Ubuntu 的默认包：



```
sudo apt install default-jdk

```

你会被要求输入你的账户密码。当你输入密码时，屏幕上什么也看不到。这很正常。直接输入密码即可。当询问时，按回车键或 `Y` 键。


![Installing JDK that also contains the Java compiler](/data/attachment/album/202109/16/163814rke6mkm22dcm63zc.png)


上述命令应该适用于其他基于 Debian 和 Ubuntu 的发行版，如 Linux Mint、Elementary OS 等。对于其他发行版，请使用你的发行版的包管理器。包的名称也可能不同。


安装完毕后，验证 `javac` 现在是否可用。


![Verify that Java compiler can be used now](/data/attachment/album/202109/16/163815ptkgzzdfcapf1rc7.png)


#### 第二步：在 Linux 中编译 Java 程序


要编译的话，你首先需要有一个 Java 程序文件。假设你创建了一个名为 `HelloWorld.java` 的新的 Java 程序文件，它的内容如下：



```
class HelloWorld{
    public static void main(String args[]){
     System.out.println("Hello World");
    }
}

```

你可以 [使用终端下的 Nano 编辑器](https://itsfoss.com/nano-editor-guide/) 或 Gedit 图形化文本编辑器来编写你的 Java 程序。



```
javac HelloWorld.java

```

如果没有错误，上面的命令不会产生输出。


当你编译 Java 程序时，它会生成一个 .class 文件，文件名是你在程序中使用的类。你需要运行这个类文件。


#### 第三步：运行 Java 类文件


你不需要在这里指定类的扩展名。只需要类的名称。而这一次，你使用 `java` 命令，而不是 `javac`。



```
java HelloWorld

```

我的程序将在屏幕上打印 “Hello World”。


![Running java programs in the Linux terminal](/data/attachment/album/202109/16/163815uej81q3tmst0333e.png)


这就是你如何在 Linux 终端中运行一个 Java 程序。


这是最简单的一个例子。这个示例程序只有一个类。Java 编译器为你程序中的每个类都创建一个类文件。对于较大的程序和项目来说，事情会变得很复杂。


这就是为什么我建议 [在 Ubuntu 上安装 Eclipse](https://itsfoss.com/install-latest-eclipse-ubuntu/) 来进行 Java 编程。在 IDE 中编程更容易。


希望本教程对你有所帮助。有问题或建议吗？评论区都是你的。




---


via: <https://itsfoss.com/run-java-program-ubuntu/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
So, you have started learning Java programming? That’s good.

And you want to run the java programs on your Linux system? Even better.

Let me show how to run Java in terminal in Ubuntu and other Linux distributions.

## Running Java programs in Ubuntu

Let’s go in proper steps here.

### Step 1: Install Java compiler

To run a Java program, you need to compile the program first. You need Java compiler for this purpose.

The Java compiler is part of [JDK](https://jdk.java.net/) (Java Development Kit). 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:

`javac --version`


If you see an error like “Command ‘javac’ not found, but can be installed with”, this means you need to install Java Development Kit.

![java compiler check ubuntu](https://itsfoss.com/content/images/wordpress/2021/09/java-compiler-check-ubuntu-800x328.png)

The simplest way to install JDK on Ubuntu is to go with the default offering from Ubuntu:

`sudo apt install default-jdk`


You’ll be asked to enter your account’s password. When you type the password, nothing is seen on the screen. That is normal. Just enter your password blindly. When asked, press the enter key or Y key.

![install jdk ubuntu](https://itsfoss.com/content/images/wordpress/2021/09/install-jdk-ubuntu-800x430.png)

The above command should work for other Debian and Ubuntu based distributions like Linux Mint, elementary OS etc. For other distributions, use your distribution’s package manager. The package name could also be different.

Once installed, verify that javac is available now.

![java compiler ubuntu](https://itsfoss.com/content/images/wordpress/2021/09/java-compiler-ubuntu.png)

### Step 2: Compile Java program in Linux

You need to have a Java program file for this reason. Let’s say you create a new Java program file named **HelloWorld.java** and it has the following content:

```
class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World");
}
}
```


You can [use Nano editor in terminal](https://itsfoss.com/nano-editor-guide/) or Gedit graphical text editor for writing your Java programs.

`javac HelloWorld.java`


If there is no error, the above command produces no output.

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.

### Step 3: Run the Java 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.

`java HelloWorld`


This will print Hello World on the screen for my program.

![running java programs in linux terminal](https://itsfoss.com/content/images/wordpress/2021/09/running-java-programs-in-Linux-terminal.png)

And that’s how you run a Java program in the Linux terminal.

This was the simplest of the example. The sample program had just one class. The Java compiler creates a class file for each class in your program. Things get complicated for bigger programs and projects.

This is why I advise [installing Eclipse on Ubuntu](https://itsfoss.com/install-latest-eclipse-ubuntu/) for proper Java programming. It is easier to program in an IDE.

I hope you find this tutorial helpful. Questions or suggestions? The comment section is all yours.