Cannot find a class or type named "Xyz"

edited November 2014 in Common Questions

Cannot find a class or type named "Xyz"

So, you try to run some sketches coming with Processing, or found on Internet (perhaps the Processing forum or this wiki) or some book, or even one of your old sketches, and you get a puzzling error:

Cannot find a class or type named "SomeType"

where SomeType is the name of a class, like NumberFormat, KeyEvent, or Robot, for example.

What happened?

Before the v.2.0, Processing used to import by default lot of Java classes. It was as if you had at the start of the sketch the following lines:

import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;

It allows to use some advanced features of Java in Processing without special ceremony...

Since Processing 2.0b7, these default imports are gone! This is to force users to make explicit declarations. So if you see an import statement at the start of a sketch, you know it won't work in '''Processing.js''', for example. It is a bold move to improve cross-platform support, that confused lot of people...

How to fix it?

Simple, brutal way: copy & paste the code above at the start of the sketch.

Inconvenience: it adds "noise" to your code, by having lot of lines unrelated to your sketch.

A smarter way is to look up for the reported class in the official Java documentation. Then you can just import it, with its package, as shown above. You might need to repeat the process for each reported class...

Sign In or Register to comment.