Java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment [Solved]

2018-09-29 17:45:16.905 ERROR [pool-1-thread-1]o.s.scheduling.support.TaskUtils$LoggingErrorHandler.handleError:95 -Unexpected error occurred in scheduled task.
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1181)
	at com.gfs.mdm.utils.inv.WaterMarkUtils.addWaterMark(WaterMarkUtils.java:125)
	at com.gfs.mdm.service.inv.ScheduleService.addMark(ScheduleService.java:282)
	at com.gfs.mdm.service.inv.ScheduleService.requestAIByTask(ScheduleService.java:113)
	at com.gfs.mdm.service.inv.ScheduleService$$FastClassBySpringCGLIB$$a1dceaa9.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
	at com.gfs.mdm.service.inv.ScheduleService$$EnhancerBySpringCGLIB$$f063fe08.requestAIByTask(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

Reason:

For a java server, it is often necessary to deal with some graphic elements, such as the creation of maps or graphs and charts. These APIs basically always need to run an X-server in order to use AWT (Abstract window toolkit)

Solution:

The first one is the first one

Modify ${Tomcat_ Home}/bin/catalina.sh or ${Tomcat_ Home}/bin/catalina.bat file:

In all codes similar to the following:
“$_ RUNJAVA” $JAVA_ OPTS $CATALINA_ OPTS \
-Djava.endorsed.dirs=”$JAVA_ ENDORSED_ DIRS” -classpath “$CLASSPATH” \
-Djava.security.manager \
-Djava.security.policy==”$CATALINA_ BASE”/conf/catalina.policy \
-Dcatalina.base=”$CATALINA_ BASE” \
-Dcatalina.home=”$CATALINA_ HOME” \
-Djava.io.tmpdir=”$CATALINA_ TMPDIR” \

Add a sentence at the end:
– DJava. AWT. Headless = true\

The revised content is as follows:
exec “$_ RUNJAVA” $JAVA_ OPTS $CATALINA_ OPTS \
-Djava.endorsed.dirs=”$JAVA_ ENDORSED_ DIRS” -classpath “$CLASSPATH” \
-Dcatalina.base=”$CATALINA_ BASE” \
-Dcatalina.home=”$CATALINA_ HOME” \
-Djava.io.tmpdir=”$CATALINA_ TMPDIR” \
-Djava.awt.headless=true \

The second method: the first method seems to have no response sometimes, because it is Baidu, so they all say that the first method is OK, but I started it several times before I succeeded, bad comments! The following is my own research according to the above arrangement. I directly add the following code when starting the container:

System.setProperty("java.awt.headless","true");

I use the spring container, so I add it to the applicationlistener & lt; ContextRefreshedEvent> In the container startup item, servletcontextlistener can be used for servlet

Similar Posts: