private static void setupVirtualServerAndToolkit() {
        final int MAX = 30;
        int i;
        File target;
        final String DISPLAY;
        final Runtime runtime;

        try {
            /*
             * This seems quite the kludge, but apparently this is the
             * algorithm used by X itself to find an available display number.
             * It's also used by Gentoo's virtualx eclass.
             */

            for (i = 0; i < MAX; i++) {
                target = new File("/tmp/.X" + i + "-lock");
                if (!(target.exists())) {
                    break;
                }
            }
            if (i == MAX) {
                fail("\n" + "Can't find an available X server display number to use");
            }
            DISPLAY = ":" + i;

            /*
             * Xvfb arguments:
             * 
             * -ac disable access control (necessary so that other program can
             * draw there)
             * 
             * -wr white background
             * 
             * -fp built-ins workaround "fixed" font not being present.
             * 
             * Also, don't try to force Xvfb to 32 bits per pixed in -screen;
             * for some reason this makes it unable to start.
             */

            runtime = Runtime.getRuntime();

            virtual = runtime.exec("/usr/bin/Xvfb " + DISPLAY
                    + " -ac -dpi 96 -screen 0 800x600x24 -wr -fp built-ins");

            sleep(100);

            checkVirtualServerRunning();

            /*
             * Attempt to terminate the virtual X server when the tests are
             * complete. This is far from bullet proof. It would be better if
             * we knew when all the tests were done running and called
             * destroy() then.
             */

            runtime.addShutdownHook(new Thread() {
                public void run() {
                    if (virtual == null) {
                        return;
                    }
                    try {
                        virtual.destroy();
                        virtual.waitFor();
                    } catch (InterruptedException e) {
                        // already exiting
                    }
                }
            });

            /*
             * Finally, initialize GTK. We close stderr to prevent noise from
             * Xlib (as used by GTK) about "XRANR not being available". This
             * of course means we're missing anything else to stderr. That
             * seems like a bad idea, but what else can we do?
             */

            System.err.close();

            Gtk.init(new String[] {
                "--display=" + DISPLAY
            });

            initialized = true;
        } catch (IOException ioe) {
            fail("Unexpected I/O problem: " + ioe.getMessage());
        } catch (InterruptedException ie) {
            fail("How did this Thread get interrupted?");
        }

    }