[Maven]package com.sun.image.codec.jpeg does not exist

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

原创博文,如需转载请注明出处!

博主:疲惫的豆豆

链接: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:[6,31] 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:[7,31] error: package com.sun.image.codec.jpeg does not exist
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.361s
[INFO] Finished at: Thu Jun 08 19:16:04 CST 2017
[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

时间: 2024-08-03 13:59:24

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

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

jenkins bulid时报错,是因为com.sun.image.codec.jpeg是sun公司私有包,oracle在1.7后不再支持 修改下代码即可解决问题 原code: BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); FileOutputStream out = new FileOutputStream(dir+filename); JP

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

项目开发中在对图片进行裁切处理的时候,有时候是会使用到 com.sun 包下的类时, 如果项目使用ant编译,会出现错误 com.sun.image.codec.jpeg does not exist 这是因为在JDK1.7+时,Oracle不允许使用sun.*的jar 具体参见http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html . 项目代码已经写好,且直接运行可以正常使用,只是使用ant编译会出现错误,现在不打

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

项目中用到图片处理相关的一些工具类,在eclipse开发工具内,程序并没有什么问题,都可以正常使用,项目也没有报错,但通过maven 进行编译打包时,则会报错: 程序包com.sun.image.codec.jpeg不存在 . 从包名看,是sun公司之前的一些类,在比较老的一些代码中,常用来处理图片的格式转换.截取等等. 类似以下代码: 1 FileOutputStream fos= new FileOutputStream(targetFile); 2 JPEGImageEncoder enc

Maven打包时:com.sun.image.codec.jpeg不存在的解决方案

报这个错误的原因是,Maven没有权限操作jre自带的jar包. 解决方法:在pom.xml添加以下依赖: <span style="font-size:18px;"><dependency> <groupId>jce</groupId> <artifactId>jce</artifactId> <version>1.0</version> </dependency> <d

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

这两天都快被这个错误整疯了!!!最近在写java项目的时候需要用到 [java] view plain copy import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; 这两个类来把图片写入磁盘.注意:这两个类可是处在系统自带的jre里的rt.jar包里 [java] view plain copy FileOutputStream out = new FileOut

import com.sun.image.codec.jpeg.JPEGImageEncoder报错解决办法

今天在写一个Java Web程序时,在处理JPEG图片时,需要导入com.sun.image.codec.jpeg.JPEGImageEncoder和com.sun.image.codec.jpeg.JPEGCodec这两个包,导入后报错如下: Access restriction: The type 'JPEGImageEncoder' is not API (restriction on required library 'E:\MyEclipse 2015\binary\com.sun.j

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

http://www.xuebuyuan.com/2008608.html 在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;报错:Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required l

导入import com.sun.image.codec.jpeg.JPEGCodec出错

在Eclipse中处理图片时,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder; 报错,编译不通过解决办法:Eclipse默认把这些受访问限制的API设成了ERROR.只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbi

编写response生成图片验证码时,报import com.sun.image.codec.jpeg.JPEGCodec;

在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;报错:Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar