我有一个使用java.awt.swing
代码的java项目,但是当程序到达使用代码的函数时,它会出现以下错误:
X11 connection rejected because of wrong authentication
在catalina.out
logfile
SEVERE: Servlet.service() for servlet [program$Servlet] in context with path [/picture] threw exception [com.vaadin.server.ServiceException: java.lang.InternalError: Can't connect to X11 window server using 'localhost:11.0' as the value of the DISPLAY variable.] with root cause
java.lang.InternalError: Can't connect to X11 window server using 'localhost:11.0' as the value of the DISPLAY variable.
根据this site上的论坛帖子,如果您想使用任何AWT或Swing组件,则需要以无头模式运行JVM。我通过执行this site中的以下代码尝试了这一点:
CATALINA_OPTS="-server -Djava.awt.headless=true"
但是,问题仍然存在且X11Forwarding
已启用
我是否想在tomcat启动时想要使用java.awt.headless这个错误的方向,还是真的要走的路?操作系统是红帽企业Linux服务器版本6.7
为了完整性:使用swing的代码,我想在图像上绘制String username的内容
public File generatePhoto(String username) throws NamingException {
BufferedImage noImg = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = noImg.createGraphics();
Font font = new Font("Arial", Font.PLAIN, 48);
g2d.setFont(font);
g2d.setPaint(Color.red);
FontMetrics fm = g2d.getFontMetrics();
g2d.dispose();
noImg = new BufferedImage(180, 180, BufferedImage.TYPE_INT_ARGB);
g2d = noImg.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(Color.WHITE);
g2d.drawString(username, 60, 100);
g2d.dispose();
try {
ImageIO.write(noImg, "jpg", new File(tmp_dir + "/noimg" + username + ".jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
File outputfilenoImg = new File(tmp_dir + "/noimg" + username + ".jpg");
System.out.println("location of no image: " + outputfilenoImg.toString()) ;
return outputfilenoImg ;
}
我尝试过的其他解决方案
static { /* works fine! ! */
System.setProperty("java.awt.headless", "true");
System.out.println("Headless?" + java.awt.GraphicsEnvironment.isHeadless());
/* ---> prints true */
}
打印错误
protected void init(VaadinRequest request)
{
System.setProperty("java.awt.headless", "true");
System.out.println("Headless?" + java.awt.GraphicsEnvironment.isHeadless());
...
}
打印错误
答案 0 :(得分:3)
经过一番搜索,我找到了解决方案:
vi ../bin/setenv.sh
使用..作为tomcat的位置并添加以下代码
JAVA_OPTS="$JAVA_OPTS -Dopenam.agents.bootstrap.dir=/oam/tomcat-xtrapps/j2ee_agents/tomcat_v6_agent/Agent_002/config"
export JAVA_OPTS="$JAVA_OPTS\
-server\
-Djava.awt.headless=true\
第一行已经存在,其余部分由我添加