Creating an application using java

How to create a basic application in Java Spring Boot

Spring MVC is a widely used module of spring which is used to create scalable web applications. But the main disadvantage of spring projects is that configuration is really time-consuming and can be a bit overwhelming for the new developers. Making the application production-ready takes some time if you are new to the spring. The solution to this is Spring Boot. Spring Boot is built on the top of the spring and contains all the features of spring. In this article, we will see how to create a basic spring boot application.
Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies which are supported by JVM. Here, we will create the structure of an application using spring initializr and then use an IDE to create a sample GET route. Therefore, to do this, the following steps are followed:

Project: Maven
Language: Java
Spring Boot: 2.2.8
Packaging: JAR
Java: 8
Dependencies: Spring Web

  1. Extract the zip file. Now open a suitable IDE and then go to File->New->Project from existing sources->Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync.
Читайте также:  Раннее статическое связывание java

  1. Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.
  2. Go to src->main->java->com.gfg.Spring.boot.app, create a java class with name
    as Controller and add the annotation @RestController. Now create a GET API as shown below:

Источник

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.

  1. Launch IntelliJ IDEA. If the Welcome screen opens, click New Project . Otherwise, from the main menu, select File | New Project .
  2. In the New Project wizard, select New Project from the list on the left.
  3. Name the project (for example HelloWorld ) and change the default location if necessary.
  4. We’re not going to work with version control systems in this tutorial, so leave the Create Git repository option disabled.
  5. Make sure that Java is selected in Language , and IntelliJ is selected in Build system . Creating a new Java project
  6. 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 ). Creating the new project and adding the JDKIf 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 . Downloading a JDK when creating a project
  7. Leave the Add sample code option disabled as we’re going to do everything from scratch in this tutorial. Click Create .
Читайте также:  Html learning book in pdf

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.

  1. In the Project tool window, right-click the src folder, select New (or press Alt+Insert ), and then select Java Class .
  2. 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 on 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

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 on 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.

  1. 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.
  2. Type o , select out , and press Control+. again.
  3. 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.
  4. 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 information on 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.

  1. Type sout and press Enter .
  2. 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.

  1. Click in the gutter and select Run ‘HelloWorld.main()’ in the popup. The IDE starts compiling your code.
  2. 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

  1. From the main menu, select File | Project Structure ( Control+Alt+Shift+S ) and click Artifacts .
  2. Click , point to JAR and select From modules with dependencies .
  3. 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.
  4. Apply the changes and close the dialog.

Build the JAR artifact

  1. From the main menu, select Build | Build Artifacts .
  2. Point to HelloWorld:jar and select Build . Building an artifactIf you now look at the out/artifacts folder, you’ll find your JAR there. The JAR artifact is built

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.

  1. Press Control+Shift+A , find and run the Edit Configurations action.
  2. In the Run/Debug Configurations dialog, click and select JAR Application .
  3. 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 Application

Trisha Gee

Most readers of the IntelliJ IDEA blog are long past writing “Hello World” applications, but there are plenty of people who haven’t yet taken that first step to writing a Java program. If you are one of them, this tutorial is for you.

(If you’re an experienced developer, show students, juniors, friends and family this tutorial to get them started).

This video is based off the existing Create your first Java application tutorial. Before you get started, you will need to download and install IntelliJ IDEA.

Remember, IntelliJ IDEA Community is open source and completely free to use, and contains all the functionality you need to get started as a Java developer.

Once you’ve installed IntelliJ IDEA, you can watch the video and follow the tutorial steps together.

Some top tips, shortcuts and features from this video:

  • To create a new class, press ⌘N on MacOS or Alt+Insert on Windows or Linux on the directory in the Project Window, and select “Java class”.
  • When creating a new class, we can type the whole package path, separated by dots, followed by the class name, and IntelliJ IDEA will create the package and the Java file.
  • Move onto the next line in a class file by pressing Shift+Enter. This will put us onto the next line in the correct position and won’t break the previous line the caret was on.
  • To create a standard Java main method, we can simply type “main”. IntelliJ IDEA will generate a public static void main method for us with the correct parameters.
  • Live templates save us a lot of typing. Use ⌘J, or Ctrl+J in Windows or Linux, to see a list of all live templates that are valid for the current context.
  • Pressing Esc will always close a dropdown or dialog without making changes.
  • If we press Ctrl+. (that’s a period/full stop) when a code completion item is highlighted, IntelliJ IDEA will complete our code with the selected item and place a dot after it.
  • Go to the Run Window with ⌘4, or Alt+4 on Windows or Linux. Pressing this shortcut again will close the run window and put the focus back on the Editor.
  • Java ARchive (JAR) files are called artifacts in IntelliJ IDEA.
  • The keyboard shortcut ⌘; or Shift+Ctrl+Alt+s on Windows or Linux will open the Project Structure dialog.
  • Press Shift twice to use Search Everywhere if you don’t know the shortcut for something.
  • Pressing Ctrl twice brings up the Run Anything box, which is yet another way to run one of our configurations.

Whether you’ve completed this tutorial or are looking for some other way to start learning Java, check out IntelliJ IDEA Edu, a version of IntelliJ IDEA that provides courses to teach you Java. Or you can install the EduTools plugin.

Источник

Оцените статью