Results 1 to 14 of 14
  1. #1
    tejz123 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default getScreenSize() method giving java.awt.HeadlessException

    Hi,

    I am getting HeadlessException for getScreenSize() method.Please find the below sample code for more details.

    Dimension dim = null;
    try{
    dim = Toolkit.getDefaultToolkit().getScreenSize();
    }catch(Exception e){
    e.printStackTrace();
    }

    Can you please help me on this issue ?|

  2. #2
    tejz123 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    Hi ,

    The getScreenSize() method is not working in Java 1.6.But it is working in 1.4.
    Recently we have upgraded the java to 1.6.

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    I ran your code in the Netbeans IDE 7.1.1 using JDK 1.6 and I didn't get an exception. Did you try to run it in a simple program?

    Here's the code
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication30;
    
    import java.awt.Dimension;
    import java.awt.Toolkit;
    
    public class JavaApplication30
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
    
            Dimension dim = null;
            try
            {
                dim = Toolkit.getDefaultToolkit().getScreenSize();
            } catch (Exception e)
            {
            }
            // TODO code application logic here
        }
    }

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    Also, my OS is windows 7.

  5. #5
    tejz123 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    yes.I have ran the code in IRAD 7.5 usind JDK 1.6.But i am getting same exception in my local and in the production environment.

    Server : WAS 7

  6. #6
    tejz123 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    Error :


    java.awt.HeadlessException
    at sun.awt.HeadlessToolkit.getScreenSize(HeadlessTool kit.java:273)
    at com.ibm._jsp._Translation._jspService(_Translation .java:158)
    at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJsp Base.java:99)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:831)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.ser vice(ServletWrapper.java:1657)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.han dleRequest(ServletWrapper.java:939)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.han dleRequest(ServletWrapper.java:502)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl .handleRequest(ServletWrapperImpl.java:179)
    at com.ibm.wsspi.webcontainer.servlet.GenericServletW rapper.handleRequest(GenericServletWrapper.java:12 1)
    at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensio nServletWrapper.handleRequest(AbstractJSPExtension ServletWrapper.java:259)
    at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensio nProcessor.handleRequest(AbstractJSPExtensionProce ssor.java:353)
    at com.ibm.ws.webcontainer.webapp.WebAppRequestDispat cher.forward(WebAppRequestDispatcher.java:353)
    at com.dcx.cinonconnect.cocapplication.FrontControlle r.dispatch(FrontController.java:256)

  7. #7
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    The headless exception is described here: HeadlessException (Java 2 Platform SE v1.4.2).

    Does your situation fit the definition?:
    "Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse. "

  8. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException


  9. #9
    tejz123 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    I am getting isHeadless() as true.

    System.out.println(java.awt.GraphicsEnvironment.is Headless());

    Output : true.

  10. #10
    tejz123 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    Thanks shall for the quick reply.

    Is there any other way we can get the screen height and width in java?

    Because i need height and width of screen details for the below code.


    Dimension dim = null;
    try{
    System.out.println(java.awt.GraphicsEnvironment.is Headless());
    dim = Toolkit.getDefaultToolkit().getScreenSize();
    }catch(Exception e){
    e.printStackTrace();
    }
    int screenHeight = dim.height;
    int screenWidth = dim.width;

    int hg=0;
    String w1="";
    String w2="";
    String w3="";
    String w4="";


    if(screenHeight == 1024 && screenWidth == 1280)
    {
    hg = 470;

    w1="13";
    w2="37";
    w3="10";
    w4 ="34";


    }else{
    if(screenHeight == 768 && screenWidth == 1024)
    {
    hg= 370;
    w1="13";
    w2="47";
    w3="10";
    w4 ="22";

    }
    }

  11. #11
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException


  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    Quote Originally Posted by shall View Post
    The headless exception is described here: HeadlessException (Java 2 Platform SE v1.4.2).
    Please don't post links to old obsoleted Java API.

    You could bookmark these:
    Java Platform SE 7
    Java Platform SE 6

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    vinodh_avala is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Re: getScreenSize() method giving java.awt.HeadlessException

    I have encountered the java.awt.headlessexception when the code I developed moved from Windows environment to AIX environment.

    The code snippet is Dimension.getDefaultToolKit().getScreenSize(). Tried a lot in forums. 2 viable options are using System.setProperty() and setting java.awt.headless=true on the console. If you use the former, have to be set even before class loading happens (I felt complicated). The latter is not recommended in enterprise applications (as the web admin has to set it every time the JVM is restarted).

    So, I opted for Javascript (screen.width and screen.height) parameters and added them to the request string to access in my servlet. Finally worked!!

Similar Threads

  1. Java code giving error
    By alfarida in forum New To Java
    Replies: 3
    Last Post: 11-10-2010, 08:11 PM
  2. Replies: 1
    Last Post: 07-23-2010, 03:42 PM
  3. Replies: 5
    Last Post: 01-17-2010, 12:14 AM
  4. Giving Inputs to Java Applet
    By rasikh in forum Java Applets
    Replies: 2
    Last Post: 10-05-2009, 04:08 PM
  5. java.awt.HeadlessException Applet not displayed
    By sunjavaboy in forum Advanced Java
    Replies: 3
    Last Post: 01-15-2009, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •