Maven compilation error caused by Android environment methods - java

I'm quite new to Maven so apologies upfront. I'm attempting to build my application which I had previously written and built in Eclipse with no issues. I've attempted to convert the project to Maven structure and create a pom.xml file bu when I try to build the project I get Compilation Errors on all of the environment methods I have used. The output from attempting to build is as follows:
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/*me*/workspace/*app*-Final/src/main/java/com/*org*/android/
*app*/SystemTests.java:[124,35] cannot find symbol
symbol: method getStorageState(java.io.File)
location: class android.os.Environment
[ERROR] /C:/Users/*me*/workspace/*app*-Final/src/main/java/com/*org*/android/
*app*/SystemTests.java:[135,30] cannot find symbol
symbol: method getBlockSizeLong()
location: variable stat of type android.os.StatFs
[ERROR] /C:/Users/*me*/workspace/*app*-Final/src/main/java/com/*org*/android/
*app*/SystemTests.java:[136,32] cannot find symbol
symbol: method getBlockCountLong()
location: variable stat of type android.os.StatFs
[ERROR] /C:/Users/*me*/workspace/*app*-Final/src/main/java/com/*org*/android/
*app*/SystemTests.java:[190,34] cannot find symbol
symbol: method getBlockSizeLong()
location: variable stat of type android.os.StatFs
[ERROR] /C:/Users/*me*/workspace/*app*-Final/src/main/java/com/*org*/android/
*app*/SystemTests.java:[191,36] cannot find symbol
symbol: method getBlockCountLong()
location: variable stat of type android.os.StatFs
[INFO] 5 errors
and my pom.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.*org*.android</groupId>
<artifactId>*app*</artifactId>
<version>1.0.0</version>
<packaging>apk</packaging>
<name>*app name*</name>
<properties>
<!-- at test time this will be overridden with snapshot version -->
<it-plugin.version>4.3.0</it-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.simpligility.maven.plugins</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>${it-plugin.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.simpligility.maven.plugins</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<sdk>
<platform>19</platform>
</sdk>
</configuration>
</plugin>
</plugins>
</build>
</project>
Finally the output from mvn --version is:
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T12:57:3
7+01:00)
Maven home: C:\Program Files\Apache\maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_51\jre
Default locale: en_GB, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
Thanks in advance!

You can either create from the beginning a maven project in eclipse, or mavenize it after creation as a regular java project (right click on the eclipse project "convert to maven") -- if you have you installed the maven plugin (m2e). Make sure you clean and build.
Also, it seems that you are using on your system java8 (Java version: 1.8.0_51, vendor: Oracle Corporation), but in the pom you configure your maven-compliler-plugin to target JRE of version 1.7. Make sure you have this version installed, and path configured in eclipse (project properties-->java build path).
Do you have various modules in your maven project? pluginManagement is only a way to share the same plugin configuration across all your project modules. You would need to put the maven-compiler-plugin in plugins as well.

The solution involved changing the android version being used to 4.2.

Related

Could not find artifact jdk.tools

I am migrating my code from Java 1.8 to Java 11. I have done the below change in my pom.xml
From:
<java.version>1.8</java.version>
To:
<java.version>11</java.version>
Also changed the source and target in maven compiler as per below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
When I execute the command maven install I get the below error:
[ERROR] Failed to execute goal on project store: Could not resolve dependencies for project. Could not find artifact jdk.tools:jdk.tools:jar:1.7 at specified path C:\Program Files\Java\jdk-11.0.12/../lib/tools.jar -> [Help 1]
How do I solve this error? Please help

Maven Java compiler - compilation error using wrong compliance version

When Running install from Eclipse we have no issues as the compiler version is set to 1.8.
When running mvn install in terminal we get the following error.
ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project servicehelper: Compilation failure
[ERROR] try-with-resources is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable try-with-resources)
[ERROR]
When using mvn install -X we're seeing -target 1.5
However here are my java and javac versions
mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T14:39:06-05:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.5", arch: "x86_64", family: "mac"
java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
javac -version
javac 1.8.0_131
I seems like from every version i run that we should have 1.8 compliance but maven target continues to try and install with 1.5 compliance.
Add this plugin to your pom.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
</configuration>
</plugin>
The way to go is first to define the version of the used plugin which means:
<project>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
You should always pin plugin versions which means all used plugin in your build.
The other option to define the java version (source/target) can be done via:
<project>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Or you can omit the pluginManagement if you have already pinned the plugin version via a corporate pom parent and only give the above properties.
If you have to run Maven with another JDK than you need to compile/test your code you need to go via toolchains
Or you can define the compiler to use via fork option which means to hard code the location of your JDK in your pom file which is not a good idea.
You can try adding the tag java.version on your pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Maven can't compile using rt.jar

My project uses sun.security.tools.keytool to generate certificate under JDK 1.8 and this package can be found in rt.jar. According to Introduction to the Dependency Mechanism, System Dependencies, I can add rt.jar as a dependency to my project:
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>rt.jar</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
I'm pretty sure Maven found this jar file. However when I import sun.security.tools.keytool.Main, it still generates an error. Moreover, the most strange thing is if I copy rt.jar into someplace and fill its path in pom.xml, it works! As soon as I switch back to use the original rt.jar, it fails!
Can anyone tell me how could this happen?
I created a Maven project and added the <dependency> of your question to its POM.
First I got:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[6,34]
package sun.security.tools.keytool does not exist
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[12,50]
cannot find symbol
symbol: variable Main
location: class igb.so.SO31353565
Then, according to Cannot find symbol (CertAndKeyGen) with JDK8, I added:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<fork>true</fork>
<compilerArgument>-XDignore.symbol.file</compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
to the POM and the compilation succeeded.
If you are using Gradle instead of Maven you can add this to your build:
compileJava {
options.fork = true
options.forkOptions.executable = 'javac'
options.compilerArgs << "-XDignore.symbol.file"
}
It worked for me! ;)
I solved it like this:
Add to JAVA_HOME/lib/rt.jar
Go to Eclipse Menu Windows -> Windows -> Preferences -> Java -> Installed JREs
Select jdk version. In me case jdk-11.0.4
Select Add External JARs.. and add JAVA_HOME/lib/rt.jar

Maven can't compile class which depends on rt.jar

CI-server (Hudson), for which I am responsible, builds Maven project. After the last commit, the build failed:
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \hudson\jobs\path to my class\MyClass.java:[33,62] package com.sun.xml.internal.messaging.saaj.packaging.mime.util does not exist
[ERROR] \hudson\jobs\path to my class\MyClass.java:[75,5] cannot find symbol
symbol : class BASE64EncoderStream
location: class |fullname of MyClass|
[ERROR] \hudson\jobs\path to my class\MyClass.java:[75,38] cannot find symbol
symbol : class BASE64EncoderStream
location: class |fullname of MyClass|
[INFO] 3 errors
Required class (com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64EncoderStream) is situated in rt.jar.
I tried (In accordance with the instructions at http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies) to add system dependency in project's pom.xml:
<dependency>
<groupId>dummy</groupId>
<artifactId>dummy</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
It did not help.
The most interesting is that all files compiled fine on the local machine of my collegue (he use Eclipse build-in compiler).
In internet I found the same question (link: http://maven.40175.n5.nabble.com/Why-can-t-Maven-find-com-sun-xml-internal-messaging-saaj-util-ByteOutputStream-class-td107361.html). The last answer was that the reason for this trouble is Oracle's Java compiler.
So, I changed Oracle's jdk to OpenJDK, but it did not help.
Does someone have any suggestions on how to solve this problem?
Need to specify -XDignore.symbol.file and add rt.jar dependency and <fork>true</fork> as the compiler plugin will otherwise silently drop any -XD flags: e.g.
...
<dependency>
<groupId>groupid</groupId>
<artifactId>artifiactId</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
<fork>true</fork>
</configuration>
...
The missing class seems to be JRE internal (as indicated in its namespace), and should not be referenced from your code. It is probably only available on specific platforms or JRE versions.
Consider replacing it with another Base64 encoder class, e.g. one from the Apache Commmons Codec project.
Java 8 update
Java 8 finally introduced a Base64 class in the public part of the JDK: java.util.Base64.

Maven: Attaching multiple artifacts

I have a maven project that uses some custom jars [not found in any repository]. To add them along with the Maven build I am using the attach-artifact goal in maven. Following is my pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maximus</groupId>
<artifactId>adminbuild</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>adminbuild</name>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>target</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar1.jar</file>
<type>jar</type>
</artifact>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar2.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
My projects that use these jars [customjar1.jar, customjar2.jar] have their dependency on the above pom file [adminbuild].
When I execute the mvn clean install command, I get the following error:
[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.7:attach-artifact (attach-artifacts) on project adminbuild: The artifact with same type and classifier: jar:null is used more than once. -> [Help 1]
Following is the output of mvn -version command:
Apache Maven 3.0.3 (r1075438; 2011-02-28 23:01:09+0530)
Maven home: C:\maven
Java version: 1.6.0_26, vendor: Sun Microsystems Inc.
Java home: C:\Java\jdk1.6.0_26\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
It seems that the way I am attaching the artifacts is incorrect. Should I not attach multiple artifacts in the same pom file? If yes then how. Please help.
Attached artifacts are normally used to install additional files created by your build, like the classes jar of a webapp, documentation or sources.
To add files to your maven repository so they are available as dependencies you should use the install-file goal.
Edit: Attached artifacts are identified by the same groupId and artifactId as your main project but with a different classifier. In your configuration, you did not specify the classifier, hence the error message.
You would want to have these custom jars in a repository manager like nexus so that they can be downloaded/used like a normal dependency jar.
Assuming that is not possible and seeing that these jars are in well-known location, perhaps you could specify these custom jars with system scope in the projects that need them?
<project>
...
<dependencies>
<dependency>
<groupId>my-group</groupId>
<artifactId>customjar1</artifactId>
<version>a.b.c</version>
<scope>system</scope>
<systemPath>${basedir}/../../resources/dependencies/java/customjar1.jar</systemPath>
</dependency>
</dependencies>
...
</project>
I used a secondary project to do this along with a multi-module.
https://stackoverflow.com/q/7908872/242042

Categories

Resources