skip to content
An anime girl with glasses

Call Java Functions from Rascal

/ 2 min read

Rascal 🤝 Java

The Problem

I have my Rascal code in one project, while having my Java project separately in another one, to keep things a little bit cleaner. However, I have to call Java functions from tRascal which makes everything a bit tricky.

My (crappy) solution

In the Java / Gradle side of things you add the following (in my case it is a Kotlin project, so the syntax for Gradle is slightly different). Keep in mind that you need to add the *.jar to your project resources folder.

Java Side

dependencies {
    implementation(files("<your-local-path>/src/main/resources/vallang-0.15.1-sources.jar"))
    implementation(files("<your-local-path>/src/main/resources/rascal-0.33.0.jar"))
}

Then add a Java class with some functionality:

package com.sample;
import io.usethesource.vallang.IValueFactory; // This interface is important, otherwise
// it won't work

public class Teste {
    private final IValueFactory vf; // Boilerplate code to make it work

    public Teste(IValueFactory vf) {
        this.vf = vf;
    }

    public void testeJava() { // The actual function
        System.out.println("it worked");
    }
}

And build it with your gradle script.

Rascal Side

In the Rascal side of things you add the following code:

@javaClass{com.sample.Teste}
java void testeJava();

In your Rascal project, go to the pom.xml and add the local dependency of you build

<groupId>com.sample</groupId>
      <artifactId>sample</artifactId>
      <version>1.0</version>
      <scope>system</scope>
    <systemPath>*INSERT THE PATH TO THE BUILT JAR*</systemPath>
</dependency>

You might need to execute a mvn clean install to make it work. Open up a new Rascal terminal, import the Module where you use the java function call and just call it (in this case testeJava())