首页 > 代码库 > java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec
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 FileOutputStream(targetFile);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(mBufferedImage);
本来在本地Eclipse测试的好好的,但是部署到Linux服务器的时候就出错了
[java] view plain copy
- java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec
- at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
- at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
- at com.vxpai.wechat.util.ImgUtil.imageScale(ImgUtil.java:72)
- at com.vxpai.wechat.webcontroller.UploadController.ss(UploadController.java:68)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:606)
- at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
- at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
- at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
- at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
- at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
- at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
- at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
- at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
- at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
- at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
- at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
- at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
- at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
- at java.lang.Thread.run(Thread.java:745)
刚开始的时候以为是没有引入rt.jar,将其用maven(我的项目是基于maven建立的)引入,部署上去之后居然还是报错:
[java] view plain copy
- java.lang.UnsatisfiedLinkError: no jpeg in java.library.path
- at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
- at java.lang.Runtime.loadLibrary0(Runtime.java:849)
- at java.lang.System.loadLibrary(System.java:1088)
- at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:67)
- at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:47)
- at java.security.AccessController.doPrivileged(Native Method)
- at sun.awt.image.codec.JPEGImageEncoderImpl.<clinit>(JPEGImageEncoderImpl.java:73)
- at com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(JPEGCodec.java:68)
- at com.vxpai.wechat.util.ImgUtil.imageScale(ImgUtil.java:72)
- at com.vxpai.wechat.webcontroller.UploadController.ss(UploadController.java:68)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:606)
- at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
- at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
- at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
- at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
- at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
- at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
- at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
- at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
- at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
- at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
- at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
- at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
- at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
- 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
- FileOutputStream out = new FileOutputStream(targetFile);
- ImageIO.write(mBufferedImage, type, out);
修改完成,部署,终于OK啦!
java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。