I'm trying to write a program for my Lego Mindstorm NXT, the program is simple, drawing some lines and saving the image in a .png file. So, I thought I'd use BufferedImage and use getGraphics to draw on it, the thing is; if I write the code:

import java.awt.image.BufferedImage;

import lejos.nxt.Button;

public class Main {

    public static void main(String[] args) {
        BufferedImage bimg = new BufferedImage(300,300, BufferedImage.TYPE_3BYTE_BGR);


When I run the program, it freezes for a while in "Linking..." and gives me the following:

Java Heap Space

So the question is this: How can I use BufferedImage with Lejos without having this problem? And if there's another method of drawing lines and saving the image on a file, it would be very helpful.

EDIT: The Java Heap Space is caused by the line

BufferedImage bimg = new BufferedImage(300,300, BufferedImage.TYPE_3BYTE_BGR);

I've also tried using ImageIO:

import javax.microedition.lcdui.Image;
Image img = Image.createImage(300, 300);
ImageIO.write((RenderedImage) img, "png", monFichier);

It gives me the same thing. I don't know but maybe this isn't the right way to create an image with Lejos. What do you think?

  • You shouldn't cross-post the same question here and on stackoverflow, it's spammy. Mar 14, 2016 at 12:54
  • Any solution is going to be specific to LeJOS, so it probably belongs here.
    – user3971
    Mar 15, 2016 at 16:13
  • Where are you getting this error? Edit your question to show the exact steps to reproduce.
    – user3971
    Mar 15, 2016 at 16:19
  • When I launch it, I gives me the following: Initializing LeJOS JDK at /home/***LeJosPath*** Linking ... Thank the Java Heap Space comes up. I've edited the question.
    – DaPoox
    Mar 15, 2016 at 17:47
  • It's been awhile since I looked at LeJOS. Is this error coming from the brick? The max heap is set to the size of the ram on the brick, I recall, and is not easily configurable.
    – user3971
    Mar 16, 2016 at 0:01

1 Answer 1


I'm a little puzzled by how you are getting these results, but I am assuming you are getting an error when you run the equivalent of nxjc and nxjlink on a supported platform. i.e., you are probably invoking nxj -r ...

Furthermore, I am assuming you are basing these commands on a recent Java compiler from Oracle; version 7 or 8.

First of all, it is my understanding that java.awt.image.* is not even present. The example showed here should not even compile because the only AWT classes we have access to are java.awt.Point and java.awt.Rectangle.

Furthermore, only some of the javax.microedition.lcdui API is implemented, so you should check the documentation to see if Image is supported in the manner you want. So, for example, ImageIO is not present.

So, the example programs you show here are not going to even compile (never mind link) with the as-shipped latest version of leJOS NXJ.

At any rate, the size of the heap for leJOS is basically all of the RAM on the brick, but it is shared with the stack and static RW memory. But I'm not convinced the message you are seeing is related to the leJOS VM runtime heap, but rather the heap of the thing that is trying to create the classes and link them into the "TinyVM" before sending them to the brick.

We don't know because you have not described exactly how you are getting this message, with what command(s), with the full output of the exception(s)!

So, if you want to solve this, we need:

  • The platform you are running leJOS on.
  • The output of %LEJOS_NXT_JAVA_HOME%\bin\javac -version (assuming Windows; you know what to do for OS X or Linux)
  • For that matter, what is %LEJOS_NXT_JAVA_HOME% or ${LEJOS_NXT_JAVA_HOME} set to?
  • The output from the command you are running. Try capturing what comes out of nxj -v -r Main
  • The version of leJOS NXJ you are running

Place this in the body of your question, please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.