Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set jdk.attach.allowAttachSelf=true globally

Tags:

java

java-9

I am trying to move one of our systems from java 8 to java 9 and about one third of the unit tests that worked OK in java 8 fails with the error below:

java.io.IOException: Can not attach to current VM

Google took me to a few pages and I quickly understood that in java 9 the default behavior was changed to prevent attaching to the current VM and to go back to the old way you need to set the system property jdk.attach.allowAttachSelf to true.

When setting this in IntelliJ the tests work fine. The same works when changing build.gradle to include this:

test {
    jvmArgs '-Djdk.attach.allowAttachSelf=true'
}

However, I would rather prefer to have this setting globally so I don't need to hack my build.gradle and IntelliJ.

I am running java 9 on ubuntu and I changed /etc/profile.d/jdk.sh to include this:

export JDK_JAVA_OPTIONS="-Djdk.attach.allowAttachSelf=true"

When running my Gradle build I can see the setting being picked up because I am getting below in the build output:

NOTE: Picked up JDK_JAVA_OPTIONS: -Djdk.attach.allowAttachSelf=true

However, the tests keep failing with the same IOException.

So what I am doing wrong and how should I fix it?

Thank you in advance for your inputs.

like image 607
Julian Avatar asked Sep 13 '25 14:09

Julian


1 Answers

My tests were running using surefire plugin, If said vm argument added in argline, then test started working.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.14.1</version>
     <configuration>
       <argLine>-Djdk.attach.allowAttachSelf=true </argLine>
     </configuration>
</plugin>
like image 84
Deepak Lokhande Avatar answered Sep 15 '25 05:09

Deepak Lokhande