Error Javascript when using @runtime

Hello,
I use OH 4.2.1 on a Synology DS under Docker and would like to save an image item to the filesystem and wanted to use the variable Files from require("@runtime").

But I get the error

2024-11-20 09:22:16.658 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule aa_JS-Test-d0d105cc-5223-4dde-a69b-4eff1a63e90b: TypeError: invokeMember (write) on java.nio.file.Files failed due to: Unknown identifier: write: TypeError: invokeMember (write) on java.nio.file.Files failed due to: Unknown identifier: write

when calling

const fPath = Files.write(fp, imageBytes, Files.WRITE);

I can’t see where my error is. Can anybody give me a hint.

The Test-Rule is

var runtime = require("@runtime");
var Files = runtime.Files;

rules.JSRule({
    name: "aa_JS Test",
    description: "Test Rule JS",
    triggers: [
        triggers.ItemStateChangeTrigger('swTestSwitch', 'OFF', 'ON')
    ],
    execute: (event) => {

        const OPENHAB_CONF = "/openhab/conf";
        const itemName = "SnapShot_Arenti_IN1_1";

        const fp = OPENHAB_CONF + "/html/img/" + "picture1.jpg";

        const imageBytes = items[itemName].rawState.getBytes();

        const fPath = Files.write(fp, imageBytes, Files.WRITE);

        console.log("fPath: " + fPath);

    }
})

Files.write takes a Path as first argument, not a string.

You could construct it with Path.of(string).

To provide some background on @apas_csc’s correct answer, eveything imported through @runtime is a Java Class. So you will need to look at the JavaDocs for how to use them. If it’s core Java like Files it’s going to be the Java 17 javadocs. If it’s openHAB specific, (e.g. runtime.ir) you’ll need to look at Overview (openHAB Core 4.3.0-SNAPSHOT API).

If you wanted to stick to JS and not have to mess with the Java, it should be possible to install the file-system Node module using npm and use that for your writing needs. file-system - npm

Thx, @apas_csc for your hint.
I have added the following parts

var Path = runtime.Path;

and

var p = Path.of("/openhab", "conf", "html", "room", "picture1.jpg");

or

var p = Path.of(fp);

but now the error is

2024-11-20 17:15:01.101 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule aa_JS-Test-3e50a498-ab4e-4d45-bc01-6f1a8296ba3b: TypeError: invokeMember (of) on java.nio.file.Path failed due to: Unknown identifier: of: TypeError: invokeMember (of) on java.nio.file.Path failed due to: Unknown identifier: of

So I’m giving file-system node module, as @rlkoshak says, a chance. :innocent:

Be aware that not everything in Java is contained in @runtime. It seems likely that you might need to import the Path class using Java.type("java.nio.file.Path");.

However, the Path class is listed in the @runtime-documentation :thinking:

Yes, but I also couldn’t get it to run. I wonder if this is because all methods on Files and Path are interface default methods.

If it’s listed then it should be included. I just wanted to warn that not every class provided by Java is part of @runtime. Or it might be a different Path then you think that is included.

There are also weirdness that happens when trying to use static methods from the classes imported through @runtime compared to using Java.type() (i.e. the static methods are often missing).

Since the error is saying that of() doesn’t exist on java.nio.file.Path but it’s [clearly part of the Class](Path (Java SE 17 & JDK 17) it seems likely that weirdness is happening here.

I bet that’s the case. An Interface doesn’t acutally implement the methods. You’d need to use one of the Classes that implement the Interface.

File is a real class though so it should be usable. And File has a toPath() method so maybe one can get to a Path through a File Object. There’s a constructor that takes a String.