IntelliJ IDEA 2017.2 Help

Java SE

The following basic how tos are intended to help you get started using Java in IntelliJ IDEA.

Moving source files into a subfolder

You shouldn't keep your source (.java) files in the project root directory. If this is, currently, the case, create a subdirectory in the project root directory and move all your source files into that subdirectory.

Enabling coding assistance for .java files

Coding assistance for Java turns on automatically as soon as you mark the folder with your.java files as containing source code. In the Project tool window (View | Tool Windows | Project), right-click the folder, point to Mark Directory as and select Sources Root.

Making the Java API accessible to your code

When writing in Java, you, generally, reuse (i.e. reference) the Java API classes. These classes are available in a JDK. So, to make the Java API accessible to your code, you should download and then specify your JDK:

  1. Open your.java file in the editor: select the file in the Project tool window and press F4.

    If you haven't specified your JDK yet, there will be a Project SDK is not defined message above the editing area.

  2. Click Setup SDK.
  3. In the dialog that opens, select your JDK and click OK, or click Configure, click /help/img/idea/2017.2/add.png, select JDK, and then select the directory where you have the desired JDK installed.

Creating a folder structure for your package or specifying a package prefix

If you don't have an appropriate folder structure for your package yet (let's assume that the name of your package is com.example.mypackage), you have two options leading to about the same result:

  • Create the corresponding folder structure in your source directory (e.g. <your-source-dir>/com/example/mypackage):

    In the editor, within the package statement (e.g. package com.example.mypackage;), place the cursor within the package name, press Alt+Enter and select Move to package 'com.example.mypackage'.

  • Assign your source folder a package prefix without actually creating the folder structure:
    1. Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S), select Modules and select your module.
    2. In the right-hand part of the Sources tab, next to your source folder, click /help/img/idea/2017.2/rootPrefix.gif.
    3. In the dialog that opens, specify the prefix (e.g. com.example.mypackage).

Making classes in a JAR accessible to your app

Say, you have a JAR with the classes that you want to reuse in your app. In that case, you should add the JAR to the dependencies of your module. As a result, the JAR classes become available for referencing in your code, and they are included in your app when you build it.

  1. Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S), select Modules and select your module.
  2. Select the Dependencies tab, click /help/img/idea/2017.2/add.png and select JARs or directories.
  3. In the dialog that opens, select your JAR file.

Compiling .java files

If you did everything as discussed earlier on this page, compiling your.java file or files is not going to be a problem. Just select one of the following options: Build | Build Project or Build | Build Module 'module_name'.

If compilation is problematic, IntelliJ IDEA will inform you about the reason and provide hints for fixing the problem.

Running your app

If the compilation is a success, you can run your app:

  1. Open the class with a main() method in the editor.
  2. In the left margin, next to the main() method, click the run marker (the green arrow) and select Run '<ClassName>.main()'.

Packaging your app in a JAR

To package your app in a JAR, you should configure a JAR artifact and then build it:

  1. Open the Project Structure dialog (Ctrl+Shift+Alt+S) and select Artifacts.
  2. Click /help/img/idea/2017.2/add.png, select JAR and select From modules with dependencies.
  3. In the dialog that opens, select the class with a main() method (the Main Class field).
  4. Save the artifact settings by clicking OK in the Project Structure dialog.
  5. Select Build | Build Artifacts, select your artifact and select Build. (If there's only one artifact, you can just press Enter when the Build Artifact menu pops up.)
Last modified: 11 October 2017

See Also