Intellij Idea − Getting Familiar

Intellij Idea – Getting Familiar ”; Previous Next 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. 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 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. Menu bar − The Menu bar provides options to create new projects and other important actions related to projects like code refactoring, builds, run, debug, version-controlling options and so on. Tool bar − The Tool bar provides shortcuts to compile, debug and run options. You can customize it according to your requirements. Navigation bar − The Navigation bar enables easier navigation within a project. This feature comes handy as code base increases. Tools tab − The Tools tab shows on either side of the main window. From here, you can access important tools like databases; Maven/Ant builds and so on. Project perspective − Project perspective window shows various elements of projects like packages, modules, classes, external libraries and so on. Editor window − This is a place where developer spends most of his/her time. Editor window allows you edit/write code with syntax highlighting and other useful features. 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 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. Steps to manage plug-ins Follow these steps to manage plug-ins − Go to the File → Settings menu. Select the Plugins tab on the left pane. 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. 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. 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 This section provides a few important tips to manage settings. It enables you to import, export and share IntelliJ settings. Export Settings It allows exporting the current settings as a jar file. Go to File → Export Settings. Export setting windows list the available settings related to UI, debugger, SDK along with others. 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 It allows importing the settings stored in the jar file. Go to File → Import settings. Select the Setting jar by navigating folder structure Click on the OK button. Sharing Settings 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 − Go to File → Settings → Plugins Search settings repository plug-in We can store the current setting on GitHub/Bitbucket and apply them on other instances. To store the current setting − Go to the File → Settings Repository. Type the Git repository URL in upstream URL dialog box. Click on the Overwrite remote button. To apply the same settings to other instances, click on the Overwrite local button. Configuring JVM Options and Platform Properties 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. –server − It allows the selection of the Java HotSpot Server VM -Xms<size> − It sets the initial Java heap size. The default value is 128 MB. -Xmx<size> − It sets the maximum Java heap size. The default value is 750 MB. -Xss<size> − It sets the Java thread stack size. –XX − It allows setting GC algorithm and other properties. Print Page Previous Next Advertisements ”;

Intellij Idea − Build Tools

Intellij Idea – Build Tools ”; Previous Next 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 Follow these steps to create a Maven project − Navigate to File → Project. Select Maven option and click on Next button. In the new project window enter tutorialspoint.com as GroupId and HelloWorld as ArtifactId. In the New window, it will open the pom.xml file. 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> Now, let us create a Java class inside the src/main/java directory of the Maven project. Follow these steps to create the class − Navigate to the src/main/java directory. Right click on it and select New → Java Class. Follow these steps to compile this class using Maven − Navigate to Run → Edit Configuration. Click on the green plus icon and select the Maven option from the dropdown menu. Enter the project name as Maven-Package. Provide package as the command line. Click on the OK button. Navigate to Run and select the Maven-Package option. It will start building package. Upon successful building of the package, you will see the following result − Create Gradle Project In this section, we will learn how to create a Gradle project − Navigate to File → Project and select Gradle. Click on the Next button. In the new project window, enter tutorialspoint as GroupId and HelloWorld as ArtifactId. Click on the Next button, verify the project details and click on the Finish button. Follow the on-screen instructions to complete the process. Open the buildl.gradle file and write Hello task as shown in the above screenshot. To compile this task, navigate to the Run → Build-Gradle option. Print Page Previous Next Advertisements ”;

Intellij Idea – Discussion

Discuss Intellij Idea ”; Previous Next IntelliJ IDEA (hereafter referred as IntelliJ) is one of the most powerful and popular Integrated Development Environments (IDE) for Java. It was developed and is maintained by JetBrains, and is available in community and ultimate edition. This feature-rich IDE enables rapid development and helps in improving code quality. This tutorial starts with a basic introduction and slowly dives deep into the advanced features. The tutorial is divided into 2 parts: beginners can start from the first 4 chapters. Others can skip over directly to Chapter-5. Print Page Previous Next Advertisements ”;

Intellij Idea − Deep Dive

Intellij Idea – Deep Dive ”; Previous Next 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 − Go to File → Project structure. Select modules and click on plus icon. It will show the option to import module. Understanding Folders 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 − 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. 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. 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. 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. Test resources − This is similar to the resources and used for unit tests. Understanding Libraries 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. Global level − Shared by all projects. Project level − Shared by all the modules of project. Module level − Shared by the classes of those modules. Understanding Facets 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 − Artifacts 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 − Importing Existing Project In this section, we will understand how to import an existing project. We can import a project in two ways − Import it from existing source Import it from the build model. At present, it supports Gradle and Maven build tools. To import project − Navigating to File → New → Project from the existing source. Select directory of the existing project, Maven’s pom.xml or Gradle’s build script. Click on the OK button. Project Formats 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 − Directory-based Project Format 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 − File-based Project Format 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. 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 − Related settings getting stored in a single file makes it easier to manage in directory-based project format. If folder contains the idea subfolder then IntelliJ recognizes that project. Because of this, you don’t have select the ipr project explicitly. 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. Print Page Previous Next Advertisements ”;

Intellij Idea − Profiling

Intellij Idea – Profiling ”; Previous Next 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 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 Visual interface for local and remote Java applications running on JVM. Monitoring of application’s memory usage and application’s runtime behavior. Monitoring of application threads Analyzing the memory allocations to different applications. Thread dumps − very handy in case of deadlocks and race conditions. Heap dumps − very handy in analyzing the heap memory allocation. Configuration In this section, we will learn the steps performed to configure VisualVM. The steps are as follow − Download it from here. Extract the zip file. Navigate to etc/visualvm.conf file and add the following line in this file − visualvm_jdkhome=<path of JDK> If your JDK is installed in the C:Program FilesJavajdk-9.0.1 directory then it should look like this − visualvm_jdkhome=”C:Program FilesJavajdk-9.0.1″ Monitoring application Let us now see how to monitor the application. Consider the following steps to understand the same − Double-click on the visualvm.exe file. Select the application from left pane. Select the monitor tab. 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 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. Sampling Application VisualVM supports CPU, memory sampling and memory leak detection. To sample application, select application and choose the sample tab − CPU sampling For CPU sampling, click on the CPU button as show in the following screenshot − Memory profiling For memory profiling, click on the Memory button as shown in the following screenshot − 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. 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 To perform CPU profiling, click on the CPU button as shown in the screenshot below − Memory profiling To perform CPU profiling, click on the CPU button as shown in the screenshot below − Print Page Previous Next Advertisements ”;