import com.sun.image.codec.jpeg.*
I have a Java program when I compile it I get the following error
[javac] ...\MyClass.java:33: package com.sun.image.codec.jpeg does not exist
[javac] import com.sun.image.codec.jpeg.*;
[javac] ^
what can I do ?
开发者_如何学编程What is the correct way of writing an image file now that the com.sun package is deprecated.
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.
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 RetiredDescription: 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 thecom.sun.image.codec.jpeg
package.
In general, try not to use classes from "sun." or "com.sun." packages, as this packages are considered implementation details, and are not part of the Java API.
They can cause problems when moving to new versions of Java because there is no backwards compatibility guarantee. Similarly, they can cause problems when moving to a different Java vendor, such as OpenJDK.
Such classes are almost always wrapped by Java API classes that should be used instead.
See: https://sonarcloud.io/organizations/default/rules?languages=java&open=squid%3AS1191&q=sun
Which JDK are you using? I think that this package is not a requirement and will only be available in Sun's JDK. This discussion explains.
You may be able to download the jar file and include it on your path file separately if you really want to use it though.
According to docs:
Note that the classes in the com.sun.image.codec.jpeg package are not part of the core Java APIs. They are a part of Sun's JDK and JRE distributions. Although other licensees may choose to distribute these classes, developers cannot depend on their availability in non-Sun implementations. We expect that equivalent functionality will eventually be available in a core API or standard extension.
Looks like you are using something that has been replaced. Maybe you are on Java 7?
Here is something that seems to describe this, and where to go to find it's proper replacement.
Add the below configuration in pom.xml for this issue
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<bootclasspath>
${java.home}/lib/rt.jar${path.separator}${java.home}/lib/javaws.jar${path.separator}${java.home}/lib/jce.jar
</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
It works for me.
精彩评论