-----------------------------------------------------------------

原创博文,如需转载请通知作者并注明出处!

博主:疲惫的豆豆

链接:http://www.cnblogs.com/dzblog/p/6971245.html

----------------------------------------------------------------

环境


Maven:3.0.5

Java:1.8.0_45

OS:Linux

问题


拿到一个java项目,想编译一下,报了如下错误:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /var/lib/jenkins/jobs/sample_xxx_pipeline/workspace/src/test/java/com/xxx/sxxxx/ib/pdf/PdfServiceTest.java:[,] error: package com.sun.image.codec.jpeg does not exist
[ERROR] /var/lib/jenkins/jobs/sample_xxx_pipeline/workspace/src/test/java/com/xxx/sxxxx/ib/pdf/PdfServiceTest.java:[,] error: package com.sun.image.codec.jpeg does not exist
[INFO] errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: .361s
[INFO] Finished at: Thu Jun :: CST
[INFO] Final Memory: 75M/1614M
[INFO] ------------------------------------------------------------------------

出错原因(来自stack overflow):

Why are you using classes in the package com.sun.image.codec.jpeg? You are not supposed to use those classes directly: Why Developers Should Not Write Programs That Call 'sun' Packages.

What does your program do? Does it just try to read or write a JPG image? That's very easy with the ImageIO API. See this tutorial: Writing/Saving an Image.

Addition
- The package com.sun.image.codec.jpeg has been removed in Java 7 as mentioned in the Java SE 7 and JDK 7 Compatibility Guide.

Synopsis: The Non-standard com.sun.image.codec.jpeg Package is Retired

Description: The com.sun.image.codec.jpeg package was added in JDK 1.2 (Dec 1998) as a non-standard way of controlling the loading and saving of JPEG format image files. This package was never part of the platform specification and it has been removed from the Java SE 7 release. The Java Image I/O API was added to the JDK 1.4 release as a standard API and eliminated the need for the com.sun.image.codec.jpeg package.

大意就是JDK7的时候,这个类已经退休了,以后凡是带sun.*的类库将不会被支持,毕竟sun已经被收购多年了,还sun,sun的,oracle看着心里多难受,全部给搞掉。请看官方说明

贴心的粘过来:

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.

解决问题


当然最好的办法是,按照oracle(我下意识写成了sun)的要求,使用新的API,不过推动项目owner来改,确实麻烦,毕竟我只是想编译一下看看。

网上失败的解决办法:

这个方法在我这里失败,修改pom.xml文件如下:可能有成功的小伙伴吧,要不往上也不会到处都是这个解决办法的转载。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>

很遗憾,在我这里报错,出错是Unable to find package java.lang in classpath or bootclasspath。

如果使用这种方案修改的,请确保你的${java.home}可用,或者使用mvn help:system命令查看所有环境变量,进行更改。

stack overflow有同病相怜者,地址

成功的解决方法

I had this problem when compiling with JDK 7. Strange enough Eclipse did not show this error, only javac did. The answer can be found in this Stackoverflow answer: javac uses a special symbol table that does not include all Sun-proprietary classes, and suppliying -XDignore.symbol.file makes the problem go away.

Of course, a much better solution is to rewrite the code without using the proprietary classes, but to support JDK 7 quickly, this option works.

首先我的pom.xml文件是这样的:

           <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

接下来,只需要添加一句话既可解决:

            <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-XDignore.symbol.file</compilerArgument>
</configuration>
</plugin>

再次编译,成功~~撒花~~~

END

[Maven]package com.sun.image.codec.jpeg does not exist的更多相关文章

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

    rt.jar 是sun公司内部使用的包,不建议外部使用,rt.jar是被用了的,但是里面的com.sun下面的包不被默认加载了, JAVA7之前是默认加载的,所有用JAVA7以前的JDK编译是通过的. ...

  2. 使用ANT编译项目报错 com.sun.image.codec.jpeg does not exist 解决方法

    项目开发中在对图片进行裁切处理的时候,有时候是会使用到 com.sun 包下的类时. 假设项目使用ant编译,会出现错误 com.sun.image.codec.jpeg does not exist ...

  3. jenkins构建项目时报错缺少com.sun.image.codec.jpeg包解决方案

    错误日志:error: package com.sun.image.codec.jpeg does not exist 网上找的一个项目,使用的是jdk1.7,除此之外其他服务器配置或是环境配置都是j ...

  4. maven 编译项目时:报com.sun.image.codec.jpeg不存在

    项目中用到图片处理相关的一些工具类,在eclipse开发工具内,程序并没有什么问题,都可以正常使用,项目也没有报错,但通过maven 进行编译打包时,则会报错: 程序包com.sun.image.co ...

  5. maven项目编译:程序包com.sun.image.codec.jpeg不存在 这个类文件的位置在jre/lib/rt.jar

    转载:http://superich2008.iteye.com/blog/2047830 失败提示信息为:程序包com.sun.image.codec.jpeg不存在 这个类文件的位置在jre/li ...

  6. 程序包com.sun.image.codec.jpeg不存在 问题的完美解决

    原文地址:http://my.oschina.net/zb0423/blog/86507 在使用Hudson进行打包的过程中,因为我们使用了一个pdf文件产生缩略图的功能,倒置添加的源码文件在mave ...

  7. java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec

    今天迁移老项目到linux服务器,jdk8 ,tomcat8.5遇到这个问题. java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.J ...

  8. import com.sun.image.codec.jpeg.JPEGCodec不通过 找不到包

    import com.sun.image.codec.jpeg.JPEGCodec;   在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg. ...

  9. import com.sun.image.codec.jpeg.JPEGCodec不通过 找不到包(转载)

    http://www.xuebuyuan.com/2008608.html 在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCode ...

随机推荐

  1. PostgreSQL时间段查询

    1.今日 select * from "表名" where to_date("时间字段"::text,'yyyy-mm-dd')=current_date 2. ...

  2. 通俗易懂--SVM算法讲解(算法+案例)

    1.SVM讲解 新闻分类案例 SVM是一个很复杂的算法,不是一篇博文就能够讲完的,所以此篇的定位是初学者能够接受的程度,并且讲的都是SVM的一种思想,通过此篇能够使读着会使用SVM就行,具体SVM的推 ...

  3. 异常:Caused by: java.sql.SQLException: Field 'cust_id' doesn't have a default value

    异常: 由Java.q.L.SqLExpExt引起:字段“CuSTyID”没有默认值 Caused by: java.sql.SQLException: Field 'cust_id' doesn't ...

  4. [少数派]如何学习Git

    用玩游戏的方式学习 Git 目录 为什么要学习 Git 怎么学习 Git Learn Git Branching 其他学习资源 用游戏的方式来学习,是一种有趣而高效的方式. 从刚接触电脑时的打字练习软 ...

  5. 0.计划用libgdx写一个六边形回合制slg兵棋游戏

    题主层是一个e社游戏迷,但是因为国家政策,e社已经放弃了中国市场,所以决定自己来做,暂时当一个副业 大致计划: 1,先完成一个类似将军的荣耀的战旗游戏 2.再在其基础上制作一个钢铁雄心或世2 3.然后 ...

  6. JavaScript -DOM 编程艺术 2nd 完

    今日看完了这本书,做完了最后一个综合性例子.说实话收获良多,终于明白前端-h5 具体做什么 越学习越无知,这个看来真是一个真理. 后期计划: 1.CSS + DIV 布局深入了解,重点实战 2.Jav ...

  7. UEditor可以如何直接复制word的图文内容到编辑器中?

    下载并打开工程: 文档的上传 运行: 复制随便一篇文档,粘贴进去. 通过粘贴后,文档以及图片被粘贴进来了,看看html代码:   图片全部使用img标签统一.传输进度条的效果也不错. 文档图片被放置在 ...

  8. 【转】python3 内循环中遍历map,遍历一遍后再次进入内循环,map为空

    今天在使用python map的过程中,发现了一个奇怪问题,map遍历完成后,再次访问map,发现map为空了,特记录下来,以备日后查看. 如下代码,期望的结果是每次从外循环进入内循环,map都从头开 ...

  9. xtrabackup单表备份与恢复

    官网最新版本下载地址 https://www.percona.com/downloads/XtraBackup/LATEST/ yum install percona-xtrabackup; [epe ...

  10. windows下Apache配置多个站点

    1. httpd.conf 找到以下两行去掉注释: # Include conf/extra/httpd-vhosts.conf # LoadModule vhost_alias_module mod ...