Using FileUtils from org.apache.commons in a OH3 script

I’m trying to read a local file an generate a base64 encoded string from it, to process it later. For this I created following code:

    val file = new"/openhab/conf/html/carport/ipcamera-" + suffix + ".gif")
    val byte[] filebytes =
    val encoded = java.util.Base64.getEncoder().encode(filebytes)

Unfortunately this ends in the following log message:

   The method or field org is undefined; line 10, column 577, length 3

I found some comments, that a lot of org.commons was removed in OH3, but if I reference it directly it should still be available, no?
If not can you recommend any alternatives to get the base64 string of a file?

It probably fails because you declared the type to be both val and also byte[]. It should either be val or byte[].

It will be more future proof to use the Java NIO Files class to read all bytes from a file:

val path = java.nio.file.Path.of("/openhab/conf/html/carport/ipcamera-" + suffix + ".gif")
val filebytes = java.nio.file.Files.readAllBytes(path)
val encoded = java.util.Base64.getEncoder().encodeToString(filebytes)

In OH 3.0 you can still use Commons IO. However it will be removed in OH 3.1.
Most of the Commons IO functionality is already provided by standard Java classes/methods in Java 11.

Thanks the declaration wasn’t the issue, but your example with nio works perfectly.

Thank you.

1 Like