Java Maven Error Invalid Flag --add-opens - java

I implemented GSON in my program (Java (16), Maven). I encountered the "InaccessibleObjectException" and by research I found out I had to add "add-opens ..." to the vm command line. Eventually I got the program running and working by adding it. But now I have the issue, once I restart my program in my IDE (intelliJ) I get the error message java: error: invalid flag: --add-opens java.base/java.time=ALL-UNNAMED.
Weird thing is, this doesn't even happen all the time, it feels like it is random because sometimes I can restart my program without any issues and I can fix it by deleting it, restarting, adding it, restarting again..
I am absolutely clueless hence I made this account here. How can I fix this?
My pom build looks like this
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<jvm.options>--add-opens java.base/java.time=ALL-UNNAMED</jvm.options>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>16</source>
<target>16</target>
<compilerArgs>
<arg>${jvm.options}</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.14.0</version>
<configuration>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.9</targetJdk>
<excludes>
<exclude>**/*Bean.java</exclude>
<exclude>**/generated/*.java</exclude>
</excludes>
<excludeRoots>
<excludeRoot>target/generated-sources/stubs</excludeRoot>
</excludeRoots>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
</plugins>
</build>

Related

While Upgrading JDK version from 8 to 11 for the maven project, test is getting failed

Error:
I am trying to upgrade jdk from 1.8 to 11. But it is giving me an error.
Test cases are getting failed.
<profiles>
<profile>
<id>surefire</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>TestNG.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>
--add-opens java.xml/jdk.xml.internal=ALL-UNNAMED
-Xmx1024m</argLine>
<forkCount>1</forkCount>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Manager:
<pluginManagement>
<plugins>
<!-- not required this plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>false</skipTests>
<argLine>
--add-opens java.xml/jdk.xml.internal=ALL-UNNAMED
</argLine>
<excludes>
<exclude>**/*RegressionTest.java</exclude>
</excludes>
<forkCount>1</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.3.0.603</version>
</plugin>
</plugins>
</pluginManagement>
I have tried with changing the surefire version. So issue get changed. But I am not able to get the solution for the above error.

How to include all the files from JRE while bundling

I'm using the javafx-maven-plugin for creating native installer. It all works fine. But inside runtime folder I would like to have all the files inside JRE. but now all the .exe file are missing. Can anyone please tell me how can I achieve this.
following is the pom file I'm using.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<vendor>mos</vendor>
<mainClass>com.smatt.morse.MorseCodeTranslator</mainClass>
</configuration>
</plugin>
</plugins>
</build>
and I'm calling mvn jfx:native cmd.
Thanks

Java program not reading environment variable set from command line

In my java program I am trying to use few native libraries. Everything works fine if I use my intelij IDE to run code, however if I ran the code from command line (from terminal) it gives me library linkage error
I am using Mac OS Sierra and Java 1.8
Following is my pom.xml looks like.
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>${start-class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.21.0</version>
</dependency>
</dependencies>
<configuration>
<forkMode>once</forkMode>
<argLine>-Djava.library.path=/Applications/CPLEX_Studio_Community128/opl/bin/x86-64_osx/</argLine>
<environmentVariables>
<DYLD_LIBRARY_PATH>/Applications/CPLEX_Studio_Community128/opl/bin/x86-64_osx/</DYLD_LIBRARY_PATH>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
Java code
#Test
public void testCPLX(){
System.out.println("DYLD_LIBRARY_PATH env: " +System.getenv("DYLD_LIBRARY_PATH"));
IloOplFactory oplF = new IloOplFactory();
}
if I run this code form InteliJ IDE it runs and prints the value of DYLD_LIBRARY_PATH as /Applications/CPLEX_Studio_Community128/opl/bin/x86-64_osx/
However if run the program using following command.
mvn clean test -Dtest=test.java.cplexTests.TestCplex
I get null for DYLD_LIBRARY_PATH and so the library linkage error in next step.
I tried setting DYLD_LIBRARY_PATH path before running mvn command however it's not helping. I tried setting DYLD_LIBRARY_PATH as part of mvn command, it's not helping either.
First Try
export DYLD_LIBRARY_PATH=/Applications/CPLEX_Studio_Community128/opl/bin/x86-64_osx/
mvn clean test -Dtest=test.java.cplexTests.TestCplex
Second Try
mvn -DDYLD_LIBRARY_PATH=/Applications/CPLEX_Studio_Community128/opl/bin/x86-64_osx/ -Djava.library.path=/Applications/CPLEX_Studio_Community128/opl/bin/x86-64_osx/ -Dtest=test.java.cplexTests.TestCplex clean test
If I remove environmentVariables section from maven I get same error when I run through InteliJ Idea.

Maven gives a java version error (ask for java 1.5) even when I have java 1.7 installed

I'm getting an error saying
(use -source 5 or higher to enable annotations) {class path} error:
for-each loops are not supported in -source 1.3
when I try to compile a module using maven.
The thing is that the java version in my machine is 1.7.0_02
Can anyone suggest a solution?
You may want to include this in your pom.xml file as I had the same problem:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument></compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Please, check the value of the JAVA_HOME environment variable.
For the user acc which is used by maven
The shortest version is to set maven.compiler.source and maven.compiler.target properties in your pom.xml:
<project>
....
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<compilerArgument></compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
by blackpanther
this other too
<project>
....
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
by rzymek
all together
and clean and build the proyect and that's work fine vatos!!!
<build>
<finalName>your project name</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>

maven 3 + pmd fails when code has annotations and generics

When I run mvn pmd:pmd I get an warning / error from PMD saying that the code cannot use generics, annotations or Enums with java 1.4 or 1.5. This is supposed to be a error when the targetJdk property is not set (which I did set).
Here is an example of the error I get
[WARNING] Error while parsing /Users/augusto/Downloads/PMD-Clover2-Cobertura-Maven2-Test/PMDTest/src/main/java/org/xh/studies/quality/App.java: Can't use generics unless running in JDK 1.5 mode!
and here's a snippet of the pom I'm using (there's a link to the whole app below)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.5</version>
<configuration>
<targetJdk>1.6</targetJdk>
</configuration>
</plugin>
</plugins>
</reporting>
I'm using Maven 3.0.2 on mac, with the latest java patch 1.6.0_24.
Click here to download the full project (8kb)
EDIT (26/03/2014)
Since version 3.3 of the maven site plugin, maven can also use the old style configuration (and in fact the maven 2 style is now recommended). Links: docs, jira.
Original answer
Pain, the answer is that the site generation changed completely from maven 2 to maven 3. There is an explanation on this blog. Note that mvn pmd:pmd doesn't work anyway with the new reporting configuration, but it works as part of the site generation.
And here's an example of a pom that works.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0-beta-2</version>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<dependencyDetailsEnabled>false</dependencyDetailsEnabled>
<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javancss-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jdepend-maven-plugin</artifactId>
<version>2.0-beta-2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.5</version>
<configuration>
<linkXRef>false</linkXRef>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>${java-version}</targetJdk>
<verbose>true</verbose>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.6</version>
</plugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>

Categories