如何在 Fedora 上开始 Java 开发
Java
![](/data/attachment/album/201805/13/075645epzn1bt0y1pn8huu.jpg)


Java 是世界上最流行的编程语言之一。它广泛用于开发物联网设备、Android 程序、Web 和企业应用。本文将提供使用 [OpenJDK](http://openjdk.java.net/) 安装和配置工作站的指南。


### 安装编译器和工具


在 Fedora 中安装编译器或 Java Development Kit（JDK）很容易。在写这篇文章时，可以用 v8 和 v9 版本。只需打开一个终端并输入：



```
sudo dnf install java-1.8.0-openjdk-devel

```

这安装 JDK v8。对于 v9，请输入：



```
sudo dnf install java-9-openjdk-devel

```

对于需要其他工具和库（如 Ant 和 Maven）的开发人员，可以使用 **Java Development** 组。要安装套件，请输入：



```
sudo dnf group install "Java Development"

```

要验证编译器是否已安装，请运行：



```
javac -version

```

输出显示编译器版本，如下所示：



```
javac 1.8.0_162

```

### 编译程序


你可以使用任何基本的文本编辑器（如 nano、vim 或 gedit）编写程序。这个例子提供了一个简单的 “Hello Fedora” 程序。


打开你最喜欢的文本编辑器并输入以下内容：



```
public class HelloFedora {

      public static void main (String[] args) {
              System.out.println("Hello Fedora!");
      }
}

```

将文件保存为 `HelloFedora.java`。在终端切换到包含该文件的目录并执行以下操作：



```
javac HelloFedora.java

```

如果编译器遇到任何语法错误，它会发出错误。否则，它只会在下面显示 shell 提示符。


你现在应该有一个名为 `HelloFedora` 的文件，它是编译好的程序。使用以下命令运行它：



```
java HelloFedora

```

输出将显示：



```
Hello Fedora!

```

### 安装集成开发环境（IDE）


有些程序可能更复杂，IDE 可以帮助顺利进行。Java 程序员有很多可用的 IDE，其中包括：


* Geany，一个快速加载的基本 IDE，并提供内置模板
* Anjuta
* GNOME Builder，已经在 [Builder - 这是一个专门面向 GNOME 程序开发人员的新 IDE](https://fedoramagazine.org/builder-a-new-ide-specifically-for-gnome-app-developers-2/) 的文章中介绍过


然而，主要用 Java 编写的最流行的开源 IDE 之一是 [Eclipse](https://www.eclipse.org/)。 Eclipse 在官方仓库中有。要安装它，请运行以下命令：



```
sudo dnf install eclipse-jdt

```

安装完成后，Eclipse 的快捷方式会出现在桌面菜单中。


有关如何使用 Eclipse 的更多信息，请参阅其网站上的[用户指南](http://help.eclipse.org/oxygen/nav/0)。


### 浏览器插件


如果你正在开发 Web 小程序并需要一个用于浏览器的插件，则可以使用 [IcedTea-Web](https://icedtea.classpath.org/wiki/IcedTea-Web)。像 OpenJDK 一样，它是开源的并易于在 Fedora 中安装。运行这个命令：



```
sudo dnf install icedtea-web

```

从 Firefox 52 开始，Web 插件不再有效。有关详细信息，请访问 Mozilla 支持网站 <https://support.mozilla.org/en-US/kb/npapi-plugins?as=u&utm_source=inproduct>。


恭喜，你的 Java 开发环境已准备完毕。




---


via: <https://fedoramagazine.org/start-developing-java-fedora/>


作者：[Shaun Assam](https://fedoramagazine.org/author/sassam/) 选题：[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
Java is one of the most popular programming languages in the world. It is widely-used to develop IOT appliances, Android apps, web, and enterprise applications. This article will provide a quick guide to install and configure your workstation using [OpenJDK](http://openjdk.java.net/).


## Installing the compiler and tools

Installing the compiler, or Java Development Kit (JDK), is easy to do in Fedora. At the time of this article, versions 8 and 9 are available. Simply open a terminal and enter:

sudo dnf install java-1.8.0-openjdk-devel

This will install the JDK for version 8. For version 9, enter:

sudo dnf install java-9-openjdk-devel

For the developer who requires additional tools and libraries such as Ant and Maven, the **Java Development** group is available. To install the suite, enter:

sudo dnf group install "Java Development"

To verify the compiler is installed, run:

javac -version

The output shows the compiler version and looks like this:

javac 1.8.0_162

## Compiling applications

You can use any basic text editor such as *nano*, *vim*, or *gedit* to write applications. This example provides a simple “Hello Fedora” program.

Open your favorite text editor and enter the following:

public class HelloFedora { public static void main (String[] args) { System.out.println("Hello Fedora!"); } }

Save the file as *HelloFedora.java*. In the terminal change to the directory containing the file and do:

javac HelloFedora.java

The compiler will complain if it runs into any syntax errors. Otherwise it will simply display the shell prompt beneath.

You should now have a file called *HelloFedora,* which is the compiled program. Run it with the following command:

java HelloFedora

And the output will display:

Hello Fedora!

## Installing an Integrated Development Environment (IDE)

Some programs may be more complex and an IDE can make things flow smoothly. There are quite a few IDEs available for Java programmers including:

[Geany](https://www.geany.org/), a basic IDE that loads quickly, and provides built-in templates[Anjuta](http://anjuta.org/)[GNOME Builder](https://wiki.gnome.org/Apps/Builder), which has been covered in the article[Builder – a new IDE specifically for GNOME app developers](https://fedoramagazine.org/builder-a-new-ide-specifically-for-gnome-app-developers-2/)

However, one of the most popular open-source IDE’s, mainly written in Java, is [Eclipse](https://www.eclipse.org/). Eclipse is available in the official repositories. To install it, run this command:

sudo dnf install eclipse-jdt

When the installation is complete, a shortcut for Eclipse appears in the desktop menu.

For more information on how to use Eclipse, consult the [User Guide](http://help.eclipse.org/oxygen/nav/0) available on their website.

## Browser plugin

If you’re developing web applets and need a plugin for your browser, [IcedTea-Web](https://icedtea.classpath.org/wiki/IcedTea-Web) is available. Like OpenJDK, it is open source and easy to install in Fedora. Run this command:

sudo dnf install icedtea-web

As of Firefox 52, the web plugin no longer works. For details visit the Mozilla support site at [https://support.mozilla.org/en-US/kb/npapi-plugins?as=u&utm_source=inproduct](https://support.mozilla.org/en-US/kb/npapi-plugins?as=u&utm_source=inproduct).

Congratulations, your Java development environment is ready to use.

## Mattias Bengtsson

If you’re developing web applets in 2018 you should probably sit down and reconsider your choices. :/

## Steve

I develop in Java on my Linux box daily. I use IntelliJ’s IDEA IDE. I find it much more intuitive than Elcipse and less bulky than netbeans. It has a fantastic selection of tools, and very useful code completion helpers. It is available as a community edition, but is not packaged with Fedora. It is the same IDE as that which Android uses for development.

## Mostafa Kamel

I expected a very powerful tools for java . Intellij has an open source version “community” . I consider it the best IDE for java . wow applet , applet is a dead man .

Geany and Eclipse isn’t powerful than IntelliJ or Netbeans . Now java8 is considered the legacy for java . the most of java developer still code by java8 .

## satai

As the previous commenters

– don’t develop applets. They are dead.

– don’t start with any other IDE than IntelliJ. (Write your first Java program using an editor of choice to see how everything works behidn the scenes and then install IntelliJ. It’s possible that ypu will be forced to downgrade to Eclipse some time in the future for some reasons such as an plugin used by your employer, but don’t torture yourself from the start).

My points

– use javac for the first “hello world” project but use a project lifecycle tool for the next one (maven and gradle are probably the most popular choices right now).

– have a look at testing tools as soon as possible. It will make your life easier from the begining (JUnit is the common choice, Spock is my recomendation if you need a bit more power.)

## Rene Reichenbach

“don’t start with any other IDE than IntelliJ” <– totally disagree

Choose the tooling that fits YOUR needs including any habits.

Eclipse can do it.

Apache Netbeans can do it.

Well configured VIM/Emacs can do it.

even VSCode with RedHat Java Extension can do it.

anything that edits text can do it …

If you like IntelliJ use it. Its not bad at all. But to declare it the universal truth is just wrong.

If you really “need” IntelliJ. Your Project is usually already to large and you should rethink the architecture.

And as mentioned tools like maven and gradle should be used as soon as you feel save with the language basics.

## Steve

I agree that you should always go with what you feel fits your work flow the best. For instance, my choice of the IntelliJ IDE IDEA over Eclipse and Netbeans was driven from past years experiences with all of them. However I am not “married” to any one of them, and if was asked outright, would likely have a rant list for all three of things I don’t like about them. Definitely familiarization with the build system is a worthwhile effort, and one which seems to be often overlooked by many. As far as size relative to use, I will confess that it is often convenience that drives me in the end, and the desire to ‘get this done’.

## judovana

Applets are dead, but ITW is also javaws (java web start ) provider. And javaws will be with us for some more time…

For fun – ITW can run also browser applets without browser – javaws –html httpx://some/page.html -as major browsers shut down npapi.

Idea have imho worst auto completion. Although in other tooling it is best. Fun is that, NetBeans autocompletion was based on Idea’s one, and then intelijidea left it, and come with the new, – current terrible one. eclipse are someewhere in the middle with autocompeltion.

## weezerwill

I’m new to Java programming. Out of curiosity, what makes the auto completion bad in the IntelliJ IDE? I struggled with getting Netbeans setup and working, but IntelliJ was pretty straight forward. I’m wondering if I need to go back to Netbeans. Thanks in advance.

## Rene Reichenbach

That sounds like some configuration issue. The autocompletion works quite good in Eclipse, Netbeans and IntelliJ. Make sure to use Apache Netbeans and not the “legacy 8.2” from Oracle.

## Aaron

It’s also worth looking at something like SDKMAN for managing different concurrent Java Versions, if for whatever reason you need oracle too etc.

## toquart

Thanks

## Petr "Stone" Hracek

Here is my IntelliJ-IDEA community repo. https://github.com/phracek/IntelliJ-IDEA-community

But I did not update it a couple month ago.