PRA01ENG.rst

Programming Laboratory

Integrated Development Environment

Attention! The code for this classes can be found on the branch IntroductionClassesStart in the https://github.com/WitMar/PRA2018-2019 repository. The ending code in the branch IntroductionClassesEnd.

Editor

In the Java environment, the recommended editor is IntelliJ IDEA available in the free Community version here:

GIT

GIT is a software version control system.

Git has three states that can contain files: approved, modified and tracked. Approved means that data has been safely stored in your local database. Modified means that the file has been changed, but the changes have not been made to the database. Tracked - means that the modified file was intended for approval in the current form in the next commit operation. Tracking branches are local branches that have a direct relationship to a remote branch.

The basic way to work with Git looks something like this:

* You modify the files in the working directory.

* You mark the modified files as tracked by adding their current status (snapshot) to the vault.

* You commit (commit), during which the contents of files from the vault are saved as a project snapshot in the Git directory.

GIT FLOW:

a10

Task 1

Create account on GitHub

Open repository

Select Fork to download the repository to your own account.

Open IdeaJ, if you open it for the first time then choose Project from version control. Otherwise choose File -> New -> Project From Version Control -> Git.

Copy the address available after choosing CloneOrDownload (the HTTPS version, not the SSH one!)

1

Go to lower right corner of the IDE and choose branch IntroductionClassesStart

a2

You should get the following effect - up to the class name which might differ. Note if the code is not colored as you expect it means that the environment did not automatically find your Maven file.

Choose search icon on tools menu and write maven, choose option Add Maven Project.

aMV

Next find the directory in which you download the project code and choose pom.xml file. You should obtain given below result (check icon next to java file!). In the rigth side of the screen you should see Maven tab, choose option COMPILE and run it (by clicking reen triangle or double click on the compile word).

a3

If the icon and java code is still not properly colored check options:

Choose File -> Project Structure set up Java version in tab Project and syntax version in tab Module.

a33

Check compilation version in the IDE settings File -> Settings -> Compiler

a113

MAVEN

Maven is an application development system that helps us automate the process of compiling and generating jar files, war files, etc. It also imposes a specific project structure:

* pom.xml – main configuration file

* /src/main – a directory where we can find files of our program, there are two sub-directories: java - all classes are here (the whole code of our module)

* resources – here will be all files that are not a code, e.g. graphics, XML files, configurations for web projects we will also have a webapp directory, which is used to put all web content

* /src/test - has a similar structure to the main directory, with the difference that it is used only during automated tests

* /target – here a compiled project goes (i.e. in the form of an executable JAR file or a WAR web application)

In the upper-right corner you will find the "Maven" tab. If it is not there go to View -> Tool Windows -> Maven Projects.

After unfolding, we should see a list of instructions (if we do not choose to refresh - two rounded green arrows, if not, we will click the plus and point to pom.xml in our project).

Task 2

Choose in Maven tab Clean + Install, you should get BUILD SUCCESS message

a22

The Main.java file in your code should have round green circle next to it, if not repeat the steps from the beginning again.

More about Mavena can be found here (you can run maven also from console if you wish):

Maven documentation:

Creating maven files for executable java files:

Task 3

Try to run class Main.java (right click with mouse and then choose Run). Try to run .jar file in the target directory. See what happens.

Logger

A very important and helpful element of an IT project is logging messages / errors. Particularly useful are the ability to split logs into levels, save them automatically to a file, and the ability to analyze multithreaded applications. An example of a login library in java is log4j.

In order to use library we have to add a dependency (to the pom.xml file) in the maven setting file:

<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>

We want to add the log4j library to find the appropriate entry, just enter maven log4j in google. The first link should lead us to the appriopriate page:

After choosing the version, we will see the entry:

The first time we add a dependecy we add the dependencies tag in the xml. In the future, we'll just paste the appropriate dependencies between this tag.

Refresh Maven tab (choose round arrows). You should see new section in the tab called Dependencies with one element log4j dependency.

Add to class main.java logger.

Logger log = Logger.getLogger("name");

and in main method add

log.info("message");

Task 4

Try to run class Main.java, see if you can see the message "message".

Log4j

The reason why you can't see the message is that you did not set up the logger (meaning did not specify its settings).

In order to add setting to logger create the file log4j.properties in the Resources directory

Fill out this file as follows:

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

More examples on Log4j can be found on:

Task 5

Try to run class Main.java, now you should be able to see the message "message". Also in your project directory there should be created a file called log.log.

JAR

JAR is a java executable file. You can run jar from IDE or from console using java -jar filename command.

The jar file need to have a so-called manifest (run path) in order to run. Add the following code to pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>${project.artifactId}-${project.version}</finalName>
                        <attach>false</attach>
                        <appendAssemblyId>false</appendAssemblyId>
                        <archive>
                            <manifest>
                                <mainClass>
                                    pracownia.introduction.Main
                                </mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Main class is the path to class that shall be run when jar is executed.

Task 6

Compile project using Clean + Install in Maven and run the .jar again, it should be working now.

Commit

Task 7

Commit your code to repository as a new branch named IntroductionClassesWithLogger.

Commit w IntelliJ

Ctrl + k to push code to local the repository

Ctrl + Shift + k to commit code to remote repository

Ctrl + t to synchronize code with repository (download code from remote repository)

Other:

In case of conflicts in order to resolve conflicts, one must merge with the code to which we commit and resolve conflicts in the newly opened window.

To download the code of a given commit to yourself without merge we can perform the operation called Cherry Pick.

Final Code

In case of any problems (you get lost or were absent) you can find final code of the classes in branch named IntroductionClassesEnd.