Self-Contained Applications, built in Java

jlink Yes, this is possible with jlink (JEP 282), but all of your code and your dependencies need to be modular JARs (i.e. ones with module-info.class). It works like this: jlink –module-path $JAVA_HOME/jmods:mods –add-modules your.app –launcher launch-app=your.app –output your-app-image In detail: –module-path lists the folders that contain modules – this needs to include the platform … Read more

What’s the difference between requires and requires static in module declaration

A requires clause expresses that the required module is needed at compile and run time. Consequently, when the module system encounters such a clause during module resolution (the phase in which module descriptors are processed and dependencies are resolved) it searches the universe of observable modules (the modules in the JDK and on the module … Read more

How to apply Filtering on groupBy in java streams

You can make use of the Collectors.filtering API introduced since Java-9 for this: Map<String, List<Employee>> output = list.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.filtering(e -> e.getSalary() > 2000, Collectors.toList()))); Important from the API note : The filtering() collectors are most useful when used in a multi-level reduction, such as downstream of a groupingBy or partitioningBy. A filtering collector differs … Read more

Can Java 9 run on a 32-bit OS?

Although the 32-bit binaries for JDK9 seem to be missing from Oracle’s lousy, unencrypted download page, if (after clicking “Accept License Agreement”) you copy the URL of the 64-bit binaries and change x64 to x86, it will give you the 32-bit binaries. Update: And now it’s gone. Bizarre! What is Oracle playing at? They went … Read more

Hibernate, Java 9 and SystemException

According to the migration guide and the java docs, since the module java.transaction which exports the package javax.transaction has been marked as @Deprecated. You should ideally migrate your code to be using javaee/javax.transaction instead. Currently, you can do so using automatic module converted from the dependency: <dependency> <groupId>javax.transaction</groupId> <artifactId>javax.transaction-api</artifactId> <version>1.2</version> </dependency> and adding to the … Read more

How to properly close MappedByteBuffer?

Try this one. public class Test { public static void main(String[] args) throws Exception { String filePath = “D:/temp/file”; RandomAccessFile file = new RandomAccessFile(filePath, “rw”); FileChannel chan = file.getChannel(); try { MappedByteBuffer buffer = chan.map(FileChannel.MapMode.READ_WRITE, 0, 128); // Do something buffer.putInt(4); buffer.force(); Cleaner cleaner = ((sun.nio.ch.DirectBuffer) buffer).cleaner(); if (cleaner != null) { cleaner.clean(); } } … Read more

How do you organize tests in a modular Java project?

Welcome to Testing In The Modular World! Which kind of tests do you want write? Extra-module tests: Create a test-only project (no “src/main” directory) and declare a “src/test/java/module-info.java” module descriptor. In-module tests: As it was from Day 1 you need to “blend in”/merge/shadow your test classes into your main classes or vice versa. Here you … Read more

SimpleDateFormat with German Locale – Java 8 vs Java 10+

I don’t say it’s a nice solution, but it seems to be a way through. Map<Long, String> dayOfWeekTexts = Map.of(1L, “Mo”, 2L, “Di”, 3L, “Mi”, 4L, “Do”, 5L, “Fr”, 6L, “Sa”, 7L, “So”); Map<Long, String> monthTexts = Map.ofEntries(Map.entry(1L, “Jan”), Map.entry(2L, “Feb”), Map.entry(3L, “Mär”), Map.entry(4L, “Apr”), Map.entry(5L, “Mai”), Map.entry(6L, “Jun”), Map.entry(7L, “Jul”), Map.entry(8L, “Aug”), Map.entry(9L, “Sep”), … Read more

Why did Java 9 introduce the JMOD file format?

The purpose of JMODs are not well documented and existing documentation is rather sparse. Here is an in-depth explanation of system, from my understanding. Parts of this answer are rather long, verbose, partially redundant, and a tough read. Constructive, structural, or grammatical edits are more than welcome to improve readability for future readers. Short(er) Answer … Read more