Create your first Java application
In this tutorial, you will learn how to create, run, and package a simple Java application that prints Hello World! to the system output. Along the way, you will get familiar with IntelliJ IDEA features for boosting your productivity as a developer: coding assistance and supplementary tools.
Prepare a project
Create a new Java project
In IntelliJ IDEA, a project helps you organize your source code, tests, libraries that you use, build instructions, and your personal settings in a single unit.
- Launch IntelliJ IDEA. If the Welcome screen opens, click New Project . Otherwise, go to File | New Project .
- In the New Project wizard, select New Project from the list on the left.
- Name the project (for example HelloWorld ) and change the default location if necessary.
- We’re not going to work with version control systems in this tutorial, so leave the Create Git repository option disabled.
- Make sure that Java is selected in Language , and IntelliJ is selected in Build system .
- To develop Java applications in IntelliJ IDEA, you need the Java SDK ( JDK ). If the necessary JDK is already defined in IntelliJ IDEA, select it from the JDK list. If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory (for example, /Library/Java/JavaVirtualMachines/jdk-20.0.1.jdk ).
If you don’t have the necessary JDK on your computer, select Download JDK . In the next dialog, specify the JDK vendor (for example, OpenJDK), version, change the installation path if required, and click Download . 
- Leave the Add sample code option disabled as we’re going to do everything from scratch in this tutorial. Click Create .
After that, the IDE will create and load the new project for you.
Create a package and a class
Packages are used for grouping together classes that belong to the same category or provide similar functionality, for structuring and organizing large applications with hundreds of classes.
- In the Project tool window, right-click the src folder, select New (or press Alt+Insert ), and then select Java Class .
- In the Name field, type com.example.helloworld.HelloWorld and click OK . IntelliJ IDEA creates the com.example.helloworld package and the HelloWorld class.
Together with the file, IntelliJ IDEA has automatically generated some contents for your class. In this case, the IDE has inserted the package statement and the class declaration.
This is done by means of file templates. Depending on the type of the file that you create, the IDE inserts initial code and formatting that is expected to be in all files of that type. For more information about how to use and configure templates, refer to File templates.
The Project tool window Alt+1 displays the structure of your application and helps you browse the project.
In Java, there’s a naming convention that you should follow when you name packages and classes.
Write the code
Add the main() method using live templates
- Place the caret at the class declaration string after the opening bracket < and press Shift+Enter . In contrast to Enter , Shift+Enter starts a new line without breaking the current one.
- Type main and select the template that inserts the main() method declaration. As you type, IntelliJ IDEA suggests various constructs that can be used in the current context. You can see the list of available live templates using Control+J .
Live templates are code snippets that you can insert into your code. main is one of such snippets. Usually, live templates contain blocks of code that you use most often. Using them can save you some time as you don’t have to type the same code over and over again.
For more information about where to find predefined live templates and how to create your own, refer to Live templates.
Call the println() method using code completion
After the main() method declaration, IntelliJ IDEA automatically places the caret at the next line. Let’s call a method that prints some text to the standard system output.
- Type Sy and select the System class from the list of code completion suggestions (it’s from the standard java.lang package). Press Control+. to insert the selection with a trailing period.
- Type o , select out , and press Control+. again.
- Type p , select the println(String x) method, and press Enter . IntelliJ IDEA shows you the types of parameters that can be used in the current context. This information is for your reference.
- Type » . The second quotation mark is inserted automatically, and the caret is placed between the quotation marks. Type Hello World!
Basic code completion analyzes the context around the current caret position and provides suggestions as you type. You can open the completion list manually by pressing Control+Space .
For more information about different completion modes, refer to Code completion.
Call the println() method using a live template
You can call the println() method much quicker using the sout live template.
After the main() method declaration, IntelliJ IDEA automatically places the caret at the next line. Let’s call a method that prints some text to the standard system output.
- Type sout and press Enter .
- Type » . The second quotation mark is inserted automatically, and the caret is placed between the quotation marks. Type Hello World! .
Build and run the application
Valid Java classes can be compiled into bytecode. You can compile and run classes with the main() method right from the editor using the green arrow icon in the gutter.
- Click in the gutter and select Run ‘HelloWorld.main()’ in the popup. The IDE starts compiling your code.
- When the compilation is complete, the Run tool window opens at the bottom of the screen. The first line shows the command that IntelliJ IDEA used to run the compiled class. The second line shows the program output: Hello World! . And the last line shows the exit code 0 , which indicates that it exited successfully. If your code is not correct, and the IDE can’t compile it, the Run tool window will display the corresponding exit code.
When you click Run , IntelliJ IDEA creates a special run configuration that performs a series of actions. First, it builds your application. On this stage, javac compiles your source code into JVM bytecode.
Once javac finishes compilation, it places the compiled bytecode to the out directory, which is highlighted with yellow in the Project tool window.
After that, the JVM runs the bytecode.
Automatically created run configurations are temporary, but you can modify and save them.
If you want to reopen the Run tool window, press Alt+4 .
IntelliJ IDEA automatically analyzes the file that is currently opened in the editor and searches for different types of problems: from syntax errors to typos. The Inspections widget in the top-right corner of the editor allows you to quickly see all the detected problems and look at each problem in detail. For more information, refer to Current file.
Package the application in a JAR
When the code is ready, you can package your application in a Java archive (JAR) so that you can share it with other developers. A built Java archive is called an artifact .
Create an artifact configuration for the JAR
- Go to File | Project Structure ( Control+Alt+Shift+S ) and click Artifacts .
- Click , point to JAR and select From modules with dependencies .
- To the right of the Main Class field, click and select HelloWorld (com.example.helloworld) in the dialog that opens. IntelliJ IDEA creates the artifact configuration and shows its settings in the right-hand part of the Project Structure dialog.
- Click Apply to save the changes and close the dialog.
Build the JAR artifact
- Go to Build | Build Artifacts .
- Point to HelloWorld:jar and select Build .
If you now look at the out/artifacts folder, you’ll find your JAR there. 
Run the packaged application
To make sure that the JAR artifact is created correctly, you can run it.
Use Find Action Control+Shift+A to search for actions and settings across the entire IDE.
Create a run configuration for the packaged application
To run a Java application packaged in a JAR, IntelliJ IDEA allows you to create a dedicated run configuration.
- Press Control+Shift+A , find and run the Edit Configurations action.
- In the Run/Debug Configurations dialog, click and select JAR Application .
- Name the new configuration: HelloWorldJar .
Run configurations allow you to define how you want to run your application, with which arguments and options. You can have multiple run configurations for the same application, each with its own settings.
Execute the run configuration
- On the toolbar, select the HelloWorldJar configuration and click to the right of the run configuration selector. Alternatively, press Shift+F10 if you prefer shortcuts. As before, the Run tool window opens and shows you the application output.
The process has exited successfully, which means that the application is packaged correctly.
Tutorial: Your first Java EE application
This tutorial describes how to create a simple Java EE web application in IntelliJ IDEA. The application will include a single JSP page that shows Hello, World! and a link to a Java servlet that also shows Hello, World! .
You will create a new Java Enterprise project using the web application template, tell IntelliJ IDEA where your GlassFish server is located, then use a run configuration to build the artifact, start the server, and deploy the artifact to it.
Here is what you will need:
IntelliJ IDEA Ultimate
Java Enterprise development is not supported in the free IntelliJ IDEA Community Edition. For more information, refer to IntelliJ IDEA Ultimate vs IntelliJ IDEA Community Edition
Relevant bundled plugins
By default, all necessary plugins are bundled and enabled in IntelliJ IDEA Ultimate. If something does not work, make sure that the following plugins are enabled:
- Jakarta EE Platform
- Jakarta EE: Application Servers
- Jakarta EE: Web/Servlets
- GlassFish
For more information, refer to Install plugins.
Java SE Development Kit (JDK) version 1.8 or later
You can get the JDK directly from IntelliJ IDEA as described in Java Development Kit (JDK) or download and install it manually, for example: Oracle JDK or OpenJDK.
The GlassFish application server version 4.0 or later. You can get the latest release from the official repository. The Web Profile subset should be enough for the purposes of this tutorial.
This tutorial uses Oracle OpenJDK 17, Jakarta EE 9.1, and GlassFish 6.2.5. For more information about the compatibility between other GlassFish, Java, and Jakarta EE versions, refer to https://github.com/eclipse-ee4j/glassfish#compatibility.
You will need a web browser to view your web application.
Create a new Java Enterprise project
IntelliJ IDEA includes a dedicated wizard for creating Java Enterprise projects based on various Java EE and Jakarta EE implementations. In this tutorial, we will create a simple web application.
- Go to File | New | Project .
- In the New Project dialog, select Jakarta EE .
- Enter a name for your project: JavaEEHelloWorld .
- Select the Web application template, Maven as a build tool, and use Oracle OpenJDK 17 as the project SDK. Don’t select or add an application server, we will do it later. Click Next to continue.

- In the Version field, select Jakarta EE 9.1 because that’s what GlassFish 6.2.5 used in this tutorial is compatible with. For GlassFish 5, select the Java EE 8 specification. For GlassFish 7, select Jakarta EE 10. In the Dependencies list, you can see that the web application template includes only the Servlet framework under Specifications .

- Click Create .
Explore the default project structure
IntelliJ IDEA creates a project with some boilerplate code that you can build and deploy successfully.
-
pom.xml is the Project Object Model with Maven configuration information, including dependencies and plugins necessary for building the project.
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
package com.example.demo; import java.io.*; import javax.servlet.http.*; import javax.servlet.annotation.*; @WebServlet(value = «/hello-servlet») public class HelloServlet extends HttpServlet < private String message; public void init() < message = "Hello World!"; >public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException < response.setContentType("text/html"); // Hello PrintWriter out = response.getWriter(); out.println("«); out.println(«
» + message + «
«); out.println(««); > public void destroy() < >>
Use the Project tool window to browse and open files in your project or press Control+Shift+N and type the name of the file.
Configure GlassFish
IntelliJ IDEA can build and deploy your application’s artifacts if you specify the location of your server. For this tutorial, you should have the GlassFish server installed. You can download GlassFish from the official project website.

- Press Control+Alt+S to open the IDE settings and then select Build, Execution, Deployment | Application Servers .
- Click and select Glassfish Server .
- Specify the path to the GlassFish server install location. IntelliJ IDEA detects and sets the name and version appropriately.
Create a GlassFish run configuration
IntelliJ IDEA needs a run configuration to build the artifact and deploy it to your application server.
- Go to Run | Edit Configurations .
- In the Run/Debug Configurations dialog, click , expand the Glassfish Server node, and select Local .
- Fix any warnings that appear at the bottom of the run configuration settings dialog.
Most likely, you will need to fix the following:
- On the Server tab, set the Server Domain to domain1 .
- On the Deployment tab, add the artifact that you want to deploy: JavaEEHelloWorld:war exploded
- On the Server tab, set the URL to http://localhost:8080/JavaEEHelloWorld-1.0-SNAPSHOT/ and save the run configuration.

- To run the configuration, press Alt+Shift+F10 and select the created GlassFish configuration.
This run configuration builds the artifact, then starts the GlassFish server, and deploys the artifact to the server. You should see the corresponding output in the Run tool window.

Once this is done, it opens the specified URL in your web browser.

Modify the application
Whenever you change the source code of the application, you can restart the run configuration to see the changes. But this is not always necessary, especially when you can’t restart the server. Most of the changes are minor and don’t require rebuilding the artifacts, restarting the server, and so on. Let’s change the JSP page of the application.
- Open index.jsp and change the greeting from Hello, World! to A better greeting. .
- In the Run tool window, click or press Command F10 .
- In the Update dialog, select Update resources because the JSP page is a static resource. Click OK .
- Refresh the application URL in your web browser to see the new string: A better greeting.
You can configure the default update action in the run configuration settings: go to Run | Edit Configurations . Change the On ‘Update’ action option under the Server tab of the GlassFish run configuration settings.

With the On frame deactivation option, you can configure to update your resources and classes without redeploying and restarting the server whenever you change focus from IntelliJ IDEA. In this case, you won’t even have to use the Update Application action, just switch to your web browser and refresh the page.
Package the application into a WAR and deploy it on a running server
In the previous steps, we deployed the application using an exploded artifact, where all files are uncompressed. This is useful during the first stages of development because it allows you to update individual resources and classes without redeploying. When you are happy with your application and ready to share it with others by deploying to a remote server, it is better to use the compressed web archive (WAR) format.
Let’s add a remote GlassFish run configuration to deploy the WAR artifact to a running server. This assumes that you did not terminate the GlassFish instance from the previous steps.
- Go to Run | Edit Configurations .
- In the Run/Debug Configurations dialog, click , expand the GlassFish Server node, and select Remote .
- Change the name of this run configuration to distinguish it, for example: Remote GlassFish 4.1.1 .
- Open the Deployment tab, click above the table of artifacts to deploy, and select Artifact . Select to deploy the JavaEEHelloWorld:war artifact and click OK .

- Click OK to save the remote run configuration.
- Open index.jsp and change the greeting to Hello from WAR! .
- Select the new run configuration in the main toolbar and click or press Shift+F10 .

The new configuration builds the WAR artifact and deploys it to the running server. Refresh the URL http://localhost:8080/JavaEEHelloWorld-1.0-SNAPSHOT/ and see the new greeting: Hello from WAR!
Package the application into an EAR
Proper enterprise applications are packaged into EAR files that can contain both WAR and JAR files. Let’s see how to do this in IntelliJ IDEA.
- In the Project tool window Alt+1 , select the top project directory.
- Press Control+Shift+A and type Add Framework Support . Once the action is found, click it to open the Add Framework Support dialog.
- In the Add Framework Support dialog, select JavaEE Application under Java EE and click OK . IntelliJ IDEA adds the META-INF/application.xml file in your module. This is the deployment descriptor for your application.
- Press Control+Alt+Shift+S to open the Project Structure dialog. On the Artifacts page, select the new JavaEEHelloWorld:ear exploded artifact and note that it contains only the javaEEApplication facet resource.
- Expand the Artifacts element under Available Elements and double-click JavaEEHelloWorld:war to add it to the EAR artifact structure.
When you see a message that says Web facet isn’t registered in application.xml , click Fix . - Click , select Java EE Application: Archive , then click For ‘JavaEEHelloWorld:ear exploded’ .

- Select the new EAR artifact and click Create Manifest .
Specify the default location under META-INF , next to application.xml . - Open application.xml . It should contain the following:
Deploy the EAR artifact
Deploying the EAR artifact is similar to deploying the WAR: you need a remote GlassFish run configuration.
- Go to Run | Edit Configurations .
- In the Run/Debug Configurations dialog, click , expand the GlassFish Server node, and select Remote .
- Change the name of this run configuration to distinguish it, for example: Remote EAR GlassFish 6.2.5 .
- Open the Deployment tab, click under the table of artifacts to deploy, and select Artifact . Select to deploy the JavaEEHelloWorld:ear artifact and click OK .
- Click OK to save the remote run configuration.
- Open index.jsp and change the greeting to Hello from EAR! .
- Select the new run configuration in the main toolbar and click or press Shift+F10 .
The new configuration builds the EAR artifact and deploys it to the running server. Refresh the URL http://localhost:8080/JavaEEHelloWorldWeb/ and see the new greeting: Hello from EAR! . Note that the URL corresponds to the context-root specified in application.xml .
Troubleshooting
If you get a 404 error, make sure you have selected the Jakarta EE specification version that is compatible with your version of GlassFish when creating the project.
For more information, refer to the GlassFish version compatibility.
What next?
In this tutorial, we created and deployed a simple Java enterprise application. To extend this knowledge, you can create a RESTful web service as described in Tutorial: Your first RESTful web service.
Как в IntelliJ IDEA открыть декомпилированный байт-код?
ну как человек разбирающийся, ответьте на вторую половину вопроса, хотя бы как имхо: Может ли измениться поведение программы после переоткрытия из байткода? Да или НЕТ?
11 окт 2017 в 13:21
Intellij IDEA это просто как пример, можно воспользоваться Eclipse, он тоже так умеет.
Вообще компиляция происходит вызовом компилятора javac он находится в папке jdk\bin , вызываете с путём к файлу исходного кода, например C:\jdk1.8.0_144\bin\javac.exe Main.java , если у вас Window , jdk при этом находится там, куда вы его установили и может иметь другую версию. Рядом с файлом Main.java появится файл Main.class, который можно открыть как файл в IDEA
Но вообще, чтобы просто посмотреть исходный байт код, достаточно создать Java проект в IDEA , скомпилировать и открыть папку куда скомпилировался файл

Отслеживать
ответ дан 11 окт 2017 в 10:48
12.1k 2 2 золотых знака 25 25 серебряных знаков 47 47 бронзовых знаков
извиняйте, не понятно. Можете переписать так: 1. Набираю код в Intellij Idea. Сохраняю проект. 2. Открываю командную строку. Набираю команды . 3. Файл сохранился в директории . 4. Открываю это из директории С:\ . 5. Смотрю, код изменился. PROFIT!
Import and export projects
This option imports the selected project to IntelliJ IDEA as is (opens it). If you want to set custom settings while importing the project (for example, select another SDK or choose the libraries that you want to import), refer to Create a project from existing sources.

- Launch IntelliJ IDEA. If the Welcome screen opens, click Open . Otherwise, go to File | Open .
- In the dialog that opens, select the directory in which your sources, libraries, and other assets are located and click Open .
- When you import or clone a project for the first time, IntelliJ IDEA analyzes it. If the IDE detects more than one configuration (for example, Eclipse and Gradle), it prompts you to select which configuration you want to use. If the project that you are importing uses a build tool, such as Maven or Gradle, we recommend that you select the build tool configuration. Select the necessary configuration and click OK . The IDE pre-configures the project according to your choice. For example, if you select Gradle , IntelliJ IDEA executes its build scripts, loads dependencies, and so on.
- If you have been working with another project, select whether you want to open the new project in a new dialog or in the current one.
For more information about getting a project from version control, refer to Check out a project from a remote host (git clone).
Import a project with settings
This section describes the functionality that is available out of the box. If you are using a framework plugin, refer to the corresponding documentation section.
Import a project from an external model
Use this type of import if your project comes from an external model, and you want to import it as a whole. In this case, IntelliJ IDEA interprets the project files (for example, your Eclipse project will be migrated to IntelliJ IDEA).
- Launch IntelliJ IDEA. If the Welcome screen opens, press Control+Shift+A , type project from existing sources , and click the Import project from existing sources action in the popup. Otherwise, go to File | New | Project from Existing Sources .

- In the dialog that opens, select the directory in which your sources, libraries, and other assets are located and click Open .
- Select the external model that your project uses:
- Eclipse
- Maven
- Gradle: select the necessary build tool and click Finish . For Maven and Gradle projects, the IDE configures the settings automatically. You will be able to adjust them after the project is imported.

Create a project from existing sources
Use this type of import to create an IntelliJ IDEA project over the existing source code that is not necessarily an exported project.
- Launch IntelliJ IDEA. If the Welcome screen opens, press Control+Shift+A , type project from existing sources , and click the Import project from existing sources action in the popup. Otherwise, go to File | New | Project from Existing Sources .

- In the dialog that opens, select the directory in which your sources, libraries, and other assets are located and click Open .
- Select the Create project from existing sources option and click Next .

- Specify the name and location and select a format for the new project. It’s recommended that you use the directory-based format. Click Next . If you are importing the project to the same directory, the IDE asks you whether you want to overwrite it. If you click Yes , IntelliJ IDEA will overwrite the files in .idea directory and the .iml files, your source files will remain intact.

- Select the directories that you want to use as source root directories (folders with your source code) and click Next .

- Select the libraries that you want to add to the new project. You can join several selected libraries or archives into a new library by clicking or split the selected library into two by clicking . Click Next

- Review module structure: select the modules that you want to include in your project. You can merge several modules into one by clicking or split the selected module into two by clicking . Click Next
- Specify the SDK that you want to use. If the necessary SDK is already defined in IntelliJ IDEA, select it from the list on the left. Otherwise, click and add a new SDK. Click Next .

- Enable support for the detected frameworks and technologies: select checkboxes next to the necessary items. You can also specify how the files-indicators should be grouped: by type (by framework) or by directory (by location).
- Click Finish .
Export a project
You can save a project as a .zip archive or export it to Eclipse.
Save a project as a .zip file
The option to export a project to a .zip file is available if the Android bundled plugin is enabled.
- Go to File | Export | Project to Zip File .
- In the dialog that opens, specify the path to which you want to save the .zip file with the project and click Save .
Your project will be saved to the specified location as a .zip archive.
