com.sun.image.codec.jpeg does not exist

问题描述

一个项目在windows下执行mvn package正常,但是放在Linux环境下,会出现找不到com.sun.image.codec的问题,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController2.java:[24,40] package com.sun.image.codec.jpeg does not exist
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController2.java:[25,40] package com.sun.image.codec.jpeg does not exist
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController.java:[25,32] package com.sun.image.codec.jpeg does not exist
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController.java:[26,32] package com.sun.image.codec.jpeg does not exist
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] ztzx 1.0.0 ......................................... SUCCESS [ 0.007 s]
[INFO] ztzx.service ....................................... FAILURE [02:11 min]
[INFO] ztzx.web 1.0.0 ..................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:11 min
[INFO] Finished at: 2018-08-06T01:51:45Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project ztzx.service: Compilation failure: Compilation failure:
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController2.java:[24,40] package com.sun.image.codec.jpeg does not exist
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController2.java:[25,40] package com.sun.image.codec.jpeg does not exist
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController.java:[25,32] package com.sun.image.codec.jpeg does not exist
[ERROR] /home/ztzx/ztzx.service/src/main/java/com/telchina/ztzx/common/controller/UploadController.java:[26,32] package com.sun.image.codec.jpeg does not exist
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :ztzx.service

分析一

按照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无法进行构建的的问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
<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仓库信息

1
2
3
4
5
6
7
8
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)

1
2
3
4
5
与windows的运行环境相比,出Java的小版本不同外,还有一个区别,就是Oracle Java与OpenJdk的区别,google资料,发现OpenJdk已经在1.7版本移除了该package。[OpenJdk移除jepg的package](http://jira.icesoft.org/browse/PDF-332?jql=project%20%3D%20PDF%20AND%20fixVersion%20%3D%205.1%20ORDER%20BY%20updated%20DESC%2C%20priority%20DESC%2C%20created%20ASC),将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类。