1. 程式人生 > >[Maven]package com.sun.image.codec.jpeg does not exist

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

sym 接下來 workspace lock nis program uic edit 問題

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

原創博文,如需轉載請註明出處!

博主:疲憊的豆豆

鏈接: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

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