Maven can't compile using rt.jar - java

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

Related

Why can't Idea not find sources generated by ANTLR4?

I have a project in which I want to use classes generated by ANTLR4 in a piece of Kotlin code.
In pom.xml, ANTLR4 is configured as follows.
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7.1</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
The generated classes are put into target/generated-sources/antlr4:
mvn clean package, mvn clean assembly, as well as rebuilding the project in Idea lead to the following error:
Note that the errors occur only in the Kotlin class Transpiler.kt, but not in the test.
How can I fix this (make sure that classes generated by ANTLR4 can be used in Kotlin code)?
Update 1: After moving the grammar file as suggested by #Bart Kiers and executing mvn clean antlr4:antlr4, the errors in Idea disappeared. However mvn clean antlr4:antlr4 install still results in build errors:
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.4.21:compile (compile) on project elispt: Compilation failure: Compilation failure:
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[9,21] Unresolved reference: DeplorableLexer
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[11,22] Unresolved reference: DeplorableParser
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[12,21] Unresolved reference: DeplorableParser
It should work if you do the following:
move Deplorable.g4 to src/main/antlr4/com/dpisarenko/deplorable/ (note that you placed it inside src/main/antlr4/com.dpisarenko.deplorable/!)
run mvn clean antlr4:antlr4
if not already done, mark target/generated-sources/antlr4 as the "Generated Sources Root" (right click it in your IDE and select Mark Directory as)
If not, try using the latest ANTLR4 version: 4.9.1 (not just the tool and runtime, but also for antlr4-maven-plugin).

Maven, Webstart, and Java 8 Headaches

I am trying to convert a project from compiling with Java 6 to Java 8. We are using the webstart-maven-plugin for which there is currently a workaround (http://mojo.10943.n7.nabble.com/jira-MWEBSTART-269-Java-8-support-td44357.html) for compiling with Java 8 by adding the following dependencies to the plugin definition.
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>webstart-maven-plugin</artifactId>
<version>1.0-beta-6</version>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>webstart-pack200-impl</artifactId>
<version>1.0-beta-6</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>keytool-api-1.7</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
...
</plugin>
</plugins>
</pluginManagement>
</build>
...
This got me past my initial issues.
I am now receiving the following error.
[ERROR] Failed to execute goal org.codehaus.mojo:webstart-maven-plugin:1.0-beta-6:jnlp-inline (default) on project <redacted>: Unable to parse configuration of mojo org.codehaus.mojo:webstart-maven-plugin:1.0-beta-6:jnlp-inline for parameter pack200: Cannot find default setter in class org.codehaus.mojo.webstart.Pack200Config -> [Help 1]
The Help link goes to the following page.
https://cwiki.apache.org/confluence/display/MAVEN/PluginConfigurationException
As far as I can figure out, the webstart-pack200-impl dependency requires some configuration to define which setter is used. Any information regarding setters that I have found online seems to be a different issue from this. I can't figure out if there is a way to set a configuration for a dependency.
Or am I looking at this in a completely incorrect way?
Many thanks in advance
the error points to pack200 which was configured as <pack200>false</pack200>in older version of webstart plugin configuration.
This can be resolved by changing pack200 configuration to this instead (within <configuration> section of the plugin settings)
<pack200><enabled>false</enabled></pack200>
for more details please refer http://www.mojohaus.org/webstart/webstart-maven-plugin/upgrade.html section "Important changes since 1.0-beta-3"

POM for Maven Compiler Plugin Jar is missing

EDIT: I've just updated my Eclipse installation from Kepler to Luna - all of my Maven projects were running fine before the update
I am receiving the error which is preventing my Maven project from installing:
The POM for org.apache.maven.plugins:maven-compiler-plugin:jar:2.5.1 is missing, no dependency information available
And when I navigate to Maven's Lifecycle Mapping in Eclipse I see
compiler:compile | error
compiler:text Compile | error
I have maven-compiler-plugin:jar:3.1 (including the POM file) in my .m2 repository and would like to use that instead.
How could this be configured in Eclipse? Alternatively, if this is not the solution to the problem, how could this be resolved?
You need to edit your pom.xml to set the version of the compiler plugin to be used. As described on the plugin's homepage, you configure the maven-compiler-plugin in the build-section of your project's pom.xml like so:
<project>
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- put your configurations here -->
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
...
</project>
If you want to use the local repository of your Maven installation (instead of Eclipse's embedded version), go to Preferences -> Maven -> Installations and add your Maven installation there.

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 "class file for ... not found" compilation error

I have a Maven project. When I try to build it with Maven, I get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.
3.2:compile (default-compile) on project myProject: Compilation failure:
Compilation failure:
[ERROR] ClassA.java:[32,38] cannot access ClassB
[ERROR] class file for ClassB not found
ClassB is inside another artifact, and that artifact is in the local repository. In fact, I have no problems building this project with the m2eclipse Maven plugin. It's only when I run mvn compile that the build fails.
What do I have to do to build from the command line?
Eclipse might be bypassing your pom dependencies and 'helping' you by finding a dependency that is not in your pom. Then when you run from command line eclipse isn't there to help anymore. I would double check your pom that you explicitly state dependency. You can also try
mvn dependency:analyze
for more info.
That looks like you have defined a usual class within the src/test/java instead of the src/main/java area which is sometimes the problem cause the eclipse things behaves a little bit different.
add to pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>src\main\webapp\WEB-INF\lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>

Categories

Resources