问题描述
一个项目在windows下执行mvn package正常,但是放在Linux环境下,会出现找不到com.sun.image.codec的问题,如下所示
1 | [ERROR] COMPILATION ERROR : |
分析一
按照Java官方的解释,sun.image.codec这个package已经被deprecated,建议不要使用,看官方的解释:
Why Developers Should Not Write Programs That Call ‘sun’ Packages
The java., javax. and org.* packages documented in the Java Platform Standard Edition API Specification make up the official, supported, public interface. If a Java program directly calls only API in these packages, it will operate on all Java-compatible platforms, regardless of the underlying OS platform.
The sun.* packages are not part of the supported, public interface. A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform. Each company that implements the Java platform will do so in their own private way. The classes in sun.* are present in the JDK to support Oracle’s implementation of the Java platform: the sun.* classes are what make the Java platform classes work “under the covers” for Oracle’s JDK. These classes will not in general be present on another vendor’s Java platform. If your Java program asks for a class “sun.package.Foo” by name, it may fail with ClassNotFoundError, and you will have lost a major advantage of developing in Java.
Technically, nothing prevents your program from calling into sun.* by name. From one release to another, these classes may be removed, or they may be moved from one package to another, and it’s fairly likely that their interface (method names and signatures) will change. (From Oracle’s point of view, since we are committed to maintaining the Java platform, we need to be able to change sun.* to refine and enhance the platform.) In this case, even if you are willing to run only on Oracle’s implementation, you run the risk of a new version of the implementation breaking your program.
In general, writing java programs that rely on sun.* is risky: those classes are not portable, and are not supported
可以这样理解,Java已经被Oracle收购多年,里面再去出现sun的package算怎么回事。。O(∩_∩)O哈哈~,当然你还是可以去使用的,可以在pom.xml 经配置了下面的参数,排除无法引入jar包的问题,这个方法并不能解决我在Linux无法进行构建的的问题。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdkVersion}</source>
<target>${jdkVersion}</target>
<compilerArguments>
<verbose />
<bootclasspath>${JAVA_HOME}/jre/lib/rt.jar${path.separator}${JAVA_HOME}/jre/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
分析二
查看mvn仓库信息
root@30e8e1cd5962:/home/ztzx# mvn -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T19:49:05Z)
Maven home: /usr/share/maven
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF-8
OS name: “linux”, version: “3.10.0-693.el7.x86_64”, arch: “amd64”, family: “unix”
查看Java 信息
root@30e8e1cd5962:/home/ztzx# java -version
openjdk version “1.8.0_91”
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-1~bpo8+1-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)
与windows的运行环境相比,出Java的小版本不同外,还有一个区别,就是Oracle Java与OpenJdk的区别,google资料,发现OpenJdk已经在1.7版本移除了该package。OpenJdk移除jepg的package,将Linux 默认的OpenJdk替换为Oracle的Jdk,即可以解决问题
其他解决方法
JPEGImageEncoder类是SUN公司私有类
一般出现在这样的代码段中:
FileOutputStream out = new FileOutputStream(dstName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(dstImage);
改写成:
String formatName = dstName.substring(dstName.lastIndexOf(".") + 1);
//FileOutputStream out = new FileOutputStream(dstName);
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//encoder.encode(dstImage);
ImageIO.write(dstImage, /*"GIF"*/ formatName /* format desired */ , new File(dstName) /* target */ );
都使用统一的ImageIO进行图像格式文件的读写,没有必要使用过时的实现类JPEGImageEncoder类。