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

2

17

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?

Banjo answered 29/10, 2012 at 12:48 Comment(1)
I was using ByteOutputStream and getting this error. When I changed it to ByteArrayOutputStream, it worked like a charm. Never knew about JRE internal classes.Checked
H
9

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.

Hedvige answered 29/10, 2012 at 12:54 Comment(3)
Well that was a special solution, what about other classes? For example Unsafe? There is not a good known replacement, and using it requires to depend on sun.* classes, what can we do in gradle?Pianism
I'm unfortunately not familiar with that class. Might be a good topic for an entirely new question, though.Beluga
Since Java 6 there's no longer a need to go external to find Base64 utilities: They are in javax.xml.bind.DatatypeConverter which is technically part of JAXB but you can use it regardless if you use JAXB or not.Guaco
K
16

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>
            ...
Kellen answered 27/5, 2015 at 3:22 Comment(0)
H
9

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.

Hedvige answered 29/10, 2012 at 12:54 Comment(3)
Well that was a special solution, what about other classes? For example Unsafe? There is not a good known replacement, and using it requires to depend on sun.* classes, what can we do in gradle?Pianism
I'm unfortunately not familiar with that class. Might be a good topic for an entirely new question, though.Beluga
Since Java 6 there's no longer a need to go external to find Base64 utilities: They are in javax.xml.bind.DatatypeConverter which is technically part of JAXB but you can use it regardless if you use JAXB or not.Guaco

© 2022 - 2024 — McMap. All rights reserved.