How I can transfer my java code to the "java snippet"

Hi everyone. I need to transfer my java code to the “java snippet” node. at the same time, the code can be large, even an class. I want it all to work. How to do it?


Hi @Nuke_Attokurov , if your Java code is actually in the form of whole Java classes that you want to use as classes, you can’t add them into Java Snippet since each class needs to be in its own file. In that case i would suggest bundling them into a jar file which can be dropped into the workflow data folder and referencing them from the Java snippet on the additional libraries tab. You’d then instantiate the classes and make calls from Java snippet as required.

On the other hand if you just want to use small segments of the code then you can copy/paste the methods into Java snippet.

So it depends on your situation.

If you are going down the jar file route, you may find this component useful which allowd you to have a jar file available at a download location (e.g on internet or a local server) and it automatically downloads it to your workflow data folder.

2 Likes

Oh, thank you very much. I’ll try your method. Maybe that’s exactly what I’m looking for.

1 Like

You’re welcome @Nuke_Attokurov , thanks for marking the solution and I hope it works out.

I will be interested in hearing experiences of the jar download component as I’ve only used it a couple of times myself, but actually only with publicly available jars rather than my own.

You probably found this already but for portability, provided you have already executed the Download JAR file to fetch the jar…
e.g.

… you can reference the data folder on the “Additional Libraries” tab by using the “Add KNIME URL”, and whilst it doesn’t give the option to browse for the file, you can specify the folder and jar file name using this format:

knime://knime.workflow/data/MyJarFile.jar

Then it doesn’t matter where the workflow gets saved, as it should always find the KNIME workflow-relative path to the data folder.

Of course, whilst the Download JAR file component was written for this specific purpose, it doesn’t actually have to be a jar file… It can probably download any file you like to the data folder, so I would think it could be used for pulling down other resources too if required.

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