Use Java Snippet to create Path column/variable


just now I realised that the Java Snippet node actually allows the data type “Path” as output. I’ve been waiting for that feature for a while now, because it will allow me to omit the String to Path nodes. However, I can’t figure out how to convert the data type properly. Assigning a string obviously doesn’t work and gives this error message instead:

Error in line 36: Type mismatch: cannot convert from java.lang.String to org.knime.filehandling.core.connections.FSLocation

Didn’t find anything online. Intended path is local only, like “C:\Users\Thyme\my_file.xlsx”. If it can’t be done, that’s ok.

@Thyme I have never tried that but would a path variable have to look something like this:

(LOCAL, , C:\Users\username\Desktop)


Yes, that’s the string representation of Path objects. Or so I think, I’m not an actual Java programmer :sweat_smile:. Building a string like that is easy, but unfortunately of no use, since it’s the wrong data type. I’m looking for the method that accepts a string and returns the path object.
The KNIME documentation doesn’t cover such a niche application (which is how it should be).

I just had the idea of looking into the source code of the StringToPath Node (it does what I want, so the answer gotta be in there). NodePit links to the source. This is unfamiliar territory though, not sure if I’ll have any success with that. StringToPath source

1 Like

Hi @Thyme,

I think the JavaSnippet Node uses the Knime internal FsLocation logic - so a simple
Path path = Paths.get(textPath);
does not work.

If you just want to transform a string to a path via the java snippet you can try the following:

       out_path1 = new FSLocation("LOCAL", "C:\\Users\\Thyme\\my_file.xlsx");

The following FSCategories can be used:

    /** Category for local "convenience" file system(s) */
    LOCAL("Local File System"),

    /** Category for relative "convenience" file systems */
    RELATIVE("Relative to"),

    /** Category for "convenience" file systems that access mountpoints */

    /** Category for "convenience" file system(s) that access only URLs */

    /** Category for file systems that need to be connected via input port. */

Hi @AnotherFraudUser

works like a charm, it’s exactly what I’ve been looking for. Thank you!

Some extra information for future people:

  • KNIME imports the libraries it needs to handle system variables on its own. Once you add a Path column as input or output, this will be added to the system imports automagically:

    import org.knime.filehandling.core.connections.FSLocation;

  • If you’re using a Path column as input, these methodes might be useful (you can also Ctrl + Space for the autocomplete suggestions):

    //returns file system category, such as LOCAL, RELATIVE, etc.; see previous post

    //returns the absolute Path, such as “C:\Users\Thyme\Desktop”

    //returns both of the above, in this format: “(LOCAL, C:\Users\Thyme\Desktop)”

  • Flow variables can only be of type int, double, string (KNIME 4.5). Additionally, the Java Edit Variable Node cannot access Path variables.

Happy New Year, T


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.