Java Processing

Hello,
after building a huge project in processing the last years I am looking for a way to store the pure algorithmic code parts out in Java files. Then I want to integrate these files as ressources in Processing. I there another way to do so than writing a library? I research for a while but could not figure it out.

As simple example I want to use the following method “sumUp” from a Java class (written in a Java IDE) in Processing to sum 2 ints.

public class sum {
    public static void main(String[] args) {       
    }
    public int sumUp(int a, int b) {
        int i = a + b;
        return i;
    }
}

Could somebody give an example for achieving this or similar with example code for the interaction between Processing and Java IDE (in my case IntelliJ)?

1 Like

I’ve got these 2 “.java” files which are usable from “.pde” files: :coffee:


1 Like

Not sure of how do you want it to work in your case, or if @GoToLoop already answered what you needed to know.

You could compile your .java files from Idea, exporting a jar file that you can use from the Processing IDE.

It can also be done like this.

  1. I write two sample java files:
// mylib/src/Bar.java
package com.hamoid;
public class Bar {
  public static int sumUp(int a, int b) {
    int i = a + b + 100;
    return i;
  }
}

// mylib/src/Sum.java
package com.hamoid;
public class Sum {
  public static int sumUp(int a, int b) {
    int i = a + b;
    return i;
  }
}
  1. In the terminal, go into that folder cd mylib

  2. Compile the java files into class files: javac -bootclasspath /usr/lib/jvm/default/jre/lib/rt.jar -source 1.6 -target 1.6 -d . -classpath /usr/share/processing/core/library/core.jar src/*.java
    There are two paths to figure out there: one is the path to rt.jar which varies depending on your OS. The other one you may need is the path to Processing’s core.jar but that’s only if your java files make use of PApplet or other Processing classes. If not you can remove -classpath....core.jar.

  3. Pack the the class files into a jar file: jar -cf all.jar com/
    I used com/ because that’s the folder that was created in step 3, since my two java files are inside package com.hamoid.

  4. Then I create a Processing sketch with these files:

    Test.pde
    code/all.jar

The content of Test.pde is this:

import com.hamoid.*;

println(Sum.sumUp(2, 5));
println(Bar.sumUp(2, 5));
  1. Remember to put all.jar inside the code/ folder in the sketch.

If I run it it prints this:

7
107

I hope this helps you a bit in your quest :slight_smile:

1 Like

Indeed my 2 “.java” files don’t depend on package processing.core.

Here’s 1 that does if it’s compiled outside Processing’s IDE (PDE):

Maybe JogAmp and JavaFX libs are also required as classpath besides Processing’s “core.jar”:


thanks hamoid, trying to follow your suggestions I get the error message “The import com.hamoid cannot be resolved”. As I have not worked much with Java IDE´s it seems that I do not generate a right jar file. Next I will try to generate it only with the shell and without IntelliJ.

ok now I solved the issue by creating a run configuration described here: https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html

Thanks Hamoid and (since years:-) GoToLoop!

It would be awesome if you can share step by step instructions for others to follow :slight_smile:

1 Like

Step by step instructions

(tested with Windows 10 x64, Processing 3.5.4, IntelliJ 2019.3.1, Java Version jdk1.8.0_241)

The following steps are derived from https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html

  1. Install the jdk. Be sure to install a jdk version which is supported by the Processing version you use e.g. p3.5.4 does not support Java9

  2. Create a new Java project

  • Launch IntelliJ IDEA.
  • If the Welcome screen opens, click Create New Project. Otherwise, from the main menu, select File | New | Project.
  • In the New Project wizard, select Java from the list on the left.
  • From the Project SDK list, select the JDK that you want to use in your project. If the list is empty, click New and specify the path to the Java home directory. Be aware that Processing 3 does not support Java 9 so be sure to choose here a supported jdk version in my case this was jdk1.8.0_241
  • We’re not going to use any additional libraries or frameworks for this tutorial, so click Next.
  • Don’t create a project from the template. In this tutorial, we’re going to do everything from scratch, so click Next.
  • Name the project, for example: Sum
  • If necessary, change the default project location and click Finish.
  1. Create a package and a class
  • Packages are used for grouping together classes that belong to the same category or provide similar functionality, for structuring and organizing large applications with hundreds of classes.
  • In the Project tool window, select the src folder, press Alt+Insert, and select Java Class.
  • In the Name field, type com.hamoid.sum.Sum and click OK.
  • IntelliJ IDEA creates the com.hamoid.sum package and the Sum class.
  • see also naming conventions: https://www.oracle.com/technetwork/java/codeconventions-135099.html
  • Together with the file, IntelliJ IDEA has automatically generated some contents for your class. In this case, the IDE has inserted the package statement and the class declaration.
  • This is done by means of file templates. Depending on the type of the file that you create, the IDE inserts initial code and formatting that is expected to be in all files of that type. For more information on how to use and configure templates, refer to File and code templates
  1. Write the code

    public static int sumUp(int a, int b) {
      int i = a + b;
      return i;
    }
    

    And in my experience to generate the package later it turned out to be necessary to add a main method (even if empty)

    public static void main(String[]args){
    }
    

    So the whole class Sum looks like this:

    package com.hamoid.sum_JDK8;
    
    public class Sum {
      public static void main(String[]args){
      }
     public static int sumUp(int a, int b) {
         int i = a + b;
         return i;
      }
    }
    
    
  2. Build and run the application

  • Click the green Run button in the gutter and select Run ‘Sum.main()’ in the popup. The IDE starts compiling your code.
  • When the compilation is complete, the Run tool window opens at the bottom of the screen.
  • The first line shows the command that IntelliJ IDEA used to run the compiled class. The second line shows the program output which is in this case empty because the class method sumUp is intended to be used from Processing only. And the last line shows the exit code 0, which indicates that it exited successfully.
  • When you click Run, IntelliJ IDEA creates a special run configuration that performs a series of actions. First, it builds your application. On this stage, javac compiles your source code into JVM bytecode.
  • Automatically created run configurations are temporary, but you can modify and save them.
  • Once javac finishes compilation, it places the compiled bytecode to the out directory, which is highlighted with yellow in the Project tool window.
  • After that, the JVM runs the bytecode.
  1. Package the application in a JAR
  • When the code is ready, you package your application in a Java archive (JAR). A built Java archive is called an artifact.

  • Create an artifact configuration for the JAR

  • From the main menu, select File | Project Structure Ctrl+Shift+Alt+S and click Artifacts.

  • Click the Add button, point to JAR and select From modules with dependencies (after my trials this will not function without the empty main method). To the right of the Main Class field, click the Browse button and select Sum (com.hamoid.sum) in the dialog that opens.

  • IntelliJ IDEA creates the artifact configuration and shows its settings in the right-hand part of the Project Structure dialog.

  • Apply the changes and close the dialog.

  • Build the JAR artifact

  • From the main menu, select Build | Build Artifacts.

  • Point to Sum:jar and select Build.

  • If you now look at the out/artifacts folder, you’ll find your JAR there.

  1. Create a run configuration for the packaged application
  • To run a Java application packaged in a JAR, IntelliJ IDEA allows you to create a dedicated run configuration.
  • Press Ctrl+Shift+A, find and run the Edit Configurations action.
  • In the Run/Debug Configurations dialog, click the Add button and select JAR Application.
  • Name the new configuration: SumJar.
  • In the Path to JAR field, click the Browse button and specify the path to the JAR file on your computer.
  • Under Before launch, click the Add button, select Build Artifacts | Sum:jar in the dialog that opens.
  • Doing this means that the Sum.jar is built automatically every time you execute this run configuration.
  • Run configurations allow you to define how you want to run your application, with which arguments and options. You can have multiple run configurations for the same application, each with its own settings.
  • Execute the run configuration
  • On the toolbar, select the SumJar configuration and click the Run button to the right of the run configuration selector. Alternatively, press Shift+F10 if you prefer shortcuts. As before, the Run tool window opens and shows you the application output (“Process finished with exit code 0”).

The Sum jar file must be in a folder inside the processing sketch folder name it e.g. code

And then in Processing access the jar files like this:

import com.hamoid.sum.*;
println(Sum.sumUp(2, 5));

Executing this Processing sketch the result 7 is shown in the console.

3 Likes