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 ( ), 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 (that is, 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:
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.
Click Setup SDK.
In the dialog that opens, select your JDK and click OK, or click Configure, click , 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 (for example <your-source-dir>/com/example/mypackage):
In the editor, within the package statement (for example
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:
Open the Project Structure dialog (for example Ctrl+Shift+Alt+S), select Modules and select your module.
In the right-hand part of the Sources tab, next to your source folder, click .
In the dialog that opens, specify the prefix (for example
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.
Select the Dependencies tab, click and select JARs or directories.
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:or .
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:
Open the class with a main() method in the editor.
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:
Open the Project Structure dialog Ctrl+Shift+Alt+S and select Artifacts.
Click , select JAR and select From modules with dependencies.
In the dialog that opens, select the class with a main() method (the Main Class field).
Save the artifact settings by clicking OK in the Project Structure dialog.
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.)