Caused by: java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11.XToolkit 异常解决

linux 下图片上传时报如下异常:

Caused by: java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11.XToolkit
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at java.awt.Toolkit$2.run(Toolkit.java:869)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:861)
    at java.awt.Image.getScaledInstance(Image.java:177)
    at com.higreen.modules.app.utils.ImageUtils.scale(ImageUtils.java:51)
    at com.higreen.modules.app.web.UploadPicController.uploadPic(UploadPicController.java:64)
    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.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    ... 41 more
 

解决方案:

找到tomcat下的bin/catalina.sh中JAVA_OPTS,在后面添加  -Djava.awt.headless=true即可。

你可能感兴趣的:(java)