Happy Coding

Processing in Java

At this point you’ve probably used Processing through the Processing editor, which allows you to write Processing code and automatically run it. You’ve also learned how to use Java libraries, which allow you to use classes and functions written by other people in your Java code. This tutorial combines those ideas and shows you how to use Processing as a Java library.


This is a companion discussion topic for the original entry at https://happycoding.io/tutorials/java/processing-in-java

Hi I followed your instructions from this link:

I get the following error when running this code below in Eclipse. Please advice. Thank you.

Error: Could not find or load main class MySketch
Caused by: java.lang.NoClassDefFoundError: processing/core/PApplet

import processing.core.PApplet;
public class MySketch extends PApplet {

public static void main(String[] args){
	String[] processingArgs = {"MySketch"};
	MySketch mySketch = new MySketch();
	PApplet.runSketch(processingArgs, mySketch);
}

}

Sorry, what error are you getting?

Error: Could not find or load main class MySketch
Caused by: java.lang.NoClassDefFoundError: processing/core/PApplet

That sounds like the Processing library isn’t on your classpath. How are you setting that up?

i am also get this error how can i solve this problem

How are you setting up your classpath?

yes


@abmushfiq it looks like your error is different from the error that @designer01 is getting.

Your error looks like it’s telling you that it can’t find your class. Can you try creating a more basic project that just prints hello world? I’d try to get that working before you try adding Processing to it.

even not comming processing window its basic i dont know whats the error

now its working thanks for your quick reply

Awesome! What was the problem and how did you end up fixing it?

Not sure if the core folder is in the right location?

I’m not super familiar with modules in Java, but naively I would expect core.jar to be on your classpath, not your modulepath?

Not sure what the problem is in Eclipse. I did the same process in IntelliJ and it works fine.

When you have multiple tabs (classes) in Processing what is the proper way to set them in Eclipse?IntelliJ? See image below? Thank you.

Behind the scenes, Processing would convert sketch_22017a1 into a Java class, and Bubble into an inner class. I believe if you export your sketch as an application you can see the generated Java code, but I believe it’ll look something like this:

public class sketch_22017a1 {

  Bubble b;

  void setup() {
    size(640, 360);
    b = new Bubble();
  }

  class Bubble {
    // code here
  }
}

This is why other tabs can access variables like width and height, and functions like ellipse() - because the compiled code is an inner class of the main sketch class.

Now, whether you set your code up that way when you’re using Processing as a Java library is up to you. Generally I would say it’s probably a better idea to have each tab be its own standalone class, and then you’d pass around references to your sketch to have access to variables like width and height, and functions like ellipse().

If you want to create a top-level class in the Processing editor, add .java to your tab name - in your case that would be Bubble.java.