eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud) eBook – Mockito – NPI EA (tag = Mockito)
[announcement - icon]
Mocking is an essential part of unit testing, and the Mockito
library makes it easy to write clean and intuitive unit
tests for your Java code.
Get started with mocking and improve your application tests
using our Mockito guide:
Download the
eBook
eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
[announcement - icon]
Handling concurrency in an application can be a tricky process
with many potential pitfalls. A solid grasp of the
fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with
our Java Concurrency guide:
>>
Download the eBook
eBook – Reactive – NPI EA (cat=Reactive) eBook – Java Streams – NPI EA (cat=Java Streams)
[announcement - icon]
Since its introduction in Java 8, the Stream API has become a
staple of Java development. The basic operations like iterating,
filtering, mapping sequences of elements are deceptively simple to
use.
But these can also be overused and fall into some common
pitfalls.
To get a better understanding on how Streams work and how
to combine them with other language features, check out our guide
to Java Streams:
>> Join Pro
and download the eBook
eBook – Jackson – NPI EA (cat=Jackson) eBook – HTTP Client – NPI EA (cat=Http Client-Side) eBook – Maven – NPI EA (cat = Maven) eBook – Persistence – NPI EA (cat=Persistence) eBook – RwS – NPI EA (cat=Spring MVC) Course – LS – NPI EA (cat=Jackson) Course – RWSB – NPI EA (cat=REST) Course – LSS – NPI EA (cat=Spring Security)
[announcement - icon]
Yes, Spring Security can be complex, from the more advanced
functionality within the Core to the deep OAuth support in the
framework.
I built the security material as two full courses - Core and
OAuth, to get practical with these more complex scenarios. We
explore when and how to use each feature and code through it on
the backing project.
You can explore the course here:
>> Learn Spring
Security
Course – All Access – NPI EA (cat= Spring)
[announcement - icon]
All Access is finally out, with all of my Spring courses. Learn
JUnit is out as well, and Learn Maven is coming fast. And, of
course, quite a bit more affordable. Finally.
>> GET THE
COURSE
Course – LSD – NPI EA (tag=Spring Data JPA)
[announcement - icon]
Spring Data JPA is a great way to handle the complexity of
JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference
course:
>> CHECK OUT THE
COURSE
Partner – Moderne – NPI EA (cat=Spring Boot)
[announcement - icon]
Refactor Java code safely — and automatically — with
OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to
put off. That’s where OpenRewrite comes in. The open-source
framework for large-scale, automated code transformations helps
teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at
Moderne run live, hands-on training sessions — one for newcomers
and one for experienced users. You’ll see how recipes work, how to
apply them across projects, and how to modernize code with
confidence.
Join the next session,
bring your questions, and learn how to automate the kind of work
that usually eats your sprint time.
Course – LJB – NPI EA (cat = Core Java)
1. Overview
Sometimes when programming in Java, it may be helpful to programmatically find the version of Java that we’re using. In this tutorial, we’ll look at a few ways to get the Java version.
2. Java Version Naming Convention
Up until Java 9, the Java version did not follow the Semantic Versioning. The format was 1.X.Y_Z. X and Y indicate major and minor versions, respectively. Z is used to indicate an update release and separated by underscore “_”. For example, 1.8.0_181. For Java 9 and beyond, the Java version follows the Semantic Versioning. The Semantic Versioning uses the X.Y.Z format. It refers to major, minor, and patch. For example,11.0.7.
3. Getting Java Version
3.1. Using System.getProperty
A system property is a key-value pair that the Java runtime provides. The java.version is a system property that provides the Java version. Let’s define a method for getting the version:
public void givenJava_whenUsingSystemProp_thenGetVersion() {
int expectedVersion = 8;
String[] versionElements = System.getProperty("java.version").split("\\.");
int discard = Integer.parseInt(versionElements[0]);
int version;
if (discard == 1) {
version = Integer.parseInt(versionElements[1]);
} else {
version = discard;
}
Assertions.assertThat(version).isEqualTo(expectedVersion);
}
To support both Java version formats, we should check the first number until the dot.
3.2. Using Apache Commons Lang 3
A second approach for getting the Java version is via the Apache Commons Lang 3 library. We first need to add the commons-lang3 Maven dependency to our pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
We’ll use the SystemUtils class to obtain information about the Java platform. Let’s define a method for this purpose:
public void givenJava_whenUsingCommonsLang_thenGetVersion() {
int expectedVersion = 8;
String[] versionElements = SystemUtils.JAVA_SPECIFICATION_VERSION.split("\\.");
int discard = Integer.parseInt(versionElements[0]);
int version;
if (discard == 1) {
version = Integer.parseInt(versionElements[1]);
} else {
version = discard;
}
Assertions.assertThat(version).isEqualTo(expectedVersion);
}
We are using the JAVA_SPECIFICATION_VERSION that is intended to mirror available values from the java.specification.version System property.
3.3. Using Runtime.version()
In Java 9 and above, we can use the Runtime.version() to get version information. Let’s define a method for this purpose:
public void givenJava_whenUsingRuntime_thenGetVersion(){
String expectedVersion = "15";
Runtime.Version runtimeVersion = Runtime.version();
String version = String.valueOf(runtimeVersion.version().get(0));
Assertions.assertThat(version).isEqualTo(expectedVersion);
}
4. Conclusion
In this article, we describe a few ways to obtain the Java version.
The code backing this article is available on GitHub. Once you're
logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung) eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
[announcement - icon]
The Apache HTTP Client is a very robust library, suitable
for both simple and advanced use cases when testing HTTP
endpoints. Check out our guide covering basic request and
response handling, as well as security, cookies, timeouts, and
more:
>> Download
the eBook
eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
[announcement - icon]
Handling concurrency in an application can be a tricky process
with many potential pitfalls. A solid grasp of the
fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with
our Java Concurrency guide:
>>
Download the eBook
eBook – Java Streams – NPI EA (cat=Java Streams)
[announcement - icon]
Since its introduction in Java 8, the Stream API has become a
staple of Java development. The basic operations like iterating,
filtering, mapping sequences of elements are deceptively simple to
use.
But these can also be overused and fall into some common
pitfalls.
To get a better understanding on how Streams work and how
to combine them with other language features, check out our guide
to Java Streams:
>> Join Pro
and download the eBook
eBook – Persistence – NPI EA (cat=Persistence) Course – LS – NPI EA (cat=REST) Partner – Moderne – NPI EA (tag=Refactoring)
[announcement - icon]
Modern Java teams move fast — but codebases
don’t always keep up. Frameworks change, dependencies drift, and
tech debt builds until it starts to drag on delivery. OpenRewrite
was built to fix that: an open-source refactoring engine that
automates repetitive code changes while keeping developer intent
intact.
The monthly training series, led by the creators and maintainers
of OpenRewrite at Moderne, walks through real-world migrations and
modernization patterns. Whether you’re new to recipes or ready to
write your own, you’ll learn practical ways to refactor safely and
at scale.
If you’ve ever wished refactoring felt as natural — and as fast
— as writing code, this is a good
place to start.
eBook Jackson – NPI EA – 3 (cat = Jackson)