LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java Graphics[2D] requires an X server? (https://www.linuxquestions.org/questions/programming-9/java-graphics%5B2d%5D-requires-an-x-server-144942/)

Cand1e 02-11-2004 03:20 PM

java Graphics[2D] requires an X server?
 
java Graphics[2D] requires an X server?

Code:

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>(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(Unknown Source)
        at java.awt.image.BufferedImage.createGraphics(Unknown Source)
        at (my code)
        at (my code)
        at (my code)

The line code that throws this exception is either
Code:

Graphics2D g = (Graphics2D)b.getGraphics();
or
Code:

Graphics2D g = b.createGraphics();
I have a linux box on which I would like to generate some images for use on a website, it doesn't have a X server - because a) it is only a P100 with a 1Gb HD, so the space required, and the processing power required for the X server are lacking! and b) the only cables out of the box are power and network... i.e. no moniter/k-b/etc

it is running Suns JVM on a debian base.

how can I resolve this issue - other than killing my machine with an X-server

the true Candle!

jtshaw 02-11-2004 03:23 PM

Well you are going to have to have an xserver to use any of the awt or swing functionality.

Cand1e 02-11-2004 03:26 PM

is there a way round it?

and why does it need an X server? it doesn't make sense, all I want to do is write images to file, not display them or anything!

dave bean 02-11-2004 04:17 PM

did you just install java recently ? i had an almost identical error for non root users when their path wasn't set correctly

Cand1e 02-11-2004 07:20 PM

it works fine on a Windows machine, just not the Linux P100. java is not a recent install, and it is in the path

jtshaw 02-11-2004 07:38 PM

I am not even close to a java expert. In fact, I haven't written a line of Java since I was forced to in a CS class in college back in 2000. But I looked through the awt functions and a lot of them having rendering sub functions and it is possible some initializer or something is trying to interact with X somehow. This wouldn't be a problem in windows because the gui interface is always there to interact with in windows.

Cand1e 02-11-2004 07:46 PM

I can (sort of) understand why Sun would make X system calls for the awt stuff, however I don't really understand why they are trying to use X to draw on a BufferedImage!

Java is a great programmnig language! cross platform, unified structure, OO, simple (all things are relative... prolog...!), it has loads of in-built classes - from Strings to windows.

jtshaw 02-11-2004 08:30 PM

Well, again, I could easily be talking out of my pinky toe or something here.... but is there no way to manipulate an image file with something that isn't a member of the Abstract Windows Toolkit? I mean, if all you want to do is write images to a file it seams kind of silly to have all that class structure for a graphical interface included. There must be some file IO commands outside of the scope of AWT.

Cand1e 02-12-2004 03:04 AM

Quote:

Originally posted by jtshaw There must be some file IO commands outside of the scope of AWT.
Quote:

Originally posted by Cand1e is there a way round it?
Quote:

Originally posted by Cand1e how can I resolve this issue
I've been reading the API :study: and *I* havn't found anything yet, but I'll keep searching :) and this was part of my search, asking here.

there are FILE IO commands out of the scope, i.e. I can read files and write files, I can probably read and write image files, but the problem is image manipulation, getting hold of a Graphics[2D] object so I can call Graphics.draw(Shape s)); before writing the image back to file.

hiteshmaisheri 02-13-2004 01:27 AM

from the knowledge i know about java it is difficult to manipulate files as such....

german 02-15-2004 11:44 AM

You can use XVFB (the X Virtual Framebuffer) to use AWT on headless *NIX boxen... it is installed with X on most distros. This is the code I use to start XVFB on my server for an on-the-fly thumbnail application:

#!/bin/sh

# Shell script to startup the Xvfb server

echo "Starting Xvfb services..."
Xvfb :1 -fp /usr/X11R6/lib/X11/fonts/misc/,/usr/X11R6/lib/X11/fonts/75dpi/ -screen 0 1x1x24 &
echo "Done."

This starts a 1x1 virtual X screen (you can write off the edges of the screen so you might as well take up as little memory as possible imho) at 24 bpp. Then when you start your java application, you have to run it like this:

export DISPLAY=localhost:1.0
java org.bunk.my.Application

and it will run AWT functions in the virtual X server.

HTH

B.

german 02-15-2004 11:58 AM

I just thought, you could also either use JDK1.4 which has headless AWT support built-in (I believe you just pass something like java -Djava.awt.headless=1), or you could try PJA (http://www.eteks.com/pja/en/) which is a third party library for headless AWT operations.

From my experience with headless AWT, my first post is the easiest to implement, and stays true to standard java libraries. I haven't played with the 1.4 headless options.

worldmagic 02-18-2004 07:18 AM

Setting "-Djava.awt.headless=true" will throw the execption "HeadlessException" if you try to create an instance of a awt class.. Im having the same problem right now, how to create offscreen images..


All times are GMT -5. The time now is 11:38 PM.