0

I have java 1.4 application that is running on server. I need to calculate the width of strings before I create pdf files that include them. When running the app locally (on my pc) it is o.k. , but when I run it on a server I get an error :

 Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY
variable.
        at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
        at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:77)
        at java.lang.Class.forName1(Native Method)
        at java.lang.Class.forName(Class.java:142)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:72)
        at java.awt.Font.initializeFont(Font.java:285)
        at java.awt.Font.<init>(Font.java:319)

the code that create the problem calculate the width of a string as follows :

 protected static String mCutStr(String iText , int iWidthPt , int iEstimatedNumOfChars)
  {
      iWidthPt = iWidthPt-4; //remove padding

      AffineTransform affinetransform = new AffineTransform();     
      FontRenderContext frc = new FontRenderContext(affinetransform,true,true);     
      Font font = new Font("Times", Font.PLAIN, 9);
      int textwidth = (int)(font.getStringBounds(iText, frc).getWidth());
    //  System.out.println("iText="+iText+";    textwidth = "+textwidth+"   iWidthPt ="+iWidthPt);
      if(textwidth <= iWidthPt)
          return iText;
      String vTestStr = iText.substring(0 , iEstimatedNumOfChars)+">>";
      textwidth = (int)(font.getStringBounds(vTestStr, frc).getWidth());
      while(textwidth < iWidthPt)
      {
          iEstimatedNumOfChars++;
          vTestStr = iText.substring(0 , iEstimatedNumOfChars)+">>";
          textwidth = (int)(font.getStringBounds(vTestStr, frc).getWidth());
    //    System.out.println("vTestStr="+vTestStr+"    textwidth = "+textwidth);
      }
      vTestStr = iText.substring(0 , iEstimatedNumOfChars-1)+">>";
      return vTestStr;
  }

Is there an other way to calc this without using AWT? (it must support minimum java 5)

Robin Krahl
  • 5,268
  • 19
  • 32
tamih
  • 93
  • 1
  • 15
  • Possible duplicate of [Java Can't connect to X11 window server using 'localhost:10.0' as the value of the DISPLAY variable](http://stackoverflow.com/questions/10165761/java-cant-connect-to-x11-window-server-using-localhost10-0-as-the-value-of-t) – Arnaud Jun 01 '16 at 09:53

1 Answers1

0

You need to run your server in headless mode. Headless mode is useful when actual Window system like X server is not available and you need to perform some AWT related functions.

Java Documentation about Headless mode

If you are using web server like tomcat search how to execute that specific server in headless mode

OR set system environment property java.awt.headless to true

Sangram Jadhav
  • 2,438
  • 16
  • 17