Write once, run anywhere (c)
- Preparation
- Ecosystem overview
- Build tools
- Basics
- Core
- First console application
- Unit testing
- Logging
- Web
- ORM
- Utils
- Advanced
Download and install required tools and software
-
- Set up JAVA_HOME and add Java bin to path for Windows for Mac
- Verify. Result of
echo %JAVA_HOME%(Windows, for Mac useecho $JAVA_HOME)java --versionshould be something like thisC:\Users\user>echo %JAVA_HOME% C:\workspace\soft\jdk-11.0.13+8 C:\Users\ychum>java --version openjdk 11.0.13 2021-10-19 OpenJDK Runtime Environment Temurin-11.0.13+8 (build 11.0.13+8) OpenJDK 64-Bit Server VM Temurin-11.0.13+8 (build 11.0.13+8, mixed mode)
-
- Verify. Result of
mvn --versionshould be something like thisC:\Users\user>mvn --version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: c:\workspace\soft\apache-maven-3.6.3\bin\.. Java version: 11.0.13, vendor: Eclipse Adoptium, runtime: C:\workspace\soft\jdk-11.0.13+8 Default locale: en_US, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
- Verify. Result of
-
Java IDE IntelliJ IDEA Community edition. Set up Intellij Idea to use your Java
Java platform
Comparison with .NET Framework
More info about JVM
Difference between OpenJDK and Adoptium/AdoptOpenJDK
The diagram below depicts building and running Java archive (jar)
Courses
Recommended books
Courses
- Java Programming Masterclass updated to Java 17
- Java SE 17 Fundamentals
- Java Fundamentals: Object-oriented Design
- Streams, Collectors, and Optionals for Data Processing in Java 8
- Java Coding Practices
Tutorial Java language updates
Intellij IDEA | Creating a Maven project
Maven project structure, detailed see
dotnet2java\java\console-app\src\main\java\com\mycompany\app\HelloWorldApp.java
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}How to run
- IntelliJ IDEA
- Console
-
Make sure that you are in
console-appfolder$ pwd /code/dotnet2java/java/console-app -
Run maven to build jar
mvn clean package -
Run Java program
java -cp ./target/console-app-1.0-SNAPSHOT.jar com.mycompany.app.HelloWorldApp
-
A Closer Look at the "Hello World!" Application
Usually, JUnit is usually used in conjunction with Mockito. Alternative testing framework is TestNG. Powermock as mocking extension for Mockito or EasyMock to test private or static methods.
Let's imagine we have basic calculator with simple method that adds two integers.
dotnet2java\java\console-app\src\main\java\com\mycompany\app\BasicCalculator.java
public class BasicCalculator {
public int addTwoIntegers(int firstInteger, int secondInteger) {
return Math.addExact(firstInteger, secondInteger);
}
}In order to test at least 1 positive and 1 negative scenarios, we can create something like this:
dotnet2java\java\console-app\src\test\java\com\mycompany\app\BasicCalculatorTest.java
package com.mycompany.app;
import org.junit.Assert;
import org.junit.Test;
public class BasicCalculatorTest {
private final BasicCalculator calculator = new BasicCalculator();
@Test
public void shouldAddTwoIntegersTest() {
//given
int firstInteger = 1;
int secondInteger = 3;
//when
int actual = calculator.addTwoIntegers(firstInteger, secondInteger);
//then
int expected = 4;
Assert.assertEquals(expected, actual);
}
@Test(expected = ArithmeticException.class)
public void shouldThrowArithmeticExceptionInCaseOverflow() {
//given
int firstInteger = Integer.MAX_VALUE;
int secondInteger = Integer.MAX_VALUE / 2;
//when
calculator.addTwoIntegers(firstInteger, secondInteger);
}
}To run tests, make sure that you in the same folder as pom.xml and run mvn clean test.
After maven execution, you will see:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.BasicCalculatorTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.041 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
Courses
- Spring framework: Core Spring
- Spring Framework: Building Web Applications and Services
- Spring Framework: Securing Spring Applications
- Spring Framework: Spring Data JPA 5 with Hibernate
- Spring Boot 2: The Big Picture
- Spring Framework 2: Creating Your First Spring Boot Application
- Spring Cloud Fundamentals
Main concepts
TODO
Project lombok is an annotation-based Java library that allows you to reduce boilerplate code using code generation. Alternative solution can be records introduced in Java 14.
Recommended books
Recommended books














