Java program not reading environment variable set from command line - java

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.

Related

Type Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerial [duplicate]

When I am creating a Maven project I am getting this error in pom.xml
Could not initialize class
org.apache.maven.plugin.war.util.WebappStructureSerializer
-Maven Configuration Problem
web.xml is missing and <failOnMissingWebXml> is set to true.
It might because the version of maven-war-plugin is too old in your project.
Try to add below code in your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
</project>
Note: you should have the build tag
place everything inside build tag then, it will not throw error
If you are getting this error within Eclipse (my Eclipse version was Version: 2021-09 (4.21.0), Build id: 20210910-1417 at the time of writing) and you recently tried to move to JDK 17, it appears Eclipse is not compatible with JDK 17. It requires at least JDK 11. I reverted back to Java 11.0.12 and the error has gone away.
I'm using Windows 10 and Eclipse Version: 2022-03 (4.23.0) Build id: 20220310-1457
My project uses JDK8, but newer versions of Eclipse require at least JDK11.
I configure this using Window->Preferences->Java->Installed JREs
as shown in the screenshot:
Maven from windows cmd shell works fine: mvn clean package
But, Eclipse gives an error on my pom.xml: Could not initialize class org.apache.maven.plugins.war.util.WebappStructureSerializer
For reference, Eclipse gave some other error messages while I was attempting to fix the issue, such as this one: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module
This error seems to be some compatibility issue with maven-war-plugin and the JDK17 that eclipse runs under. It's probably due to the 'module' permissions.
Using these helpful answers, I was able to find a workaround:
#Capricorn1 https://stackoverflow.com/a/69544567/4505142
#SreedharGS https://stackoverflow.com/a/29383652/4505142
I downloaded OpenJDK11 from: https://adoptium.net/
Updated eclipse.ini to use JDK11 instead of JDK17:
--launcher.appendVmargs
-vm
C:\Users\User1\OpenJDK11\jdk-11.0.15+10\bin
-vmargs
-Dosgi.requiredJavaVersion=11
-Dosgi.instance.area.default=#user.home/eclipse-workspace
-Dsun.java.command=Eclipse
-XX:+UseG1GC
-XX:+UseStringDeduplication
--add-modules=ALL-SYSTEM
-Dosgi.requiredJavaVersion=11
-Dosgi.dataAreaRequiresExplicitInit=true
-Dorg.eclipse.swt.graphics.Resource.reportNonDisposed=true
-Xms512m
-Xmx1024m
--add-modules=ALL-SYSTEM
For reference, here's the original eclipse.ini JDK17 path:
-vm
plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.2.v20220201-1208/jre/bin
Using JDK11 to run Eclipse, eliminated the pom.xml error "Could not initialize class org.apache.maven.plugins.war.util.WebappStructureSerializer"
For reference, here are the relevant snippets of my pom.xml:
<properties>
<spring-boot.version>1.5.13.RELEASE</spring-boot.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
<maven-war-plugin.version>3.0.0</maven-war-plugin.version>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<packagingExcludes>**/application-*.properties</packagingExcludes>
</configuration>
<executions>
<execution>
<id>default-war</id>
<phase>prepare-package</phase>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
In Pom.xml, need to add the below snippet, and please find image attachment
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
After update Maven Project
enter image description here
Please use the below code snip:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</pluginManagement>
Please refer this
Try to add the plugin to your maven project, by adding this in your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
After that: Right click -> Maven -> Update Project
I have the same problem as this when I create a maven project. So I got a solution for this just adding this plug into your pom.xml:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
If you already have it, then you should upgrade its version.

Maven command runs fine on local machine, but not in Jenkins

So I have a maven project that has a profile
When I run the following in the terminal for my project, the tests will run just fine
mvn clean install -Pserial-integration-test -Dcucumber.options="--tags #'QR_Scan'" -Dtest.environment=PrePRod
When I run the following in a Jenkins project it will not run the tests that I need it too
clean install -Pserial-integration-test -Dcucumber.options="--tags #'QR_Scan'" -Dtest.environment=PrePRod
Jenkins settings
I have setup a Jenkins Maven project and told Jenkins where my Pom is, and I send the command below for the goal and options "clean install -Pserial-integration-test -Dcucumber.options="--tags #'QR_Scan'" -Dtest.environment=PrePRod"
Maven Settings
serial-integration-test
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</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.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<junitPlatformArtifactName>com.github.junit-team.junit5:junit-platform-engine</junitPlatformArtifactName>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/Parallel*IT.class</include>
</includes>
<systemPropertyVariables>
<test.environment></test.environment>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>validate</phase>
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<plugins>
<plugin>
<name>com.gm.onstar.rita.cucumber.CucumberReporter</name>
</plugin>
</plugins>
<glue>
<package>com.gm.onstar.rita.stepdefinitions</package>
</glue>
<featuresDirectory>src/main/resources/features</featuresDirectory>
<tags>
<tag>${cucumber.tag1}</tag>
<tag>${cucumber.tag2}</tag>
<tag>${cucumber.tag3}</tag>
<tag>${cucumber.tag4}</tag>
<tag>${cucumber.tag5}</tag>
<tag>~#Ignore</tag>
</tags>
</configuration>
</execution>
</executions>
</plugin>
What I have tried
I have tried putting in like this in Jenkins -Dcucumber.options="--tags #QR_Scan" and like this -Dcucumber.options='--tags #QR_Scan'
I have tried putting the profile -Pserial-integration-test with a space and without a space -P serial-integration-test
I have tried to see if I a missing some sort of plugin
I have tried to see if Mvn clean install or Mvn clear verify would run it on Jenkins
What it might be.
Jenkins does not like that my cucumber runner is in the main and not in java test folder?
Am I missing a plugin for Jenkins?
Am I typing the command in wrong in jenkins, and maybe missing some parentheses or something?
Any help would be appreciated. Thank you!

Java Maven Error Invalid Flag --add-opens

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>

How to run java program using Maven

I am trying to run a Java project using Maven, need help on how to run
I tried with various options, Run As > Maven clean, Run As > Maven Install, Run As > Maven test, etc.
But the output is not showing in the console though build is successful
I am using eclipse and able to run java file using Run As > Java Application
My Build tag in pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>mypackage.classnamehavingmain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
You should add the goal for execution
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>your_goal_name</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
and then to execute you can perform :
mvn exec:your_goal_name
If you would like to run it from the terminal you can do mvn compile exec:java -Dexec.mainClass=mypackage.classnamehavingmain.

Deploy Java app to Heroku using Git

My project written using the Spark framework has this build config.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<verbose>true</verbose>
<fork>true</fork>
<compilerVersion>1.7</compilerVersion>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.avaje</groupId>
<artifactId>ebean-maven-enhancement-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>main</id>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
<configuration>
<classSource>target/classes</classSource>
<packages>somepath.domain.**</packages>
<transformArgs>debug=1</transformArgs>
</configuration>
</plugin>
</plugins>
</build>
Its main class resides in somepath.Bootstrap.
The Procfile sits next to the pom.
What should the contents of the Procfile be?
(I am deploying via git.)
Procfile contains command, that will be executed after deploying your appliaction (git push heroku master for you example i guess).
I do not very familiar with spark, but i guess it can be something like this:
web: mvn exec:java -Dexec.mainClass="somepath.Bootstrap"
A Procfile is a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform.
Here is an example for a Java app:
web: java -cp target/classes:target/dependency/* Main
This is taken from a Heroku Java sample app that uses Spark.
Be aware that build tools, such as Maven, will not be available at runtime.

Categories