Java / SpringBoot / Processing - How to launch a PApplet within a Spring Boot application?

Hi,

I’m building a web application in Java, with Spring Boot, including Processing (4) as a library. I’m using Intellij IDEA. I have posted this question on StackOverflow as well: Click

I’m stuck with the integration of launching a processing sketch whilst running my Spring Boot application. What I want to achieve is a sketch is triggered when a certain event happens on the client side - my Spring Boot app is already running when this event occurs.
For running my application I run the SpringBootApplication. This works fine.

package io.eho.foo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FooApplication {

	public static void main(String[] args)
	{
		SpringApplication.run(FooApplication.class, args);
	}
}

I do manage to run a Processing sketch on its own (located in the same project), by triggering its own main method.

package io.eho.foo.play.sketchtest;

import processing.core.PApplet;
// this works fine ONLY IF running PlaySketch.main() manually from within this class.
public class PlaySketch extends PApplet {

    @Override
    public void setup() {
        background(111, 222, 11);
    }

    @Override
    public void settings() {
        size(400, 400);
    }

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

Launching a simple PApplet with green background. Good.

I do NOT succeed launching the PApplet from within the Spring Boot application. I think it is because I do not understand how to launch the PApplet main method from elsewhere in the application. It also confuses me that the PlaySketch.main() is a 2nd main method in my program.

I tried a few things to trigger the PApplet main method from somewhere else in my program:

package io.eho.foo;

import io.eho.foo.play.sketchtest.PlaySketch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FooApplication {

	public static void main(String[] args)
	{
		SpringApplication.run(FooApplication.class, args);
        PlaySketch.main(args);
	}
}

combined with the same code for PlaySketch as above.

I tried to include the PApplet args to launch the sketch in the main method of the Spring Boot application:

package io.eho.foo;

import io.eho.foo.play.sketchtest.PlaySketch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import processing.core.PApplet;

@SpringBootApplication
public class FooApplication {

	public static void main(String[] args)
	{
		SpringApplication.run(FooApplication.class, args);

		String[] processingArgs = {"PlaySketch"};
		PlaySketch playSketch = new PlaySketch();
		PApplet.runSketch(processingArgs, playSketch);
	}
}

Combined with:

  • the same PlaySketch code as above
  • no main for PlaySketch
  • empty main for PlaySketch

And I tried to include a CommandLineRunner (as a Bean) in the SpringBootApplication class and trigger the sketch from there.

package io.eho.foo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import processing.core.PApplet;

@SpringBootApplication
public class FooApplication {

	public static void main(String[] args)
	{
		SpringApplication.run(FooApplication.class, args);
	}

	@Bean
	CommandLineRunner commandLineRunner() {
		return args -> {
			PApplet.main("PlaySketch");
		};
	}
}

Interesting thing is, all these trials produce the same message (or error, it is not clear to me if it is an error or a information):


Cannot run sketch without a display. Read this for possible solutions:
Running without a Display · processing/processing Wiki · GitHub

The referred page explains how to run a sketch without a display, but that is not what I wanted / intended.

I realize I do not fully understand what I’m doing here with these 2 main methods, but so far my trials and searching around for answers didn’t result in a solution… Anyone has an idea what I am doing wrong?

This is my first question here - I hope it is clear, but if not, let me know what i can clarify. I also hope I’m just missing something pretty straightforward :slight_smile:

Thanks,
Erik