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

Posted 快乐的GTD吧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec相关的知识,希望对你有一定的参考价值。

这两天都快被这个错误整疯了!!!
最近在写java项目的时候需要用到

[java] view plain copy
 
  1. import com.sun.image.codec.jpeg.JPEGCodec;  
  2. import com.sun.image.codec.jpeg.JPEGImageEncoder;  

这两个类来把图片写入磁盘。注意:这两个类可是处在系统自带的jre里的rt.jar包里

 

[java] view plain copy
 
  1. FileOutputStream out = new FileOutputStream(targetFile);  
  2. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  3. encoder.encode(mBufferedImage);  

 

本来在本地Eclipse测试的好好的,但是部署到Linux服务器的时候就出错了

 

[java] view plain copy
 
  1. java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec  
  2.         at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)  
  3.         at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)  
  4.         at com.vxpai.wechat.util.ImgUtil.imageScale(ImgUtil.java:72)  
  5.         at com.vxpai.wechat.webcontroller.UploadController.ss(UploadController.java:68)  
  6.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  7.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
  8.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  9.         at java.lang.reflect.Method.invoke(Method.java:606)  
  10.         at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)  
  11.         at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)  
  12.         at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)  
  13.         at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)  
  14.         at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)  
  15.         at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)  
  16.         at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)  
  17.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)  
  18.         at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)  
  19.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)  
  20.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)  
  21.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
  22.         at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)  
  23.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
  24.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
  25.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
  26.         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)  
  27.         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)  
  28.         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)  
  29.         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)  
  30.         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)  
  31.         at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)  
  32.         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  
  33.         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)  
  34.         at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)  
  35.         at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)  
  36.         at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)  
  37.         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
  38.         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
  39.         at java.lang.Thread.run(Thread.java:745)  

刚开始的时候以为是没有引入rt.jar,将其用maven(我的项目是基于maven建立的)引入,部署上去之后居然还是报错:

 

 

[java] view plain copy
 
  1. java.lang.UnsatisfiedLinkError: no jpeg in java.library.path  
  2.         at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)  
  3.         at java.lang.Runtime.loadLibrary0(Runtime.java:849)  
  4.         at java.lang.System.loadLibrary(System.java:1088)  
  5.         at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:67)  
  6.         at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:47)  
  7.         at java.security.AccessController.doPrivileged(Native Method)  
  8.         at sun.awt.image.codec.JPEGImageEncoderImpl.<clinit>(JPEGImageEncoderImpl.java:73)  
  9.         at com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(JPEGCodec.java:68)  
  10.         at com.vxpai.wechat.util.ImgUtil.imageScale(ImgUtil.java:72)  
  11.         at com.vxpai.wechat.webcontroller.UploadController.ss(UploadController.java:68)  
  12.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  13.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
  14.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  15.         at java.lang.reflect.Method.invoke(Method.java:606)  
  16.         at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)  
  17.         at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)  
  18.         at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)  
  19.         at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)  
  20.         at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)  
  21.         at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)  
  22.         at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)  
  23.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)  
  24.         at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)  
  25.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)  
  26.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)  
  27.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
  28.         at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)  
  29.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
  30.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
  31.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
  32.         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)  
  33.         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)  
  34.         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)  
  35.         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)  
  36.         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)  
  37.         at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)  
  38.         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  
  39.         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)  
  40.         at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)  
  41.         at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)  
  42.         at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)  
  43.         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
  44.         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  

最后,果断弃百度google了一下,终于找到了产生java.lang.ClassNotFoundException的原因:The basic explanation is that com.sun.image.codec.jpeg.JPEGCodec and related libraries have long been marked as depreciated, and you should use the ImageIO library instead.  参考地址:http://www.derrickwilliams.com/

 

原来,在jdk最新的版本中,这jpeg包下的类已经不推荐,所以,果断用ImageIO替换这两个类

 

[java] view plain copy
 
  1. FileOutputStream out = new FileOutputStream(targetFile);  
  2. ImageIO.write(mBufferedImage, type, out);  

修改完成,部署,终于OK啦!

以上是关于java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec的主要内容,如果未能解决你的问题,请参考以下文章