[First Java] Make something that works with Intellij for the time being


This is the procedure for Java beginners to create a program in Intellij, package it, and make something that works for the time being.

This time, we will create a program that only acquires the content of the title on the top page of Keta. Rather than studying Java, it is more like a tutorial for understanding the flow of Java development.


Please download and install jdk and Intellij from the above link.

Creating a project

After launching Intellij, click Create New Project

Screen Shot 2017-12-27 at 9.49.30.png

Select Maven and click Next

Screen Shot 2017-12-27 at 9.50.50.png

Enter the following values and click Next

item value
GroupId jp.sample
ArtifactId sample-artifact
Version Default
Screen Shot 2017-12-27 at 10.00.47.png

Enter the Project name and the location where you want to place the Project files and click Finish

Screen Shot 2017-12-27 at 10.01.23.png

If the following screen is displayed, it is successful.

Screen Shot 2017-12-27 at 10.02.46.png

Create pom.xml

File for project management. Is it something like Gemfile in Rails?

Create with the following contents.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





After adding to pom.xml, If you see Maven projects need to be imported in the bottom right, click ʻEnable Auto-Import`

Screen Shot 2017-12-28 at 17.53.45.png

Description of each setting

dependencies Dependent library settings. This time, we will use the following libraries.

Library name Use
Jsoup For scraping
junit for test

properties Described to avoid the following errors during build.

[ERROR] Source option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.

maven-assembly-plugin A plugin for creating packages that include all dependencies. Specify Main-Class in archive-> manifest. This eliminates the need for Manifest.MF.


Creating a program

Create a scraping program.

First, create a Java Class.

Screen Shot 2017-12-28 at 17.55.34.png

Create a class named Main.

Screen Shot 2017-12-27 at 11.43.07.png

Once created, write the following in Main.java.


package jp.sample;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        final ScrapeService scrapeService = new ScrapeService();

Create ScrapeService.java in the same way. It is a program to get the title of Keta.


package jp.sample;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;

public class ScrapeService {
    private String url = "https://qiita.com/";

    public String getTitle() throws IOException {
        Document document = Jsoup.connect(url).get();
        Elements title = document.select("title");
        return title.text();

Open Main.java, click the play button to the left of public class Main {, and click Run'Main.main ()' to run the program.

Screen Shot 2017-12-28 at 17.11.01.png

Then, the following result will be displayed in the debugger below. If the title of Keta is displayed, it is successful.

Screen Shot 2017-12-28 at 17.13.16.png

Creating test code

Next, write the test code.

Open ScrapeService.java, with the cursor on the class name ScrapeService, and press ʻAlt + ʻEnter.

Then, as shown in the image below, the option Create Test will be displayed. Click it.

Screen Shot 2017-12-28 at 17.15.49.png

This time, I will use JUnit4. If you see a button called FIX, click it. There is a list of testable methods under Generate test methods for:, check them and click OK.

Screen Shot 2017-12-28 at 17.57.41.png

Then, the test code file is created as shown below.

Screen Shot 2017-12-28 at 17.59.34.png

Then edit it as follows.


package jp.sample;

import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;

public class ScrapeServiceTest {

    public void getTitle() throws IOException {
        final ScrapeService scrapeService = new ScrapeService();
        assertEquals("Qiita - A technical knowledge sharing platform for programmers.", scrapeService.getTitle());

Click the play button to the left of public class LoginServiceTest {and click Run'Main.main ()' to run the test.

Screen Shot 2017-12-28 at 18.05.53.png

If it is displayed in the debugger as shown below, the test is successful.

Screen Shot 2017-12-28 at 18.06.41.png

build Build and package. From here on, you'll be working from the command line, not Intellij.

Go to the sampleartifact / directory where pom.xml is located and run the following command.

$ mvn package

 T E S T S
Running jp.sample.ScrapeServiceTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.786 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ sample-artifact ---
[INFO] Building jar: /Users/username/IdeaProjects/sampleartifact/target/sample-artifact-1.0-SNAPSHOT.jar
[INFO] --- maven-assembly-plugin:3.1.0:single (make-assembly) @ sample-artifact ---
[INFO] Building jar: /Users/username/IdeaProjects/sampleartifact/target/sample-1.0-SNAPSHOT-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.910 s
[INFO] Finished at: 2017-12-28T18:10:59+09:00
[INFO] Final Memory: 20M/67M
[INFO] ------------------------------------------------------------------------

Run the generated jar file

A jar file called sample-1.0-SNAPSHOT-jar-with-dependencies.jar is generated in sampleartifact / target, so try running it.

$ java -jar sample-1.0-SNAPSHOT-jar-with-dependencies.jar
Qiita - A technical knowledge sharing platform for programmers.

If the title of Keta is displayed like this, it is successful!

mvn install mvn install will generate files not only under target, but also in the Local repository under ~ / .m2 /.

$ mvn install
$ ls -1 -a ~/.m2/repository/jp/sample/sample-artifact/1.0-SNAPSHOT/

Delete the target directory

You can delete it with the following command. It is used to empty the target and then rebuild it.

$ mvn clean


Recommended Posts

[First Java] Make something that works with Intellij for the time being
Introduction to java for the first time # 2
Learning for the first time java [Introduction]
Use Java external library for the time being
Run Dataflow, Java, streaming for the time being
Learn for the first time java # 3 expressions and operators
Oreore certificate https (2020/12/19) for the first time with nginx
Learning memo when learning Java for the first time (personal learning memo)
Modeling a Digimon with DDD for the first time Part 1
Spring Boot for the first time
Access Web API on Android with Get and process Json (Java for the time being)
Spring AOP for the first time
Java14 came out, so I tried record for the time being
Make something like Java Enum with Typescript
Java12 came out, so I tried the switch expression for the time being
Glassfish tuning list that I want to keep for the time being
[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
Programming for the first time in my life Java 1st Hello World
I tried using Docker for the first time
[Java] Set the time from the browser with jsoup
Walls hit by Rspec for the first time
Impressions and doubts about using java for the first time in Android Studio
The training for newcomers was "Make an app!", So I made an app for the time being.
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
I want you to use Scala as Better Java for the time being
Install Amazon Corretto (preview) for the time being
I tried to make a program that searches for the target class from the process that is overloaded with Java
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
A memo to do for the time being when building CentOS 6 series with VirtualBox
[CircleCI] I will explain the stupid configuration file (config.yml) that I wrote for the first time.
[Android studio / Java] What you don't understand when you touch it for the first time
[Java basics] Let's make a triangle with a for statement
Try running Spring Cloud Config for the time being
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Command to try using Docker for the time being
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
A summary of what Java programmers find when reading Kotlin source for the first time
For aggregation processing that cannot be done with the Hive predefined function, first try the Reflect function.
Whether to make the server side at the time of system rebuild with Kotlin or Java
[Rails] I tried using the button_to method for the first time
Plan optimization with AI that understands the reason for the result
With the software I've been making for a long time ...
[DL4J] Java deep learning for the first time (handwriting recognition using a fully connected neural network)
How to deal with the type that I thought about writing a Java program for 2 years
Think when Rails (turbolinks) doesn't load the page for the first time
Generate colors.xml for dark themes with the technology that supports Force Dark
[WSL] Solution for the phenomenon that 404 is displayed when trying to insert Java with apt (personal memo)
Let's experience the authorization code grant flow with Spring Security OAuth-Part 2: Creating an app for the time being