Search code examples
linuxopengllwjglnvidia

Invalid X server connection specified


I run IDEA in Ubuntu: sudo ./idea.sh
I wrote an application using the library LWJGL. And when I run the project, with the discovery of an error window:

Exception in thread "main" java.lang.IllegalStateException: Invalid X server connection specified.
at org.lwjgl.system.linux.opengl.LinuxGLContext.createFromCurrent(LinuxGLContext.java:68)
at org.lwjgl.system.linux.opengl.LinuxGLContext.createFromCurrent(LinuxGLContext.java:63)
at org.lwjgl.opengl.GLContext.createFromCurrent(GLContext.java:41)
at core.Core.createWindow(Core.java:160)
at core.Core.main(Core.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Core.java, Creatind windows:

if (glfwInit() != GL_TRUE) System.out.println("#0: Failed to create window");
    window = glfwCreateWindow(Settings.getWidth(), Settings.getHeight(), "Look", GL_NONE, GL_NONE);
    if (window == GL_NONE) System.out.println("#1: Failed to create window");
    glfwMakeContextCurrent(window);
    glfwShowWindow(window);

    ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window,
            (GLFWvidmode.width(vidmode) - Settings.getWidth()) / 2,
            (GLFWvidmode.height(vidmode) - Settings.getHeight()) / 2);
    GLContext.createFromCurrent(); // String №160
    glfwSwapInterval(1);

Solution

  • First things first: Why are you trying to run a graphical application with superuser privileges? This is in general a a bad idea.


    X11 implements various authentication mechanisms to prevent users which are not part of the login session to access the X server. This is implemented not though Unix file permissions, but through protocol authentication. So even if you're root, you can not talk to the X server, without jumping through a few hoops. In particular every client needs access to the Xauthority cookies. Those are stored by default in ${HOME}/.Xauthority but can be defined to be fetched from a different location using the XAUTHORITYenvironment variable.

    So to pass that through sudo you'd set XAUTHORITY to point to your login session user's ~/.Xauthority and then call sudo with environment preserving option.

    export XAUTHORITY="${HOME}/.Xauthority"
    sudo -E ./idea.sh
    

    Or you could set the XAUTHORITY environment after sudo. Another, much more blunt options is to simply punch a hole into authentication that allows all clients running locally to access the X server. However this is a bad idea, because then every user on your system could run a keylogger or otherwise mess with your session (for example preventing the screen locker to work). You can punch that hole with xhost +si:localuser but you really should not do it.