# Java Processing

**URL:** <https://discourse.processing.org/t/java-processing/17141>\
**Category:** Development\
**Created:** [January 17, 2020, 4:09pm UTC](https://discourse.processing.org/t/java-processing/17141 "2020-01-17T16:09:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerome](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@gerome](https://discourse.processing.org/u/gerome)\
**Post date:** [January 17, 2020, 4:09pm UTC](https://discourse.processing.org/t/java-processing/17141/1 "2020-01-17T16:09:12Z")

</div>

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.

```auto
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)?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 17, 2020, 6:44pm UTC](https://discourse.processing.org/t/java-processing/17141/2 "2020-01-17T18:44:34Z")

</div>

> [@gerome](#):
>
> Could somebody give an example for achieving this or similar with example code for the intercation between Processing and Java IDE (in my case IntelliJ)?

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

> <https://gist.github.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21>

> <https://gist.github.com/GoToLoop/bba0c288aaeeb5ef9bb1>

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [January 17, 2020, 7:25pm UTC](https://discourse.processing.org/t/java-processing/17141/3 "2020-01-17T19:25:42Z")

</div>

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:

```auto
// 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:

The content of Test.pde is this:

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 17, 2020, 7:51pm UTC](https://discourse.processing.org/t/java-processing/17141/4 "2020-01-17T19:51:21Z")

</div>

> [@hamoid](#):
>
> 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.

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):

> <https://gist.github.com/GoToLoop/1d1eb18f468a7d5758469707276b6ea1>

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

> **[Java OpenGL](https://en.wikipedia.org/wiki/Java_OpenGL)**
>
> Java OpenGL (JOGL) is a wrapper library that allows OpenGL to be used in the Java programming language. It was originally developed by Kenneth Bradley Russell and Christopher John Kline, and was further developed by the Sun Microsystems Game Technology Group. Since 2010, it has been an independent open-source project under a BSD license. It is the reference implementation for Java Bindings for OpenGL (JSR-231).
> JOGL allows access to most OpenGL features available to C language programs through t...

> **[JavaFX](https://en.wikipedia.org/wiki/JavaFX)**
>
> JavaFX is a software platform for creating and delivering desktop applications, as well as rich web applications that can run across a wide variety of devices. JavaFX has support for desktop computers and web browsers\[citation needed\] on Microsoft Windows, Linux (including Raspberry Pi), and macOS, as well as mobile devices running iOS and Android, through Gluon Mobile.
> With the release of JDK 11 in 2018, Oracle made JavaFX part of the OpenJDK under the OpenJFX project, in order to increase the ...

---

<div class="post-metadata">

**Author:** ![gerome](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@gerome](https://discourse.processing.org/u/gerome)\
**Post date:** [January 18, 2020, 6:47am UTC](https://discourse.processing.org/t/java-processing/17141/5 "2020-01-18T06:47:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gerome](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@gerome](https://discourse.processing.org/u/gerome)\
**Post date:** [January 18, 2020, 7:28am UTC](https://discourse.processing.org/t/java-processing/17141/6 "2020-01-18T07:28:59Z")

</div>

> [@hamoid](#):
>
> println(Sum.sumUp(2, 5));

ok now I solved the issue by creating a run configuration described here: [Create your first Java application | IntelliJ&nbsp;IDEA Documentation](https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html)

Thanks Hamoid and (since years:-) GoToLoop!

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [January 18, 2020, 9:21am UTC](https://discourse.processing.org/t/java-processing/17141/7 "2020-01-18T09:21:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gerome](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@gerome](https://discourse.processing.org/u/gerome)\
**Post date:** [January 20, 2020, 7:32am UTC](https://discourse.processing.org/t/java-processing/17141/8 "2020-01-20T07:32:21Z")

</div>

### 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](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](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

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:

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

```

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