Noosrep
Noosrep

Reputation: 416

X11 connection rejected because of wrong authentication tomcat

I have a java project which uses java.awt.swing code but when the program gets to the function which uses the code, it gives following error:

X11 connection rejected because of wrong authentication

with following output in 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.

According to a forum post on this site you will need to run the JVM in headless mode if you want to use any of the AWT or Swing components. I tried this by executing following code from this site:

CATALINA_OPTS="-server -Djava.awt.headless=true"

However, the problem still occurs and X11Forwarding is enabled

Am I searching in the wrong direction by wanting to eneabled java.awt.headless on tomcat startup or is that really the way to go? OS is Red Hat Enterprise Linux Server release 6.7

For completeness: the code which uses swing where I want to draw the content of String username on an image

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 ;

}

Extra solutions I have tried

static { /* works fine! ! */
      System.setProperty("java.awt.headless", "true");
      System.out.println("Headless?" + java.awt.GraphicsEnvironment.isHeadless());
      /* ---> prints true */
    }

Prints false

        protected void init(VaadinRequest request) 
        {
            System.setProperty("java.awt.headless", "true");
            System.out.println("Headless?" + java.awt.GraphicsEnvironment.isHeadless());
...
}

Prints false

Upvotes: 3

Views: 1033

Answers (1)

Noosrep
Noosrep

Reputation: 416

After some more searching I found the solution:

vi ../bin/setenv.sh

with .. being the location of your tomcat and added following code

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\

The first line was already there, the rest was added by me

Upvotes: 3

Related Questions