Skip to content

Blog

How to resolve module java.base does not 'opens java.lang' to unnamed module?

1. Purpose

In this post, I will demonstrate how to resolve the following error when running spring cloud eureka server:

Terminal window
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project service-discovery: An exception occurred while running. null: InvocationTargetException: Cannot load configuration class: org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration: ExceptionInInitializerError: Unable to load cache item: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @505e2a79 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

The key problem is below:

Terminal window
java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module ...

2. The solution

The detailed error

I want to start an spring cloud eureka server in IntelliJ IDEA, but I got this error:

Terminal window
/Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java "-Dmaven.multiModuleProjectDirectory=/Users/bswen/JavaProjects/SpringCloudLearning202201/springcloud-demo/chapter03 -- hello-cloud/service-discovery" "-Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-Dmaven.ext.class.path=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven-event-listener.jar" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2019.3.3 org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run
[INFO] Scanning for projects...
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @505e2a79
at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:357)
...
at com.cd826dong.clouddemo.Application.main (Application.java:32)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:78)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:567)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:527)
at java.lang.Thread.run (Thread.java:831)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project service-discovery: An exception occurred while running. null: InvocationTargetException: Cannot load configuration class: org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration: ExceptionInInitializerError: Unable to load cache item: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @505e2a79 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

The module’s dependency

The source code

The Eureka Server’s source code:

@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}

The parent module’s dependency:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<groupId>cd826dong.cloud</groupId>
<artifactId>springcloud-book-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>SpringCloud Demo Projects(HelloCloud) -- Parent Pom</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<guava.version>20.0</guava.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

This module’s dependency:

<parent>
<groupId>cd826dong.cloud</groupId>
<artifactId>springcloud-book-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<artifactId>service-discovery</artifactId>
<packaging>jar</packaging>
<name>SpringCloud Demo Projects(HelloCloud) -- Eureka Server</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

if start from command line, we got this error:

Terminal window
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project springcloud-book-parent: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Let’s Debug the error

Check the java version:

Terminal window
chapter03 -- hello-cloud git:(master) java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

The solution

Run the following command in parent module:

Terminal window
mvn install

Then re-run the module:

mvn spring-boot:run

But double-clicking the run to run the spring-boot plugin in the idea still reports an error:

The reason of the error

Idea use jdk 1.6 to start spring applicaiton, just as the logs show:

/Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java ...

Then I check:

➜ chapter03 -- hello-cloud git:(master) ✗ /Library/Java/JavaVirtualMachines/temurin-16.jdk/Contents/Home/bin/java --version
openjdk 16.0.2 2021-07-20
OpenJDK Runtime Environment Temurin-16.0.2+7 (build 16.0.2+7)
OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (build 16.0.2+7, mixed mode, sharing)
➜ chapter03 -- hello-cloud git:(master) ✗

It’s jdk1.6, NOT jdk1.8, that is the problem.

Change project sdk to 1.8:

Then click ‘spring-boot:run’ to run again:

2022-01-13 17:27:30.757 INFO 10418 --- [ Thread-12] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2022-01-13 17:27:30.766 INFO 10418 --- [ Thread-12] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2022-01-13 17:27:30.828 INFO 10418 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8260 (http)
2022-01-13 17:27:30.829 INFO 10418 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8260
2022-01-13 17:27:30.835 INFO 10418 --- [ main] com.cd826dong.clouddemo.Application : Started Application in 9.384 seconds (JVM running for 13.583)

It works!

How to change default JAVA sdk version for new java projects?

Click File--Other Settings--Structure for new projects:

then change default sdk for intellij idea:

3. Summary

In this post, I demonstrated how to solve the module java.base does not "opens java.lang" to unnamed module error. The key point is to make sure that your java version is correct. That’s it, thanks for your reading.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

How to resolve 'plugin org.springframework.boot was not found in any of the following sources exception' when building spring boot applications with gradle

Problem

When we build spring boot applications with gradle, sometimes we get this error :

Terminal window
Build file '/Users/bswen/private/bw/bswen-github/bswen-project/spring-boot-23/build.gradle' line: 2
Plugin [id: 'org.springframework.boot', version: '2.3.2.RELEASE'] was not found in any of the following sources:
* What went wrong:
Plugin [id: 'org.springframework.boot', version: '2.3.2.RELEASE'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.3.2.RELEASE')
Searched in the following repositories:
Gradle Central Plugin Repository

Environments

The environments are as follows:

  • java version: jdk 1.8+
  • spring boot version: 2.3.2.RELEASE+

The build.gradle

The build.gradle of this spring boot application is as follows:

build.gradle
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.bswen'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}

How to resolve the problem

This error is caused by the incorrectly configured build.gradle in the project. You should add buildscript block to the build.gradle, this buildscript block is required by the build.gradle script.

According to this post (by Peter Niederwieser):

and this comment by Ashish:

build.gradle and buildscript relationship

So please do as follows:

  1. Open the build.gradle ,add the lines:
build.gradle
buildscript {
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE")
}
}
  1. After the above change, the whole build.gradle file is as follows:
build.gradle
buildscript {
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE")
}
}
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.bswen'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}

More about BuildScript

The buildscript block in a build.gradle file is used to configure the build script itself. It allows you to define dependencies and repositories that are needed for the build script to run. This is different from the dependencies and repositories that are needed for the project being built.

Here is an example of a buildscript block:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}

In this example:

  • The repositories block specifies where to find the dependencies needed for the build script. In this case, it is using Maven Central.
  • The dependencies block specifies the dependencies needed for the build script. Here, it includes the Android Gradle plugin.

This configuration is necessary for setting up the environment in which the build script will execute.

The code base of this example is uploaded to github.com, you can click this web url to download the code.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

How to resolve openssl error when loading shared libraries libssl.so.1.1

1. The purpose of this post

After install openssl, we run the below command:

Terminal window
openssl version

to verity the installation, but sometimes we would get this error:

Terminal window
[root@node1 openssl-1.1.0f]# openssl version
openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

2. Environments

The linux system is:

Terminal window
[root@node1 openssl-1.0.0f]# uname -a
Linux node1 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

3. Debug

We try to find the file named libssl.so.1.1 as follows:

Terminal window
[root@localhost openssl-1.1.0f]# ll /usr/lib64/libssl*
-rwxr-xr-x. 1 root root 315096 8m 7 2017 /usr/lib64/libssl3.so
lrwxrwxrwx. 1 root root 16 7m 9 2009 /usr/lib64/libssl.so.10 -> libssl.so.1.0.2k
-rwxr-xr-x. 1 root root 470336 8m 4 2017 /usr/lib64/libssl.so.1.0.2k
[root@localhost openssl-1.1.0f]# ll /usr/local/lib64
Total 8984
drwxr-xr-x. 2 root root 39 11m 8 11:30 engines-1.1
-rw-r--r--. 1 root root 4967326 11m 8 11:30 libcrypto.a
lrwxrwxrwx. 1 root root 16 11m 8 11:30 libcrypto.so -> libcrypto.so.1.1
-rwxr-xr-x. 1 root root 2934272 11m 8 11:30 libcrypto.so.1.1
-rw-r--r--. 1 root root 766182 11m 8 11:30 libssl.a
lrwxrwxrwx. 1 root root 13 11m 8 11:30 libssl.so -> libssl.so.1.1
-rwxr-xr-x. 1 root root 521384 11m 8 11:30 libssl.so.1.1
drwxr-xr-x. 2 root root 61 11m 8 11:22 pkgconfig

We can find that the libcrypto.so.1.1 is located in the /usr/local/lib64, But openssl try to find the .so libraries in the LD_LIBRARY_PATH:

Terminal window
[root@localhost ~]# echo $LD_LIBRARY_PATH
/usr/lib64:/usr/local/lib64

So the solution is try to tell openssl where the library is.

4. Resolve it

There are two methods to resolve this problem:

4.1 Method 1: Change the LD_LIBRARY_PATH

Terminal window
export LD_LIBRARY_PATH = /usr/local/lib64:$LD_LIBRARY_PATH
Terminal window
[root@localhost openssl-1.1.0f]# ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
[root@localhost openssl-1.1.0f]# ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

5. More about the LD_LIBRARY_PATH

As the linux documents shown:

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

how to solve `Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent` problem when running android app on android 12 ?

1. Problem

When our app change the target to android sdk 31 or android 12, the app crash and we get this error message:

Terminal window
06/03 17:37:21: Launching 'app' on Pixel 5 API 31 android12.
Install successfully finished in 109 ms.
$ adb shell am start -n "com.bswen.wzreceiver/com.bswen.wzreceiver.ui.main.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 11604 on device 'Pixel_5_API_31_android12 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
V/GraphicsEnvironment: ANGLE Developer option for 'com.bswen.wzreceiver' set to: 'default'
V/GraphicsEnvironment: Neither updatable production driver nor prerelease driver is supported.
D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
I/MultiDex: VM with version 2.1.0 has multidex support
I/MultiDex: Installing application
I/MultiDex: VM has multidex support, MultiDex support library is disabled.
D/WM-WrkMgrInitializer: Initializing WorkManager with default configuration.
W/FA-Ads: Disabling data collection. Found google_app_id in strings.xml but Google Analytics for Firebase is missing. Remove this value or add Google Analytics for Firebase to resume data collection.
I/DynamiteModule: Considering local module com.google.android.gms.ads.dynamite:0 and remote module com.google.android.gms.ads.dynamite:213806100
I/DynamiteModule: Selected remote version of com.google.android.gms.ads.dynamite, version >= 213806100
V/DynamiteModule: Dynamite loader version >= 2, using loadModule2NoCrashUtils
D/CompatibilityChangeReporter: Compat change id reported: 160794467; UID 10146; state: ENABLED
E/AndroidRuntime: FATAL EXCEPTION: Thread-3
Process: com.bswen.wzreceiver, PID: 11604
java.lang.IllegalArgumentException: com.bswen.wzreceiver: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at com.bswen.wzreceiver.utils.AlarmUtil.scheduleAlarm(AlarmUtil.java:30)
at com.bswen.wzreceiver.App$1.run(App.java:115)
at java.lang.Thread.run(Thread.java:920)
I/Process: Sending signal. PID: 11604 SIG: 9

The key error message is:

Terminal window
E/AndroidRuntime: FATAL EXCEPTION: Thread-3
Process: com.bswen.wzreceiver, PID: 11604
java.lang.IllegalArgumentException: com.bswen.wzreceiver: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at com.bswen.wzreceiver.utils.AlarmUtil.scheduleAlarm(AlarmUtil.java:30)
at com.bswen.wzreceiver.App$1.run(App.java:115)
at java.lang.Thread.run(Thread.java:920)
I/Process: Sending signal. PID: 11604 SIG: 9

2. Reason

Since Android 12 brought important updates to PendingIntents, including the need to explicitly determine if a PendingIntent is mutable, I thought it was worth talking in-depth about what a PendingIntent does, how the system uses it, and why you might need mutable types PendingIntent.

What is pending intent?

Let’s look at the different ways PendingIntents are used in applications, and why we use them.

Basic usage of pending intent:

val intent = Intent(applicationContext, MainActivity::class.java).apply {
action = NOTIFICATION_ACTION
data = deepLink
}
val pendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
intent,
PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(
applicationContext,
NOTIFICATION_CHANNEL
).apply {
// ...
setContentIntent(pendingIntent)
// ...
}.build()
notificationManager.notify(
NOTIFICATION_TAG,
NOTIFICATION_ID,
notification
)

You can see that we built a standard type of Intent to open our app, and then simply wrapped it with a PendingIntent before adding it to the notification.

In this example, we constructed a PendingIntent that cannot be modified with the FLAG_IMMUTABLE flag because we know exactly what we need to do in the future.

The job is done after calling NotificationManagerCompat.notify(). When the system displays a notification, and the user clicks on the notification, PendingIntent.send() is called on our PendingIntent to start our application.

How to update the immutable pending intent?

You might think that if the application needs to update the PendingIntent, it needs to be a mutable type, but it’s NOT. The PendingIntent created by the application can be updated with the FLAG_UPDATE_CURRENT flag.

val updatedIntent = Intent(applicationContext, MainActivity::class.java).apply {
action = NOTIFICATION_ACTION
data = differentDeepLink
}
val updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
// This PendingIntent has been updated

3. Solution

So, the final solution is as below:

change from this :

Intent intent = new Intent(context, SMSRetryAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

to this:

PendingIntent pi=null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pi = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}else {
pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

4. Summary

In this post, I demonstrated how to solve the Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent problem when your android app change target sdk to 31 and above, the key point is adding a PendingIntent.FLAG_IMMUTABLE flag to your PendingIntent. That’s it, thanks for your reading.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

How to resolve IllegalArgumentException Could not resolve placeholder when using @Value in Spring Boot application

1. Introduction

When we read properties from files into Spring Boot applications, Sometimes, you will get exception of reading the properties.

This post will demo how to resolve the IllegalArgumentException:Could not resolve placeholder exception when using @Value in Spring app or Spring Boot apps.

2. Environments

  • Spring Boot

3. The Exception

When we use @Value like this:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp}")
private String myProp;
...
}

However, most importantly, if there is no myProp property in sysconfig.properties, we will get this exception:

Terminal window
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in value "${myProp}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:846)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1087)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1067)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:364)
... 35 common frames omitted

The key error message Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in value "${myProp}" indicated that there is no property named myProp in the custom properties file named sysconfig.properties.

4. How to fix it

4.1 Way 1: Add the property key to your properties file

src/main/resources/sysconfig.properties
myProp = test

4.2 Way 2: Provide a default value for the property

If you don’t want to provide the key in your file, you can just set @Value’s default value:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp:defaultValue}")
private String myProp;
...
}

You can change the defaultValue to your real default value.

4.3 Way 3: Provide an empty default value for the property

If you want to provide an empty default value, just do like this:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp:}")
private String myProp;
...
}

Notice the myProp: , the colon is important.

Just leave empty after the colon, then you would get an empty default value for property myProp.

5. Summary

You can see that @Value must be used carefully, if you get the above exceptions ,just provide the default values of the @Value.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!