Intellij Idea 简明教程

Intellij Idea - Quick Guide

Intellij Idea - Introduction

IntelliJ 是 Java 领域功能最强大且最受欢迎的集成开发环境 (IDE) 之一。它由 JetBrains 开发和维护,同时提供了社区版和旗舰版。这款功能丰富的 IDE 可实现快速开发,并有助于提高代码质量。

IntelliJ is one of the most powerful and popular Integrated Development Environments (IDE) for Java. It is developed and maintained by JetBrains and available as community and ultimate edition. This feature rich IDE enables rapid development and helps in improving code quality.

What is IDE and its benefits?

IDE 即集成开发环境。它整合了多个工具,使得软件开发流程更容易、更强大且更不容易出错。与普通文本编辑器相比,它拥有以下优势 −

IDE stands for Integrated Development Environment. It is a combination of multiple tools, which make software development process easier, robust and less error-prone. It has following benefits over plain text editor −

  1. Integration with useful tools like compiler, debugger, version control system, build tools, various frameworks, application profilers and so on.

  2. Supports code navigation, code completion, code refactoring and code generation features which boosts development process.

  3. Supports unit testing, integration testing and code coverage via plug-ins.

  4. Provides rich set of plug-ins to enhance IDE functionality further.

Features of IntelliJ

IntelliJ IDEA 拥有部分顶级的 Java 代码补全功能。其预测算法能够准确地假定编码人员试图输入的文本,并为其补全,即使编码人员不知道特定类、成员或任何其他资源的确切名称。

IntelliJ IDEA has some top productive Java code completion features. Its predictive algorithm can accurately assume what a coder is attempting to type, and completes it for him, even if he doesn’t know the exact name of a particular class, member or any other resource.

Deep Insight

IntelliJ IDEA 真正理解且 deep insight 您的代码以及编码人员的上下文,这使得它在其他 Java IDE 中独一无二。

IntelliJ IDEA really understands and has a deep insight into your code, as well as the context of the coder, which makes it so unique among other Java IDEs.

  1. Smart code completion − It supports context based code completion. It gives a list of the most relevant symbols applicable in the current context.

  2. Chain code completion − It is an advanced code completion feature which lists applicable symbols accessible via methods or getters in the current context.

  3. Static member’s completion − It allows you to use static methods or constants and automatically adds the required import statements to avoid compilation error.

  4. Detecting duplicates − It finds the duplicate code fragments on the fly and gives notification/suggestion about it to user.

  5. Inspections and quick-fixes − Whenever IntelliJ detects that you’re about to make a mistake, a little light bulb notification pops up on same line. Clicking it shows the suggestion list.

Developer Ergonomics

IntelliJ IDEA 是围绕编程原则设计的,即开发人员应该能够尽可能少地分心。因此,在这种情况下,编辑器是屏幕上唯一可见的东西,拥有针对所有其他与编程无关的功能的专用快捷键。

IntelliJ IDEA is designed around the coding principle that developers should be allowed to write codes with as less distraction as possible. That is why in this case, the editor is the only thing visible on the screen, with dedicated shortcuts for all other coding-unrelated functions.

  1. Editor-centric environment − Quick pop-ups help in checking additional information without leaving the current context.

  2. Shortcuts for everything − IntelliJ IDEA has keyboard shortcuts for nearly everything, including rapid selection and switching between tool windows and many more.

  3. Inline debugger − Inline debugger allows you to debug application in IDE itself. It makes the development and debugging process seamless.

Built-in Developer Tools

为了帮助开发人员组织他们的工作流程,IntelliJ IDEA 为他们提供了惊人的工具集,其中包括反编译器、Docker 支持、字节码查看器、FTP 和许多其他工具 −

To help the developers organize their workflow, IntelliJ IDEA offers them an amazing toolset, which comprises of a decompiler, Docker support, bytecode viewer, FTP and many other tools −

  1. Version control − IntelliJ supports most of the popular version control system like Git, Subversion, Mercurial, CVS, Perforce, and TFS.

  2. Build tools − IntelliJ supports Java and other build tools like Maven, Gradle, Ant, Gant, SBT, NPM, Webpack, Grunt, and Gulp.

  3. Test runner and code coverage − IntelliJ IDEA lets you perform unit testing with ease. The IDE includes test runners and coverage tools for major test frameworks, including JUnit, TestNG, Spock, Cucumber, ScalaTest, spec2, and Karma.

  4. Decompiler − IntelliJ comes with a built-in decompiler for Java classes. When you want to take a look inside a library that you do not have the source code for, you can do it without using any third-party plug-ins.

  5. Terminal − IntelliJ provides built-in terminal. Depending on your platform, you can work with the command line prompt, like PowerShell or Bash.

  6. Database tools − IntelliJ provides database tools, which allow you to connect to live databases; run queries; browse and update data; and even manage your schemas in a visual interface from IDE itself.

  7. Application server − IntelliJ supports major application servers: Tomcat, JBoss, WebSphere, WebLogic, Glassfish, and many others. You can deploy your artifacts onto application servers and debug the deployed applications in IDE itself.

  8. Docker support − Via a separate plug-in, IntelliJ provides a dedicated tool window that lets you connect to locally running Docker machines.

Comparison between Ultimate and Community Edition

Ultimate Edition 旨在协助 Web 和企业开发,而 Community Edition 旨在用于 JVM 和 Android 开发。让我们考虑一下一些要点,它们将帮助我们理解这两个版本之间的比较 −

The Ultimate Edition is designed to assist in web and enterprise development, whereas the Community Edition is designed for JVM and Android Development. Let us consider a few important points that will help us understand the comparison between the two editions −

Feature

Ultimate Edition

Community Edition

License

Commercial

Open-source, Apache 2.0. for commercial development.

Java, Kotlin, Groovy, Scala

Supported

Supported

Android development

Supported

Supported

Maven, Gradle, SBT

Supported

Supported

Git, SVN, Mercurial, CVS

Supported

Supported

Detecting Duplicates

Supported

Not supported

Perforce, TFS

Supported

Not supported

JavaScript, TypeScript

Supported

Not supported

Java EE, Spring, GWT, Vaadin, Play, Grails, Other Frameworks

Supported

Not supported

Database Tools, SQL

Supported

Not supported

Intellij Idea - Installation and Configuration

在本节中,我们将了解如何安装和配置 IntelliJ IDEA。这个过程的第一步是从选择版本开始。根据您的需要,您可以下载社区版或旗舰版。顾名思义,社区版绝对免费,并且我们也可以用它进行商业开发。然而,旗舰版是付费版本,我们可以免费评估 30 天。

In this chapter, we will understand how to install and configure IntelliJ IDEA. The first step of the process starts with choosing the edition. As per your requirements, you can download community or ultimate edition. As name suggests, community edition is absolutely free and we can use it for commercial development as well. However, ultimate edition is paid version and we can evaluate it freely for 30 days.

Installation on Windows

IntelliJ 与 2003 年之前的几乎所有版本的 Windows 兼容。具体清单如下:Windows 10/8/7/Vista/2003/XP。建议在 Windows 上安装 IntelliJ 之前关闭所有其他应用程序。

IntelliJ is compatible with almost all versions of Windows prior to 2003. A comprehensive list will be: Windows 10/8/7/Vista/2003/XP. It is recommended that you shut down all other applications before you install IntelliJ on Windows.

System Requirements

  1. A minimum 2 GB of RAM capacity is recommended for seamless performance.

  2. For better visualization, 1024x768 screen resolution is recommended.

  3. Minimum 300 MB disk space for installation and additional 1 GB for cache.

Downloading and Installation

  1. Downloading − You can download windows installer from their official website.

  2. Installation − Let us begin with the installation followed by the configuration steps. Installation of IntelliJ is similar to other software packages. Just double-click on the installer and follow the on-screen instructions to complete the installation process.

Installation on Linux

要在 Linux 平台上安装 IntelliJ,您需要注意,它不捆绑 32 位 JDK,因此建议使用 64 位系统。

For installation of IntelliJ on Linux platforms, you need to note that a 32-bit JDK is not bundled, so a 64-bit system is recommended.

System Requirements

  1. GNOME, KDE or XFCE desktop environment

  2. Minimum 2 GB of RAM is recommended for seamless usage

  3. 300 MB of disk space for installation and addition 1 GB for caches

  4. For better visualization, 1024x768 screen resolution is recommended

Downloading and Installation

  1. Downloading − You can download IntelliJ for Linux from their official website.

  2. Installation − We have downloaded tar.gz bundle. Note that in our case bundle’s name was ideaIC-2017.2.5.tar.gz. It may change with the edition/version. Please use the appropriate bundle name.

First extract it using following command:
$ tar xvf ideaIC-2017.2.5.tar.gz
It will create new directory with idea-IC-172.4343.14 name.
Now change directory to idea-IC-172.4343.14/bin/ and execute idea.sh shell script as shown below:
$ cd idea-IC-172.4343.14/bin/
$ ./idea.sh
Follow on-screen instructions to complete installation procedure.

Configure IntelliJ

在两个平台上的配置步骤是相似的。要开始配置,请启动 IntelliJ 应用程序。或者,您可以在此向导中 import 现有配置。单击“下一步”按钮继续。

The configuration steps are similar on both platforms. To begin configuration, launch IntelliJ application. Optionally, you can import the existing configuration from this wizard. Click on the next button to continue.

Step1 − 如果您正在使用旗舰版,则会弹出一个许可证激活窗口。选择免费评估选项,然后单击评估按钮,如下图所示。

Step1 − If you are using the ultimate edition, then a license activation window will pop-up. Select evaluate for free option and click on the evaluate button as shown in the following image.

license  activation

Step 2 − 接受许可协议以继续,然后按照屏幕上的说明启动 IntelliJ。您将看到 IntelliJ 的欢迎屏幕。

Step 2 − Accept the license agreement to proceed and follow on-screen instruction to start IntelliJ. You will see the Welcome screen of IntelliJ.

Step 3 − 现在,是时候使用 IntelliJ 配置 Java Development Kit(以下简称 JDK)了。如果尚未安装JDK,请按照 here 中的说明进行操作。

Step 3 − Now, it is time to configure the Java Development Kit (hereafter, we will refer to it as JDK) with IntelliJ. If JDK is not installed already then follow the instruction as in here.

  1. On the Welcome screen, click on ‘configure’

  2. Select ‘project defaults’ from the drop-down list

  3. Select the ‘project structure’ option

project structure
  1. Select the ‘SDKs’ option from the ‘platform settings’ menu.

  2. Click on the ‘plus’ icon and select the ‘JDK’ option.

  3. Select JDK’s home directory and follow the on-screen instructions.

platform settings

Intellij Idea - Getting Familiar

在本章中,我们将更熟悉 IntelliJ IDEA。要讨论任何工具的优势和功能,人们必须熟悉该工具,而 IntelliJ 也不例外。

In this chapter, we will get more familiar with IntelliJ IDEA. To discuss the advantages and functionality of any tool, one must be familiar with that tool and IntelliJ is no exception to that.

本章概述了 IntelliJ。本章从讨论 IDE 的可视元素、配置设置开始,最后以讨论 JVM 和平台属性结束。

This chapter gives you an overview of IntelliJ. The chapter begins with a discussion about IDE’s visual elements, configuration settings and finally ends by discussing JVM and platform properties.

Visual Elements

关于 IDE 的重要内容之一是其可视元素。识别和理解可视元素使您能够以一种 quicker 更简单的方式执行操作。以下屏幕截图及其上的标签显示了 IntelliJ 的主界面。

One of the important things about IDE is its visual elements. Identifying and understanding the visual elements enables to you do action in a quicker and easier manner. The following screenshot and the labels on it show the main interface of IntelliJ.

visual elements

在主窗口的底部,有一个 status 栏,其中显示了有关文件的某些其他属性,例如其格式和编码类型。它还提供了切换当前文件只读属性的选项。您还可以在此处管理检查级别。

At the bottom of the main window, there is a status bar, which shows some additional attributes about the file - like its format and the encoding type. It also provides option to toggle the read-only attribute of the current file. You can also manage inspection level from here.

Working with Plug-ins

插件有助于扩展 IntelliJ 的功能。它提供了大量插件,包括数据库、版本控制、配置文件等等。

Plug-ins help to extend functionality of IntelliJ. It provides a large number of plug-ins ranging from databases, version controlling, profiling and this list goes on.

plug ins ranging

Steps to manage plug-ins

按照以下步骤管理插件 −

Follow these steps to manage plug-ins −

  1. Go to the File → Settings menu.

  2. Select the Plugins tab on the left pane.

  3. This window lists all installed plug-ins. There is a checkbox on the right side of each plug-in name. Toggling that checkbox enables/disables the plug-ins.

  4. IntelliJ online plug-in repository is available here. To add/remove plug-in repository, click on the browse repository button and it will provide a way to do needful.

  5. In addition to this, it allows offline plug-in installation. For this, download plug-in and select install plug-in from the disk button and navigate to the download path.

要对插件执行其他操作,如卸载、更新和排序,请右键单击任意插件。它将显示一个下拉菜单,从中可以选择其中一个操作。

To perform other actions on plug-ins like uninstalling, updating and sorting, right-click on any plug-in. It will show a dropdown menu from which you can select one of the actions.

Working with Settings

本节提供了一些管理设置的重要提示。它使您可以导入、导出和共享 IntelliJ 设置。

This section provides a few important tips to manage settings. It enables you to import, export and share IntelliJ settings.

Export Settings

它允许将当前设置导出为 jar 文件。

It allows exporting the current settings as a jar file.

  1. Go to File → Export Settings.

  2. Export setting windows list the available settings related to UI, debugger, SDK along with others.

  3. It provides a checkbox for selection. Once the selection is done, click on the OK button to save the settings on the local disk.

Import Settings

它允许导入 jar 文件中存储的设置。

It allows importing the settings stored in the jar file.

  1. Go to File → Import settings.

  2. Select the Setting jar by navigating folder structure

  3. Click on the OK button.

Sharing Settings

IntelliJ IDEA 允许你在不同的实例之间共享你的 IDE 设置。当你想在团队或组织内应用相同的设置时,这特别有用。要实现此目的的前提条件是启用 Settings Repository 插件。它默认安装并启用。

IntelliJ IDEA allows you to share your IDE settings between different instances. This is particularly useful when you want to apply the same settings within a team or organization. The prerequisite for this is to enable the Settings Repository plug-in. It is installed and enabled by default.

要确保其状态 −

To ensure its status −

  1. Go to File → Settings → Plugins

  2. Search settings repository plug-in

我们可以将当前设置存储在 GitHub/Bitbucket 上,并将其应用于其他实例。要存储当前设置 −

We can store the current setting on GitHub/Bitbucket and apply them on other instances. To store the current setting −

  1. Go to the File → Settings Repository.

  2. Type the Git repository URL in upstream URL dialog box.

  3. Click on the Overwrite remote button.

  4. To apply the same settings to other instances, click on the Overwrite local button.

Configuring JVM Options and Platform Properties

可以通过导航到 Help → Edit Custom VM Options 菜单配置 JVM 选项。以下是我们可以设置的一些重要的 JVM 选项。

We can configure the JVM options by navigating to the Help → Edit Custom VM Options menu. Following are a few important JVM options we can set.

  1. –server − It allows the selection of the Java HotSpot Server VM

  2. -Xms<size> − It sets the initial Java heap size. The default value is 128 MB.

  3. -Xmx<size> − It sets the maximum Java heap size. The default value is 750 MB.

  4. -Xss<size> − It sets the Java thread stack size.

  5. –XX − It allows setting GC algorithm and other properties.

Intellij Idea - Create First Java Project

现在我们通过 IntelliJ 获取实践体验的时候了。在本章,我们将创建第一个 Java 项目。我们将编写并执行传统的 Hello World 程序。本章解释了 Java 应用程序的编译和运行。

It is time we got a hands-on experience with IntelliJ. In this chapter, we will create our first Java Project. We will write and execute the traditional Hello World program. This chapter explains the compilation and running of Java application.

Create Project

对于与开发相关的任何内容,开发者都必须使用 IntelliJ 创建一个新项目。让我们遵循以下步骤来创建一个项目:

For anything related to development, a developer has to create a new project with IntelliJ. Let us follow these steps to create a project −

  1. Launch IntelliJ.

  2. Go to File → New → Project menu.

  3. Select the Java project and appropriate SDK and click on the Next button.

java project
  1. If you want to create a Java class with the main method, then select Create Project from the template checkbox.

  2. Select the command line app from the dialog box shown below and continue.

  3. Enter the project name and the directory location.

  4. Click on the Finish button.

Create Package

包在 Java 项目下创建,可以单独创建,也可以在创建类时创建。让我们遵循以下步骤来创建一个包:

A package is created under Java project and can be created separately, or at the same time of creating a class. Let us follow these steps to create a package −

  1. Go to the project perspective.

  2. Right-click on Project, select the New→Module option.

project perspective
  1. The new module window will be similar to the new project. Select the Java option and appropriate SDK and click on the Next button.

  2. Enter the module name.

  3. Click on the Finish button.

Create Java Class

在这一部分,我们将学习如何创建一个 Java 类。Java 类可以在 Java 模块下创建。遵循以下这些步骤来创建一个模块:

In this section, we will learn how to create a Java class. A Java class can be created under a Java module. Follow these steps to create a module −

  1. Go to the Project perspective.

  2. Expand Project and select the src directory from the module.

  3. Right click on it; select the New→Java Class option.

  4. Enter the class name in the dialog-box and click on the OK button.

  5. It will open the Editor window with the class declaration.

Run Java Application

现在我们将看到如何运行一个 Java 应用程序。遵循这些步骤并查看它是如何运行的:

We will now see how to run a Java application. Follow these steps and see how it runs −

  1. Let us write a simple code, which will print a message on the console. Enter the following code in the Editor window −

public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello, World !!!");
   }
}
  1. Go to the Run menu and select the Run option.

  2. Select the Class name and click on Run.

  3. If there are no compilation errors, then it will show output at the bottom of the window.

java application

Intellij Idea - Deep Dive

本教程的前四章旨在让初学者对 IntelliJ 有一个基础了解。本部分深入探讨 IntelliJ,并讨论更多有关项目、其格式以及其他内容。

The first four chapters of this tutorial were designed to give beginners a basic-level overview of IntelliJ. This section dives deep into IntelliJ and discusses more about projects, its format along with other things.

Understanding Projects

项目是指你正在开发的应用程序或软件。它可以包含多个模块、类、库、配置等等。它处于层次结构中的最顶端。

A project is an application or software on which you are working. It can contain multiple modules, classes, libraries, configuration, and so on. It is the topmost element in the hierarchy.

Understanding Modules

模块位于“项目”下列的一级。模块是一个独立的实体,可以独立于其他模块进行编译、调试和运行。单个项目可以包含多个模块。你可以随时添加或删除项目中的模块。

Modules feature one rung below ‘Project’. A module is a separate entity that can be compiled, debugged and run independently of other modules. A single project can contain multiple modules. You may add or remove modules from a project any time.

除此之外,我们还可以导入现有模块。请按照下列步骤导入现有模块 −

In addition to this, we can also import the existing modules. Follow these steps to import the existing modules −

  1. Go to File → Project structure.

  2. Select modules and click on plus icon.

  3. It will show the option to import module.

modules feature

Understanding Folders

Content root 是一个包含构成模块的所有文件的文件夹。一个模块可以有多个内容文件夹。文件夹分以下类型 −

Content root is a folder that contains all the files that make up your module. A module can have more than one content folder. Folders are categorized into the following types −

  1. Sources − By assigning this category to folder, we instruct IntelliJ that this and its subfolder contain java source code and it should be compiled as part of the compilation process.

  2. Tests − By assigning this category to folder, we instruct IntelliJ that it is the location for unit tests. This folder can access classes from the Sources folder.

  3. Resources − It represents various resource files used in project, for instance, images, XML and properties files. During the compilation process, contents of this folder is copied as is to the output folder.

  4. Excluded − Contents from the excluded folder will not be indexed by IntelliJ. This means that IntelliJ will not provide the code completion suggestion and other hints. For example, the output directory and the target directory are excluded by default.

  5. Test resources − This is similar to the resources and used for unit tests.

content root

Understanding Libraries

库是由不同类的编译组成的。库使代码可以重用。在 Java 中,库可以封装在 ZIP、Jar 或简单的文件夹中。我们可以分三个不同级别定义库。级别为 - 全局、项目和模块级别。

Library is a compilation of different classes. Library enables code reusability. In Java, library can be enclosed in ZIP, Jar or simply folder. We can define libraries at three different levels. The levels are - global, project and module level.

  1. Global level − Shared by all projects.

  2. Project level − Shared by all the modules of project.

  3. Module level − Shared by the classes of those modules.

Understanding Facets

构面是模块的扩展。它们增加对框架和技术的支持。将构面添加到模块时,IntelliJ 会识别出增加的支持。举例而言,编辑器中的提示和帮助、窗口栏中的新工具、依赖项下载等等。您可以从 File → Project Structure 窗口添加构面,如下所示:

Facets are the extensions to the modules. They add support to the frameworks and technologies. When a facet is added to a module, IntelliJ identifies it adds support. For instance, hints and help in editor, new tools in window bar, dependency downloading and so on. You can add facets from the File → Project Structure window as shown below −

window bar

Artifacts

构建产品是项目的输出。可以是简单的 JAR 文件、Java EE 应用程序或 Java EJB 应用程序。如果我们使用 Gradle 或 Maven 等外部构建工具,则 IntelliJ 会自动为它们添加构建产品。可以导航至 File → Project Structure 创建构建产品,如下所示:

Artifacts are the output of a project. It can be a simple JAR file, Java EE application, or Java EJB application. If we are using external build tools like Gradle or Maven, then IntelliJ will add artifact for them automatically. Artifacts can be created by navigating to the File → Project Structure as shown below −

artifacts

Importing Existing Project

在本节中,我们将了解如何导入现有项目。我们可以通过两种方式导入项目:

In this section, we will understand how to import an existing project. We can import a project in two ways −

  1. Import it from existing source

  2. Import it from the build model.

当前,它支持 Gradle 和 Maven 构建工具。要导入项目,请:

At present, it supports Gradle and Maven build tools. To import project −

  1. Navigating to File → New → Project from the existing source.

  2. Select directory of the existing project, Maven’s pom.xml or Gradle’s build script.

  3. Click on the OK button.

importing existing project

Project Formats

IntelliJ 支持两种项目格式,一种是 directory-based ,另一种是 file-based 。基于目录的格式较新,建议使用。默认情况下,IntelliJ 会创建基于目录的项目格式。您可以在创建新项目时选择项目格式。在新建项目窗口中,只需单击更多设置,如下面的图片所示:

IntelliJ supports two types of project format one is directory-based and other is file-based. Directory based format is newer one it is recommended. By default, IntelliJ creates directory based project format. You can select project format while creating new project. On new project window just click on more settings as show in below image −

project formats

Directory-based Project Format

此格式有助于在项目中创建一个 idea 文件夹,并将项目中的所有配置文件保留在此文件夹中。设置被分组到 xml 文件中。举例而言,它将创建 misc.xml, modules.xml, workspace.xml 等。以下屏幕截图将帮助您了解它的工作原理:

This format helps create an idea folder in your project and keep all configuration files inside that folder. Settings are grouped into the xml files. For instance, it will create misc.xml, modules.xml, workspace.xml and so on. The following screenshot will help you understand how this works −

directory based project format

File-based Project Format

它将创建两个项目文件,分别带有 ..iprwpr 扩展名。ipr 文件将包含特定于项目的设置,而 wpr 文件将包含特定于工作区设置。

It will create two project files with ..ipr and wpr extensions. The ipr file will contain project-specific settings and the wpr file will contain workspace-specific settings.

file based project format

要将基于文件的项目转换为基于目录的项目,请转至 File → Save as a Directory-Based format

To convert a file-based project to a directory-based project, go to the File → Save as a Directory-Based format.

Directory-based vs File-based Project Format

与基于文件的项目格式相比,基于目录的项目格式将设置存储在带有有意义名称的单独文件夹中。其它差异:

Compared to the file-based project format, the directory-based project format stores settings in a separate folder with meaningful names. Other differences are −

  1. Related settings getting stored in a single file makes it easier to manage in directory-based project format.

  2. If folder contains the idea subfolder then IntelliJ recognizes that project. Because of this, you don’t have select the ipr project explicitly.

  3. Directory-based project format breaks settings into multiple files hence it is easier to select particular type of setting to store in version control system.

Intellij Idea - Deep Dive into Editor

编辑器是开发人员花费其大部分时间的地方。精通编辑器是提高任何资源生产力的第一步。本章讨论了编辑器可视元素、最常见的编辑器操作及 SonarLint 插件,它提供了 lint 检查。

Editor is that area where a developer spends most of his/her time. Mastering the editor is the first step at improving the productivity of any resource. This chapter discusses visual elements of editor, the most common editor actions and SonarLint plug-in, which provides lint checking.

Visual Elements of Editor

IntelliJ 具备许多功能,其中包括旨在帮助开发人员浏览并了解其编码真实状态的可视元素。

IntelliJ has many provisions that include visual elements designed to assist developers in navigating through and understanding the real status of their coding.

现在让我们了解不同的功能 −

Let us now go through the different provision −

Gutter Area

编辑器小窗口区域位于 IDE 的左侧,如下图所示 −

Editor’s gutter area is located on the left side of IDE as shown in the following image −

gutter area

Labels

我们现在了解标签如何工作的。上面屏幕截图以数字标记。我们现在看看这些数字必须显示什么 −

We will now understand how the labels work. The above screenshot is marked in numbers. We will now see what the numbers have to show −

  1. Here we can see line numbers along with other useful options. If you observe carefully just before class name and main method there is a small green triangle. It provides option to run, debug and run application with coverage. Just click on the triangle icon and it will provide options.

  2. You can see the small plus symbol at line number 6. This is the code folding functionality. If you click on that icon, then it will unfold it. Code folding can be done at class, method, loop and other block levels.

  3. At line numbers 11 and 14, there are 2 arrows which point towards each other. The marker represents the method’s start and end position. If you click on any arrow, then it will perform the fold action on that code block. The Gutter area will show various symbols when certain actions are taken; for instance, it will show symbols for breakpoints, Java annotations.

Status Bar

在编辑器窗口的底部,有一个状态栏,它会显示有关当前文件和项目状态的信息。

At the bottom of the Editor window, there is a status bar, which shows information about the current file and the project status.

event log
status bar
  1. In the first image, 16:1 represents the current line number and the column number respectively.

  2. Next is the line separator, for UNIX and mac OS it’s \n and for windows it’s \r.

  3. UTF-8 represents the file encoding.

  4. Next is the lock icon. We can toggle file’s read-only attribute by clicking on it.

  5. At the end, there is a symbol with a man’s face. It allows managing the code inspection level. When we type code in Editor, IntelliJ inspects code on the fly and shows hints/suggestion. We can decide the inspection level from here. For instance, we can completely disable it or allow inspection only for syntax and so on. We can also enable the Power Save mode from here, which will disable background code analysis and other background jobs.

  6. The Status bar also shows information about project actions. For instance, second image show information about project compilation.

Scratches

IntelliJ 提供了一个临时编辑器。您可以在不修改当前项目结构的情况下创建文本或代码片段。它提供了两种类型的临时文件 -

IntelliJ provides a temporary Editor. You can create text or piece of code without modifying the current project structure. It provides two types of temporary files −

Scratch files

它们是可操作的、可运行的和可调试的。创建草稿文件 −

They are functional, run-able and debug-able. To create a scratch file −

  1. Go to File → New → Scratch file.

  2. Select the language of your choice.

  3. It will create a file in the IntelliJ-Installation-Directory\ config \scratches folder.

Scratch buffers

它仅用于创建任何文本。 若要创建草稿缓冲区:

This is used only for creating any text. To create a scratch buffer −

  1. Press Ctrl + Shift + A or click on Search Everywhere icon

  2. It will pop up dialog box.

  3. Enter new scratch buffer text in that box and press enter.

  4. It’ll open scratch buffer in editor window.

与草稿文件类似, scratch buffers 也存储在 IntelliJ-Installation-Directory\config\scratches 文件夹中。

Like the scratch files, scratch buffers are also stored in the IntelliJ-Installation-Directory\config\scratches folder.

Scrollbar

滚动条突出显示代码中的警告/错误消息/待办事项标记。例如,sayHello() 和 sayGoodBye() 方法从未使用过;因此,滚动条显示黄色标记。

Scrollbar highlights warnings/error messages/TODO markers from the code. For instance, the sayHello() and sayGoodBye() method is never used; hence, the scrollbar shows yellow marker for them.

scrollbar

在第 8 行,有待办事项注释,滚动条使用蓝色标记突出显示它。错误使用红色标记突出显示。

At line number 8, there is the TODO comment, scrollbar highlights it using the blue marker. Errors are highlighted using red marker.

Editor Actions

在本节中,我们将讨论 IntelliJ 中的编辑器操作。

In this section, we will discuss the Editor actions in IntelliJ.

Search and Replace

要搜索当前文件中的文本:

To search text in the current file −

  1. Navigate to the file in the Editor window and press Ctrl+F.

  2. It will show text box, type text to be searched there.

  3. You can provide various options here - case sensitive match, exact match, regular expression and so on.

editor actions

你可以在非常细化的级别执行搜索操作。例如,可以在项目、模块或目录级别进行搜索:

You can perform the search operation at a very granular level. For instance, search can be done at project, module or directory level −

  1. Press the Ctrl+Shift+F key combination.

  2. A new window will appear; from here, you can select the appropriate level.

new window

要对当前文件执行替换操作:

To perform a replace action in the current file −

  1. Press the Ctrl+R key combination.

  2. A dialog box will appear, enter the text to be replaced here.

  3. This operation allows you to replace single match, all matches or skip current match.

replace single

要执行粒度级别的替换操作 -

To perform the replace action at a granular level −

  1. Press the Shift+Ctrl+R key combination.

  2. It will allow you replace text at project, module, directory and other scopes.

Column Selection Mode

要启用列模式选择,在选择文本时按住 Alt 键。要永久启用列选择模式,选择 Edit → Column selection mode

To enable the column mode selection, hold the Alt key while selecting the text. To enable the column selection mode on a permanent basis, select Edit → Column selection mode.

column selection

Clipboard History

当我们复制任何文本时,它将进入剪贴板历史记录。IntelliJ 维护剪贴板历史记录。要查看此历史记录,请按 Ctrl+Shift+V 键组合。你可以从该窗口中选择要粘贴的内容。

When we copy any text, it goes to the clipboard history. IntelliJ maintains the clipboard history. To view this history, pressthe Ctrl+Shift+V key combination. You can select the content to paste from this window.

clipboard history

Code Completion

IntelliJ 会检查 code on the fly 并提供准确的代码完成。例如,当我键入单词 say 时 - 它会建议方法 sayHello()sayGoodBye()

IntelliJ inspects the code on the fly and provides accurate code completion. For instance, when I type the word say - it suggests sayHello() and sayGoodBye() methods.

code completion

代码补全在处理类对象时建议类属性和类方法。

Code completion suggests class attributes and class methods while working with the class objects.

Generating Code

IntelliJ 提供生成有用的代码(例如构造函数、getter、setter、toString() 方法、override 方法等)的一种方法。按照以下步骤操作以在编辑器窗口中右键单击生成。

IntelliJ provides a way to generate useful code like constructor, getter, setter, toString() method, override methods and so on. Follow these steps to generate right-click in the Editor window.

  1. Select the Generate option.

  2. It will list the methods for which code can be can generated.

generate option

Code Inspection

我们已经看到 IntelliJ 识别语法错误、警告消息和 TODO 标记。除此之外,它还会建议代码改进、识别死代码和代码优化。例如,在下面给出的代码中,flag 的值始终为 true。因而 else 部分将永远不会被执行。

We have already seen that IntelliJ identifies syntax errors, warning messages and TODO markers. In addition to this, it suggests code improvement, identifies dead code, code optimization. For instance, in the code given below, the value of flag is always true. Hence, the else part will never be executed.

boolean flag = true;
if (flag == true) {
   System.out.println("Value of flag is true");
} else {
   System.out.println("Value of flag is false");
}

IntelliJ 识别到这一点并建议移除此代码块。

IntelliJ identifies this and suggests removing this code block.

code inspection

Comparing Files and Folders

按照以下步骤比较文件和文件夹 -

Follow these steps to compare files and folders −

  1. Hold the Ctrl key and select files to be compared from the project perspective.

  2. Right-click on it and select the Compare Files option.

  3. It will open the Compare window where the changes are highlighted.

你也可以将从一个文件应用到另一个文件中的更改。如果你观察,有两个箭头标记以红色突出显示。单击它们以应用来自其他文件的更改。按 Ctrl+Z 撤消更改。

You can also apply the changes from one file to another. If you observe, there are two arrow markers highlighted in red color. Click on them to apply changes from other file. Press Ctrl+Z to undo the changes.

类似地,要比较文件夹,请从项目视角(而不是文件)选择文件夹。

Similarly, to compare the folders, select folders from project perspective instead of files.

compare files

Getting Help

获取代码详细信息非常有用,例如方法说明、其返回类型、参数类型和参数数量——IntelliJ 可在编辑器窗口本身中提供这些详细信息。

It is useful if you get details about code easily. Like the method description, its return type, arguments type and number of arguments - IntelliJ can provide these details in the Editor window itself.

Inline Documentation

顾名思义,IntelliJ 可以通过从代码提取文档来访问文档。如果您为代码提供 Java 文档,那么 IntelliJ 就能够显示有关该方法的帮助。要访问内联文档,请将光标悬停在方法名称上并按 Ctrl+Q 键组合。

As the name suggests, IntelliJ can access documentation by extracting it from the code. If you provide Java Docs for your code, then IntelliJ can show help about it. To access inline documentation, hover click on method name and press the Ctrl+Q key combination.

inline documentation

Viewing Definition

要查看定义,请将鼠标悬停在方法名称上并按 Ctrl+Shift+I 键组合。

To view definition, hover the mouse over method name and press the Ctrl+Shift+I key combination.

viewing definition

Show Usage

要查看方法用法,请单击方法声明/定义并按 Alt+Ctrl+F7 键组合。

To view method usage, click on method declaration/definition and press the Alt+Ctrl+F7 key combination.

show usage

View Parameters

要查看方法参数,请键入方法名称并按 Ctrl+P 键组合。

To view the method parameters, type method name and press the Ctrl+P key combination.

view parameters

Linting Code

Linting 是一个过程,其中 lint 工具分析源代码并报告与编码标准相关的潜在问题。它有助于提高代码质量。IntelliJ 支持可对代码进行 lint 的 SonarLint 插件。

Linting is a process in which the lint tool analyzes the source codes and reports potential issues related to the coding standard. It helps in improving the code quality. IntelliJ supports SonarLint plug-in which lints the code.

About SonarLint

安装并启用 SonarLint 插件后,当您在编辑器窗口中打开一个文件时,它将开始自动分析代码。它将在 SonarLint 面板中报告问题。我们将在本节中详细讨论它。

Once you install and enable the SonarLint plug-in, it will start analyzing the code automatically when you open a file in the Editor window. It will report issues in the SonarLint panel. We will discuss more about it in this section.

SonarLint 支持流行的编程语言,如 Java、JavaScript、PHP 和 Python。

SonarLint supports popular programming languages like Java, JavaScript, PHP and Python.

Installation

按照以下步骤安装 SonarLint −

Follow these steps to install SonarLint −

  1. Download SonarLint from here.

  2. Go to File → Settings → Select plugins.

  3. Click on the Install plugin from disk button.

  4. Follow on-screen installation to complete the installation procedure.

on screen installation

Code Analysis

安装并启用插件后,它将开始分析 code on the fly 。它将在 SonarLint 面板中报告问题。按照以下步骤查看当前文件的错误 −

Once the plug-in is installed and enabled, it will start analyzing code on the fly. It will report issues in the SonarLint panel. Follow these steps to view issues of the current file −

  1. Click on the Current file tab.

  2. It will report issues like unused local variables, unused class attributes, message logging and so on.

  3. To know more about issue, click on issue from SolarLint panel.

  4. In the right side of the window, it will show its severity as Major, Minor and so on.

severity as major

Do One Time Code Analysis

如果您不熟悉即时代码分析,那么您可以在准备好代码后禁用它并执行一次代码分析。按照以下步骤禁用即时代码分析 −

If you are not comfortable with on-the-fly code analysis, then you can disable it and perform one time code analysis once you are ready with code. Follow these steps to disable on-the-fly code analysis −

  1. Go to File → Settings → Other Setting → SonarLint General Settings

  2. Uncheck Automatically Trigger Analysis checkbox from the settings tab.

  3. Click on Apply followed by Ok button.

automatically trigger analysis

Intellij Idea - Code Refactoring

在本章中,我们将介绍代码重构以及它在 IntelliJ 中的工作原理。代码重构是对代码进行重组,而无需更改其功能和可用性。可以对代码进行重构以提高代码可读性、性能或移除未使用/重复的功能。IntelliJ 为代码重构提供了强大的支持。本章讨论了各种代码重构操作。

In this chapter, we will learn about Code Refactoring and how it works in IntelliJ. Code refactoring is restructuring of code without changing its functionality and usability. Code refactoring can be done to improve code readability, performance or to remove unused/duplicate functionality. IntelliJ provides great support for code refactoring. This chapter discusses various code refactoring actions.

Rename

重命名操作可用于重命名方法、其参数、类属性、局部变量等。我们来在 IntelliJ 中创建以下类。

Rename actions can be used to rename methods, its parameters, class attributes, local variables and so on. Let us create the following class in IntelliJ.

public class Employee {
   private String name;
   private String address;
   private int age;
   public Employee() {
      this("Jarvis", "Palo Alto", 35);
   }
   public Employee(String name, String address, int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }

   @Override
   public String toString() {
      return "Employee{" +
      "name='" + name + '\'' +
      ", address='" + address + '\'' +
      ", age=" + age +
      '}';
   }
   public static void main(String args[]) {
      Employee e = new Employee();
      System.out.println(e);
   }
}

现在,让我们将 Employee 类重命名为 Person 。此操作将在构造函数和 main() 方法中进行修改 -

Now, let us rename Employee class to Person. This action will do modifications in constructors and the main() method −

  1. Select Employee word

  2. Go to Refactor → Rename and rename it with Person.

rename actions

Replace Code Duplicates

这是强大的重构操作之一。IntelliJ 识别代码重复并用适当的代码替换它。让我们引入代码重复并对其进行重构。在编辑器中键入以下代码 -

This is one of the powerful refactoring actions. IntelliJ identifies code duplicates and replaces it with appropriate code. Let us introduce code duplication and refactor it. Type the following code in the Editor −

public class Employee {
   private String name;
   private String address;
   private int age;
   public Employee() {
      this("Jarvis", "Palo Alto", 35);
   }
   public Employee(String name, String address, int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   public void setData(String name, String address,  int age) {
      this.name = name;
      this.address = address;
      this.age = age;
   }
   public void showEmployeeDetail() {
      System.out.println("Name = " + name + ", Address = " + address + ", Age = " + age);
   }
   public static void main(String args[]) {
      Employee e = new Employee();
      e.showEmployeeDetail();
   }
}

在此示例中,Employee(String name, String address, int age) 构造函数和 public void setData(String name, String address, int age) 方法完全相同。重构后,Employee(String name, String address, int age) 构造函数会修改为以下内容 -

In this example, Employee(String name, String address, int age) constructor and public void setData(String name, String address, int age) method are exactly identical. After refactoring, the Employee(String name, String address, int age) constructor gets modified as follows −

public Employee(String name, String address, int age) {
 setData(name, address, age);
}

替换重复部分 -

To replace the duplicates −

  1. Go to Refactor → Find and Replace Code Duplicates.

  2. Select refactor scope and follow on-screen steps to complete action.

replace code duplicates

Copy Refactoring

在本节中,我们将了解如何将一个类复制到另一个类。让我们将 Employee 类复制到 Person 类。我们可以将其复制到现有模块或新建模块。IntelliJ 将根据它进行必要的更改。按照以下步骤执行复制重构 -

In this section, we will understand how to copy one class to another. Let us copy Employee class to Person class. We can copy it to the existing module or a new one. IntelliJ will do the required changes depending on it. Follow these steps to perform copy refactoring −

  1. Go to Refactor → Copy, it will open the dialog box.

  2. Enter new name and destination package.

  3. Click on the OK button and it will do the needful.

copy class

Move Refactoring

移动重构类似于复制,但它不是制作另一个副本,而是将代码移动到另一个包或使其成为另一个类的内部类。

Move refactoring is similar to copy but instead of making another copy it moves the code to a different package or make it as inner class of another class.

按照以下步骤执行移动重构 -

Follow these steps to perform move refactoring −

  1. Go to, Refactor → Move.

  2. A new window will appear.

  3. Select one of the options according to your choice and click on Refactor.

move refactoring

Safe Delete

仅当对象在项目中任何地方都没有被引用时,安全删除操作才会删除该对象。此选项的目标可以是类、接口、方法、字段或参数。

The Safe Delete action will delete object only when it is not referenced anywhere in the project. The target for this option can be class, interface, method, field or parameter.

让我们看看实际操作。在编辑器中输入以下代码 −

Let us see this in action. Type the following code in Editor −

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

按照以下步骤执行安全删除操作 −

Follow these steps to perform the safe delete action −

  1. Select the sayHello() method.

  2. Right-click on it and select the Refactor → Safe Delete option.

  3. As the sayHello() method is being used it will show an error as in the following screenshot −

safe delete action

Change Signature

该操作修改方法签名。它可以更改方法名称、参数、类型、返回值等等。让我们从上面的示例中获取一个方法并更改其签名。

The action modifies method signature. It can change the method name, its parameters, types, return values and so on. Let us take a method from the above example and change its signature.

按照以下步骤执行更改签名操作 −

Follow these steps to perform the Change Signature action −

  1. Select method.

  2. Right-click on it and select the Refactor → Change signature action

  3. A new window will appear wherein you can perform the above actions.

  4. At the bottom of the window, it shows the preview of new signature.

change signature

Type Migration

类型迁移更改符号的类型。此符号可以是方法参数或类属性。让我们在执行所需操作之前考虑以下方法 −

The Type Migration changes the type of the symbol. This symbol can be a method parameter or class attribute. Let us consider the following method before performing the required action −

static void sayHello(String name) {
   System.out.println(name);
}

按照以下步骤执行类型迁移 −

Follow these steps to perform type migration −

  1. Select the “String” data type.

  2. Right-click on it and select Refactor → Type migration.

type migration
  1. Enter the required data type in the given text box.

  2. Choose scope and click on the Refactor button.

Intellij Idea - Running Projects

  1. IntelliJ 提供多种创建配置的方法。本章讨论了创建临时和永久配置的选项。还讨论了在其他用户之间共享配置的方法。

IntelliJ provides multiple ways to create configuration. This chapter discusses the options to create temporary, permanent configuration. It also discusses method to share configuration among other users.

Create Temporary Configuration

  1. 当您运行或调试 Java 类或测试用例时,将创建临时配置。考虑以下步骤来了解其工作原理:——

Temporary configuration is created when you run or debug Java class or test case. Consider the following steps to understand how this works −

  1. Create a Java class.

  2. Right-click on it and select the Run option.

  3. After the first Run, temporary configuration is added to the run menu.

temporary configuration

Create Permanent Configuration

  1. 通过使用 IntelliJ 保存临时配置,可以将其转换为永久配置。要保存配置,请从运行菜单中单击 Save Configuration 选项:——

Temporary configuration can be converted to permanent configuration by saving it with IntelliJ. To save configuration, click on the Save Configuration option from the Run menu −

permanent configuration
  1. 您还可以通过编辑来修改现有配置。

You can also modify the existing configuration by editing it.

Create New Configuration

  1. 在本节中,我们将了解如何创建新配置。按照以下步骤创建新配置:——

In this section, we will understand how to create new configuration. Follow these steps to create new configuration −

  1. Navigate to the Run → Edit Configuration.

  2. Click on the Plus button to add new configuration.

  3. Select Application from the dropdown list.

new configuration
  1. It will create un-named configuration.

  2. Configure it according to your requirements and click on the OK button.

  3. This saved configuration will be accessible from the Run menu along with other configurations.

Sharing the Configuration

按照以下步骤与他人共享配置 −

Follow these steps to share configuration with others −

  1. Navigate to the Run → Edit Configuration.

  2. Select configuration from the left pane and click on the Share checkbox.

  3. It will store configuration on disk.

如果使用基于目录的格式,它会将配置单独保存在 runConfiguration 文件夹下的文件中。否则,它会将配置保存在 .ipr 文件中。

If directory based format is used, it will save configuration in separate file under runConfiguration folder. Otherwise, it will store configuration in the .ipr file.

share configuration

Run Configuration

按照以下步骤使用特定配置运行项目 −

To run project with specific configuration −

  1. Select configuration from the Run menu.

  2. Run the project as shown in the following screenshot −

run configuration

Intellij Idea - Build Tools

IntelliJ 提供了一种方法来构建和打包 Java 包。它支持 Maven 和 Gradle 等外部构建工具。本章将讨论这些构建工具。

IntelliJ provides a way to build and package Java package. It supports external build tools like Maven and Gradle. This chapter discusses about these build tools.

Creating Maven Project

按照以下步骤创建 Maven 项目 −

Follow these steps to create a Maven project −

  1. Navigate to File → Project.

  2. Select Maven option and click on Next button.

maven project
  1. In the new project window enter tutorialspoint.com as GroupId and HelloWorld as ArtifactId.

  2. In the New window, it will open the pom.xml file.

  3. We need to add properties to this file; the final pom.xml file should look like this −

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
      http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoing</groupId>
   <artifactId>HelloWorld</artifactId>
   <version>1.0-SNAPSHOT</version>
   <properties>
      <maven.compiler.source>1.7</maven.compiler.source>
      <maven.compiler.target>1.7</maven.compiler.target>
   </properties>
</project>

现在,让我们在 Maven 项目的 src/main/java 目录内创建一个 Java 类。按照以下步骤创建该类 −

Now, let us create a Java class inside the src/main/java directory of the Maven project. Follow these steps to create the class −

  1. Navigate to the src/main/java directory.

  2. Right click on it and select New → Java Class.

按照以下步骤使用 Maven 编译此类 −

Follow these steps to compile this class using Maven −

  1. Navigate to Run → Edit Configuration.

  2. Click on the green plus icon and select the Maven option from the dropdown menu.

  3. Enter the project name as Maven-Package.

  4. Provide package as the command line.

  5. Click on the OK button.

command line
  1. Navigate to Run and select the Maven-Package option.

  2. It will start building package. Upon successful building of the package, you will see the following result −

building package

Create Gradle Project

在本节中,我们将学习如何创建 Gradle 项目 −

In this section, we will learn how to create a Gradle project −

  1. Navigate to File → Project and select Gradle.

  2. Click on the Next button.

  3. In the new project window, enter tutorialspoint as GroupId and HelloWorld as ArtifactId.

  4. Click on the Next button, verify the project details and click on the Finish button.

  5. Follow the on-screen instructions to complete the process.

finish button
  1. Open the buildl.gradle file and write Hello task as shown in the above screenshot.

  2. To compile this task, navigate to the Run → Build-Gradle option.

Intellij Idea - Unit Testing

单元测试在软件开发中起着重要作用。IntelliJ 支持各种单元测试框架,如 JUnit、TestNG、Spock 等。在本章中,我们将使用 JUnit3。

Unit testing plays an important role in software development. IntelliJ supports various unit testing frameworks like JUnit, TestNG, Spock and many more. In this chapter, we are going to use JUnit3.

Create Unit test

在本节中,我们将学习如何创建单元测试。按照以下步骤创建测试 −

In this section, we will learn how to create a Unit Test. Follow these steps to create the test −

  1. Select the Navigate → Test option.

  2. A dialog box will appear wherein, you have to select Create New Test.

  3. Follow the on-screen instructions to continue −

create unit test
  1. Provide the details about the test like testing library, class details, setUp, tearDown methods and so on.

tear down methods
  1. Click on the OK button to continue.

  2. A test class will be created. Initially it may fail to resolve some symbols. Navigate the cursor to the error line, it will show the hint symbol.

  3. Select the appropriate option from the dropdown menu. We have selected the Add library ‘junit.jar!’ to classpath option −

classpath option

你可以根据业务要求在每个测试中添加逻辑。为了简单起见,我已将此项保留为空。

You can add logic inside each test according to your business requirement. I have kept it empty for simplicity.

Run Unit Test

按照以下步骤来运行单元测试 -

Follow these steps to run unit test −

  1. Select unit test class from the Editor window.

  2. Navigate to the Run menu and select the Run option.

  3. The following result will be generated

run option

Intellij Idea - Debugging

调试器可以使应用程序调试更为容易。通过使用调试器,可以停止程序在某个时间点的执行、检查变量、深入某项功能、并执行许多操作。IntelliJ 提供内置的 Java 调试器。

Debugger makes application debugging much easier. Using debugger, we can stop the execution of program at a certain point, inspect variables, step into function and do many things. IntelliJ provides inbuilt Java debugger.

Breakpoints

断点允许停止程序在某个时间点的执行。可以通过将鼠标悬停在编辑器的边距区域上并单击该区域来设置断点。

Breakpoint allows stopping program execution at certain point. Breakpoints can be set by hovering the mouse over the Editor’s gutter area and clicking on it.

以红色圆圈符号表示断点。考虑在第 3 行设置断点。

Breakpoints are denoted using red circle symbols. Consider the breakpoint set at line 3.

breakpoint

考虑以下步骤来进一步了解断点的工作方式 -

Consider the following steps to understand more on how the breakpoints work −

  1. Right-click on the red circle symbol.

  2. Select the More options.

  3. To remove breakpoint just click on same symbol.

按照以下步骤来启动调试器 -

Follow these steps to start the debugger −

  1. Navigate to the Run menu.

  2. Select the Debug option.

Step into

在调试过程中,如果遇到一个函数并且选择了进入操作,则调试器将如同该函数启用了调试那样在该函数的每个点停止程序执行。

While debugging, if a function is encountered and a step into action is selected, then debugger will stop program execution at each point of that function as if debugging is enabled for that function.

例如,当程序执行到达第 9 行并且如果我们选择进入操作时,它将在 sayGoodBye() 函数中的每行停止执行。

For instance, when program execution reaches at line 9 and if we select the step into action then it stops the execution at each line in the sayGoodBye() function.

program execution

Step out

“跳出”操作与“进入”操作完全相反。例如,如果您在上述场景中执行“跳出”操作,那么调试器将返回 sayGoodBye() 函数并在第 10 行开始执行。

The Step out action is exactly the reverse of Step in action. For instance, if you perform the step out action with the above scenario then debugger will return from the sayGoodBye() method and start execution at line 10.

step out

Step over

“跨步执行”操作不会进入函数,而是会跳转到代码的下一行。例如,如果您在第 9 行并执行“跨步执行”操作,那么它将把执行移动到第 10 行。

The Step over action does not enter into function instead, it will jump to the next line of code. For instance, if you are at line 9 and execute the step over action then it will move execution to line 10.

step over

Resume Program

“继续程序”操作将通过忽略所有断点来继续执行程序。

The Resume Program action will continue execution of program by ignoring all breakpoints.

breakpoints

Stop action

“停止”操作有助于停止调试器。

The Stop action helps stop the debugger.

stop action

Smart step into

在调试过程中,我们有时可能会到达调用多个方法的一行代码。在调试这些代码行时,调试器通常允许我们使用 step into 并引导我们遍历所有子函数,然后返回到父函数。但是,如果我们只想进入一个子函数怎么办?使用智能单步进入,它允许我们选择要进入的函数。

While debugging, we may sometimes reach a line of code that calls several methods. When debugging these lines of code, the debugger typically allows us to use step into and leads us through all child functions and then back to the parent function. However, what if we only wanted to step into one child function? With Smart step-into, it allows us to choose the function to step into.

现在,让我们用以下代码行创建一个 Java 类:

Now, let us create a Java class with the following line of code −

public class HelloWorld {
   public static void main(String[] args) {
      allFunctions();
   }
   static void allFunctions() {
      System.out.println(function1() + " " + function2() + " " + function3());
   }
   static String function1() {
      return "function1";
   }
   static String function2() {
      return "function2";
   }
   static String function3() {
      return "function3";
   }
}

在上面的代码中, allFunctions() 调用了 3 个附加函数。让我们在这个函数中设置断点。按照以下步骤执行智能单步进入:

In the above code, allFunctions() calls 3 more functions. Let us set the breakpoint at this function. Follow these steps to perform smart step into −

  1. Go to run

  2. Select smart step into.

  3. Select the child function to go.

child function

Inspecting variables

在调试过程中,IntelliJ 在编辑器窗口本身中显示变量的值。我们还可以在调试窗口中查看相同的信息。

During debugging, IntelliJ shows value of variable in the Editor window itself. We can also view the same information in the Debug window.

inspecting variables

Evaluate expression

“求值表达式”允许动态求值表达式。按照以下步骤执行此操作:

Evaluate expression allows to evaluate expression on the fly. Follow these steps to perform this action −

  1. Start application in debugger

  2. Navigate to Run→Evaluate expression.

  3. Enter expression. In the example given below, the current value of variable ‘i’ is 0; hence, expression ‘i > 100’ will evaluate to false

evaluate expression

Intellij Idea - Profiling

Profiler 会提供有关您的应用程序的信息,例如它的 CPU、内存和堆栈使用情况。它还会提供有关应用程序线程的详细信息。本章讨论 VisualVM 工具用于 Java 应用程序分析的用法。它可以分析 CPU 和堆栈等实体。

Profiler gives insights about your application like its CPU, memory and heap usage. It also gives details about the application threads. This chapter discusses the usage of VisualVM tool for Java application profiling. It can profile entities such as CPU and heap.

建议本教程的读者熟悉应用程序分析器概念。

It is recommended that the readers of this tutorial are familiar with the application profiler concepts.

Introduction

VisualVM 是一款可视化工具,它集成了 JDK 工具并为您提供了强大的分析功能。它允许您生成和分析堆数据,追踪内存泄漏,监控垃圾回收,并执行内存和 CPU 分析。

VisualVM is a visual tool that integrates JDK tools and gives you powerful profiling capabilities. It allows you to generate and analyze heap data, track down memory leaks,monitor the garbage collector and perform memory and CPU profiling.

Benefits

  1. Visual interface for local and remote Java applications running on JVM.

  2. Monitoring of application’s memory usage and application’s runtime behavior.

  3. Monitoring of application threads

  4. Analyzing the memory allocations to different applications.

  5. Thread dumps − very handy in case of deadlocks and race conditions.

  6. Heap dumps − very handy in analyzing the heap memory allocation.

Configuration

在本部分中,我们将学习配置 VisualVM 所执行的步骤。步骤如下:

In this section, we will learn the steps performed to configure VisualVM. The steps are as follow −

  1. Download it from here.

  2. Extract the zip file.

  3. Navigate to etc/visualvm.conf file and add the following line in this file −

visualvm_jdkhome=<path of JDK>
  1. If your JDK is installed in the C:\Program Files\Java\jdk-9.0.1 directory then it should look like this −

visualvm_jdkhome="C:\Program Files\Java\jdk-9.0.1"

Monitoring application

现在让我们看看如何监视应用程序。根据以下步骤来理解相同的操作:

Let us now see how to monitor the application. Consider the following steps to understand the same −

  1. Double-click on the visualvm.exe file.

  2. Select the application from left pane.

  3. Select the monitor tab.

monitoring application

您将被定向到一个窗口,您将在其中获取有关 CPU、堆、类和线程的详细信息。具体来说,将鼠标悬停在任何图表上方即可。我们在上面的屏幕截图中可以看到堆的使用率。

You will be directed to a window where you will get the details about CPU, Heap, Classes and threads. To be specific with the usage, hover the mouse over any graph. We can see the usage of Heap in the above screenshot.

Insights about Threads

Java 应用程序可以包含多个执行线程。要了解有关线程的更多信息,请选择特定应用程序的“线程”选项卡。它将提供有关线程的各种统计信息,例如活动线程数和守护线程数。不同的线程状态有:正在运行,正在休眠,正在等待,正在停放和正在监视。

Java application can contain multiple threads of execution. To know more about threads, select the Threads tab of a particular application. It will give various statistics about threads like number of live threads and daemon threads. The different thread states are Running, Sleeping, Waiting, Park and Monitor.

insights threads

Sampling Application

VisualVM 支持 CPU、内存采样和内存泄漏检测。要对应用程序进行采样,请选择应用程序并选择“采样”选项卡:

VisualVM supports CPU, memory sampling and memory leak detection. To sample application, select application and choose the sample tab −

CPU sampling

对于 CPU 采样,点击 CPU 按钮,如下面屏幕截图所示:

For CPU sampling, click on the CPU button as show in the following screenshot −

memory sampling

Memory profiling

对于内存分析,点击内存按钮,如下面屏幕截图所示:

For memory profiling, click on the Memory button as shown in the following screenshot −

memory profiling

Memory leaks

当应用程序在运行时,缓慢地用程序不会自动删除的对象填充堆时,就会发生内存泄漏。

A memory leak occurs when an application, while running, slowly fills up the heap with objects that are not automatically deleted by the program.

如果程序不使用的对象未被删除,那么它将保留在内存中,GC 无法回收其空间。如果应用程序中的字节数和实例数持续大幅增加,以至于用尽所有空间,则这可能是内存泄漏的征兆。

If an object that is not used by the program is not deleted, then it remains in memory and the GC cannot reclaim its space. If the number of bytes and number of instances in your application were to increase constantly and significantly in your program to the point of using up all the space, this can be an indication of a memory leak.

Profiling application

在本部分中,我们将学习如何对应用程序进行分析。要分析应用程序,请从左侧窗格中选择应用程序,然后单击“分析”选项卡:

In this section, we will learn how to profile an application. To profile an application, select application from left pane and click the profile tab −

CPU profiling

要执行 CPU 分析,请单击屏幕截图所示的 CPU 按钮:

To perform CPU profiling, click on the CPU button as shown in the screenshot below −

cpu profiling

Memory profiling

要执行 CPU 分析,请单击屏幕截图所示的 CPU 按钮:

To perform CPU profiling, click on the CPU button as shown in the screenshot below −

cpu memory profiling

Intellij Idea - Version Control

IntelliJ 支持各种版本控制系统,如 Git、Subversion、Mercurial、CVS、GitHub 和 TFS。您可以直接从 IDE 执行与版本控制相关的操作。

IntelliJ supports various version control systems like Git, Subversion, Mercurial, CVS, GitHub and TFS. You can perform version control related action from the IDE itself.

在本章中,我们将讨论 Git 和 Subversion(以下简称 SVN)。我们假设读者熟悉 Git 和 SVN 工具及其术语。

In this chapter, we will discuss Git and Subversion (hereafter referred to as SVN). We assume that the reader is familiar with Git and SVN tool and its terminology.

Git

在本节中,我们将了解如何使用 Git。

In this section, we will learn how to work with Git.

Clone

要克隆现有的 Git 仓库 −

To clone an existing Git repository −

  1. Navigate to File→New→Project from Version Control→Git.

  2. Enter the repository URL, Parent directory and Directory name.

  3. Click on the clone button to continue.

clone repository
  1. Upon successful running of the above steps, the repository will get cloned.

Track changes

Git 将跟踪你在仓库中所做的更改。让我们修改仓库中的任何文件,并将其与仓库进行比较。

Git will track the changes that you make in repository. Let us modify any file from the repository and compare it with the repository.

  1. Navigate to VCS → Git → Compare with Latest Repository Version.

  2. The above step will open the diff window.

  3. You can see there is a new line on the right side with green background colour.

  4. Git shows it in green as we have added new contents. If we remove any contents then it’ll be shown in red colour

track changes

Revert local changes

按照以下步骤放弃本地更改 −

Follow these steps to discard the local changes −

  1. Navigate to the VCS → Git → Revert option.

  2. It will ask for confirmation and remove your changes.

revert changes

Add file to repository

要将文件添加到仓库,请导航至 VCS → Git → Add option 。此操作类似于 git add 操作。

To add file to repository navigate to VCS → Git → Add option. This action is similar to the git add action.

Commit changes

提交操作将创建本地提交。它类似于 git commit 操作。要执行提交 −

The Commit operation will create local commit. It is similar to the git commit action. To perform commit −

  1. Navigate to the VCS → Git → Commit File option.

  2. Select files to be committed.

  3. Enter commit message and click on Commit button.

commit changes

Push changes to the remote repository

推送操作会将本地更改发送至远程存储库。要推送更改 −

The Push action will send local changes to the remote repository. To push changes −

  1. Navigate to the VCS → Git → Push option.

  2. A window will appear. Here, you can see the comitts to be pushed.

  3. Verify commit and click on the Push button to publish your changes.

push changes

View history or logs

要显示历史记录,请导航至 VCS → Git → Show history option 。此操作类似于 git log 命令。它将按如下所示显示历史记录 −

To show history, navigate to the VCS → Git → Show history option. This action is similar to the git log command. It will show history as follows −

view history or logs

Get updates

按照以下步骤从存储库中获取更新 −

Follow these steps to fetch updates from the repository −

  1. Navigate to the VCS → Git → Pull option.

  2. Select the option according to your requirements.

  3. Click on the Pull button.

get updates

Add existing project to Git

要在 Git 下添加现有项目 −

To add existing project under Git −

  1. Navigate to VCS → Import into Version Control → Create Git repository.

  2. Select project by browsing directory.

  3. Click on the OK button.

existing project

Subversion

在本节中,我们将了解 Subversion 在 IntelliJ 中的工作原理。让我们考虑几个重要的操作来理解这一点。

In this section, we will understand how Subversion works in IntelliJ. Let us consider a few important actions to understand this.

Checkout

签出 SVN 存储库 −

To checkout SVN repository −

  1. Navigate to File → New → Project from Version Control → Subversion.

  2. Enter repository URL.

  3. Click on the OK button.

checkout

Track changes

SVN 将跟踪您在存储库中所做的更改。让我们修改存储库中的任何文件,并将其与存储库进行比较 −

SVN will track changes you made in the repository. Let us modify any file from repository and compare it with repository −

  1. Navigate to VCS→Subversion→Compare with Latest Repository Version

  2. You can see there is new line on right side with green background colour.

  3. SVN shows it in with green background to indicated the addition of new content. If we remove any content then it will be shown in red colour.

latest repository

Revert local changes

按照以下步骤还原您所做的本地更改 −

Follow these steps to revert the local changes you have made −

  1. Navigate to the VCS → Subversion → Revert option.

  2. It will ask for confirmation and remove your changes.

remove your changes

Commit changes to remote repository

按照以下步骤提交远程仓库中的更改 −

Follow these steps to commit changes in the remote repository −

  1. Navigate to VCS → Subversion → Commit Files option.

  2. A new window will appear. Here, you can see the files to be committed to remote respository.

  3. Verify the files and click on the Commit button to publish your changes.

commit button

View history or logs

若要显示历史记录,请导航至 VCS → Subverion → Show history 选项。此选项类似于 svn log 命令。它将按以下方式显示历史记录 −

To show history, navigate to the VCS → Subverion → Show history option. This option is similar to the svn log command. It will show history as follows −

history navigate

Update operation

要获取最新更改,请导航至 VCS → Subversion → Update File/Update Folder 选项。

To fetch latest changes navigate to VCS → Subversion → Update File/Update Folder option.

update operation

Add existing project to Subversion

按照以下步骤在 SVN 下添加现有项目 −

Follow these steps to add existing project under SVN −

  1. Navigate to VCS → Import into Version Control → Import into Subverion.

  2. Enter the repository URL and click on the Import button −

add existing project

Intellij Idea - Databases

IntelliJ 提供数据库工具,允许您直接在 IDE 中执行与数据库相关的操作。它支持所有主要数据库,如 MySQL、Oracle、Postgress、SQL server 和更多数据库。本章将讨论 IntelliJ 如何支持 MySQL 数据库。

IntelliJ provides database tool which allows you to perform database related operation from the IDE itself. It supports all major databases like MySQL, Oracle, Postgress, SQL server and many more. In this chapter, we will discuss how IntelliJ supports MySQL database.

我们假设读者熟悉数据库概念,并且系统中已安装并配置好所需的数据库工具。

We assume that the reader is familiar with the database concepts and the required databases’ tools are installed and configured on your system.

Create Database

首先,我们将创建一个数据库 - test_db 。在命令提示符中执行以下命令 −

To begin with, we will create a database - test_db. Execute the following command in the command prompt −

create database

Connect to Database

按照以下步骤连接到数据库 −

Follow these steps to connect to a Database −

  1. Navigate to View → Tool Windows → Database.

  2. Click on the green plus icon and select Data Source → MySQL.

connect to a database
  1. Enter the host address and click on the Test Connection button.

  2. If everything goes well then it’ll show Successful as shown in above image.

  3. Click on OK button to save connection.

Create table

按照这些步骤创建新表:

Follow these steps to create a new table −

  1. Right-click on the database pane and select schema.

  2. Select the New → Table option

create table
  1. A new window will appear. Define the table with columns, types and so on.

table with columns
  1. Click on the Execute button

  2. Click on the Execute button

Insert Data

按照这些步骤插入数据:

Follow these steps to insert data −

  1. Select table from the database pane.

  2. It will open table in the new window.

  3. Click on the plus icon to insert new row.

  4. Click on the Submit button to make the changes permanent.

insert data

Retrieve Data

要检索数据,从数据库窗口双击 student_table。它将在新窗口中显示表数据。

To retrieve data, double-click on student_table from the database pane. It will show table data in new window.

要确保已将数据插入表中,请打开命令提示符并执行以下命令:

To ensure that the data is inserted into the table, open the command prompt and execute the following commands −

retrieve data

Intellij Idea - Migrating from NetBeans

NetBeans 是另一个流行的 Java IDE。如果你当前是 NetBeans 的用户,并且想要从 NetBeans 迁移到 IntelliJ,那么这里将是一个不错的起点。

NetBeans is another popular Java IDE. If you are a current user of NetBeans and want to migrate from it to IntelliJ then this will serve as a good starting point.

本章讨论了在 IntelliJ 中导入 NetBeans 项目及其与 NetBeans 等效的术语、流行的快捷键和常见问题。

This chapter discusses the importing of NetBeans projects in IntelliJ, its terminologies equivalent to NetBeans, popular shortcuts and frequently asked questions.

Import NetBeans project

在本节中,我们将学习如何导入 NetBeans 项目。按照以下步骤导入该项目:

In this section, we will learn how to import NetBeans project. Follow these steps to import the project −

  1. Navigate to File → New → Project from Existing Sources

  2. Select your NetBeans project directory.

  3. When the Import Project wizard opens, select the Create project from existing sources option.

  4. Follow the on-screen instructions to continue.

IntelliJ vsNetBeans terminology

下表比较了 IntelliJ 和 NetBeans 的术语:

The following table compares IntelliJ and NetBeans terminology −

IntelliJ

NetBeans

Module

Project

Global library

Global library

Module library

Project library

Module dependency

Project dependency

Module SDK

Project-specific SDK

IntelliJ 是一款以键盘为中心的 IDE。它为大多数操作提供了快捷键。下表列出一些重要的快捷键:

IntelliJ is a keyboard-centric IDE. It provides shortcuts for most of the actions. The following table lists a few important shortcuts −

Action

Shortcut

Run Java program

ctrl+shift+F10

Organize imports

ctrl+alt+o

System.out.println()

Type sout and press ctrl+j

Delete current line

ctrl + y

Search

ctrl + shift + f

Generate getter and setter

alt + insert

Format code

ctrl + alt + l

Comment out code

ctrl + /

Go to line

ctrl + g

Go to declaration

ctrl + b

Rename

shift+F6

Move lines

ctrl + shift + up/down

Debugger shortcuts

下表列出了几个重要的调试器快捷键:

The following table lists down a few important debugger shortcuts −

Debug action

Shortcut

Debug a program

Shift + F9

Choose configuration and debug

Shift+Alt+F9

Step over

F8

Step into

F7

Smart step into

Shift + F7

Step out

Shift + F8

Force step over

Shift+Alt+F8

Force step into

Shift+Alt+F7

Resume program

F9

Evaluate expression

Alt+F8

Toggle breakpoints

Ctrl+F8

View breakpoints

Ctrl+Shift+F8

FAQsand Tips

在本节中,我们将学习一些常见问题解答和提示。常见问题解答和提示如下:

In this section, we will go through a few Frequently Answered Questions and Tips. The FAQs and tips are as follows −

Can I use the NetBeans key bindings in IntelliJ IDEA?

  1. Navigate to Files → Settings and select Keymap.

  2. Select NetBeans from the drop down box

drop down box

Is local history in IntelliJ IDEA different from that in NetBeans?

通常,IntelliJ IDEA 中的本地历史更详细。任何关于目录、文件、类、方法或字段或代码块的操作都会反映在你的本地历史中。本地历史还包括 VCS 操作。

Local history in IntelliJ IDEA, generally, is more detailed. Whatever you do with a directory, file, class, method or field, or a code block is reflected in your local history. The local history also includes VCS operations.

Can I enable ‘compile on save' in IntelliJ IDEA?

  1. Navigate to File → Settings → Build, Execution, Deployment → Compiler

  2. Select Build Project Automatically option.

  3. Click on the OK button.

build project automatically

Can I use NetBeans plugins in IntelliJ IDEA?

不行,你不能。

No, you cannot.

Is it possible to build NetBeans RCP applications with IntelliJ IDEA?

这是有可能的;但是,你无法获得与使用 NetBeans 所获得的相同类型支持(向导、菜单操作等)。要了解更多详细信息,请访问 this.

It is possible; however, you will not get the same kind of support that you get with NetBeans (wizards, menu actions, etc.). For more details, visit this.

Intellij Idea - Migrating from Eclipse

Eclipse 是另一种流行的 Java IDE。如果您是 Eclipse 的现用用户,并想从其迁移到 IntelliJ,那么这是一个很好的起点。

Eclipse is yet another popular Java IDE. If you are a current user of Eclipse and want to migrate from it to IntelliJ, then this is a good starting point.

本章讨论了如何在 IntelliJ 中导入 Eclipse 项目、它的 Eclipse 等价术语、常用快捷方式和常见问题。

This chapter discusses how to import Eclipse projects in IntelliJ, its terminologies equivalent to Eclipse, popular shortcuts and frequently asked questions.

Import existing project

在本节中,我们将讨论如何导入现有项目。请按以下步骤导入项目:

In this section, we will discuss how to import an existing project. Follow these steps to import the project −

  1. Navigate to File → New → Project from Existing Sources.

  2. Select your NetBeans project directory.

  3. When the Import Project wizard opens, select the Create project from existing sources option.

  4. Follow the on-screen instructions to continue.

IntelliJ vsEclipse terminology

下表比较了 IntelliJ 和 NetBeans 术语 -

The following table compares IntelliJ and NetBeans terminologies −

IntelliJ

Eclipse

Project

Workspace

Module

Project

Facet

Facet

Library

Library

SDK

JRE

Path variable

Classpath variable

IntelliJ 是一个以键盘为中心的 IDE。它为大多数操作提供了快捷键。下表列出了一些常用快捷键 -

IntelliJ is a keyboard-centric IDE. It provides shortcuts for most of the actions. The following table lists a few popular shortcuts −

Action

Shortcut

Run java program

ctrl+shift+F10

Organize imports

ctrl+alt+o

System.out.println()

Type sout and Press ctrj+j

Delete current line

ctrl + y

Search

ctrl + shift + f

Generate getter and setter

alt + insert

Format code

ctrl + alt + l

Comment out code

ctrl + /

Go to line

ctrl + g

Go to declaration

ctrl + b

Rename

shift+F6

Move lines

ctrl + shift + up/down

Debugger shortcuts

下表列出了一些常用的调试快捷键 -

The following table lists down commonly used debugger shortcuts −

Debug action

Shortcut

Debug a program

Shift + F9

Choose configuration and debug

Shift+Alt+F9

Step over

F8

Step into

F7

Smart step into

Shift + F7

Step out

Shift + F8

Force step over

Shift+Alt+F8

Force step into

Shift+Alt+F7

Resume program

F9

Evaluate expression

Alt+F8

Toggle breakpoints

Ctrl+F8

View breakpoints

Ctrl+Shift+F8

FAQsand Tips

在此部分中,我们会看到一些常见问题和提示。常见问题及提示如下 -

In this section, we will see a few Frequently Answered Questions and tips. The FAQs and tips are as follows −

Use Eclipse Compiler

Eclipse 使用它自己的编译器,而 IntelliJ IDEA 使用与项目 JDK 捆绑在一起的 javac 编译器。如果你想使用 Eclipse 编译器 -

While Eclipse uses its own compiler, IntelliJ IDEA uses the javac compiler bundled with the project JDK. If you want to use the Eclipse compiler −

  1. Navigate to File → Settings → Build, Execution, Deployment → Compiler → Java Compiler.

  2. Select the required compiler from User compiler dropdown.

eclipse compiler

Eclipse Keymap

对于不想学习新快捷键的 Eclipse 用户,IntelliJ IDEA 提供了 Eclipse 快捷键映射,它非常类似其快捷键 -

For Eclipse users who prefer not to learn new shortcuts, IntelliJ IDEA provides the Eclipse keymap that closely mimics its shortcuts −

  1. Navigate to File → Settings → Keymap option.

  2. Select Eclipse from Keymap dropdown.

eclipse keymap

Code formatting

若要导入你的 Eclipse 格式化程序设置 -

To import your Eclipse formatter settings −

  1. Navigate to File → Settings → Editor → Code Style → Java.

  2. Select the Eclipse XML profile.

eclipse formatter settings

Working with Build Tools

与 Eclipse 类似,IntelliJ 并不为编辑 Maven/Gradle 配置文件提供可视化形式。

Like Eclipse, IntelliJ does not provide visual forms for editing Maven/Gradle configuration files.

一旦你导入/创建了你的 Maven/Gradle 项目,你就可以直接在文本编辑器中编辑其 pom.xml/build.gradle 文件。

Once you have imported/created your Maven/Gradle project, you are free to edit its pom.xml/build.gradle files directly in the text editor.