Running java code at KNIME

Hello everyone, can you tell me if it is possible to run java code inside KNIME without external nodes?

Have you tried Column Expression? For an example: Column Expression with IF - #4 by elsamuel

or Java Snippet: Java Snippet (simple) – KNIME Hub

2 Likes

You can find several examples how to use Java Code in KNIME here:

If you need additional Java packages it should be possible to use them. But you wil have to tell KNIME where to find them and import them (not an expert there, just mentioning):

2 Likes

I didn’t manage to implement what you said. I want to run CMD for example. How can I do this with node java snippet?

I didn’t manage to implement what you said. I want to run CMD for example. How can I do this with node java snippet?

You can‘t. That would be a task for a mentioned Bash node or this:

1 Like

Sorry, if this node can run JAVA code, then why can’t it run CMD?

You just know what the deal is. In fact, JAVA itself can run CMD by accessing the registry. And KNIME is JAVA generation. So why not?

The core question: Do you want to run existing Java code or create new one for a specific workflow?

External Tools

  • Runs an external script / command which can launch another java application

This is basically “last resort” as it is very “dirty hack”. Usually better to either somehow integrate the functionality in knime by nodes or java/python code or as custom node or wrap it outside of knime in a web service or such.

Java Snippet Node:

  • Runs your own code and applies it row-wise to each row in the input table

The important part here is “your own code” you will need to write and that the code is run as often as the input table has rows.

Of course you can put code into the java snippet that runs an external cmd via according java capabilities. BUT as said above. This will launch 1 external process per row of your input table which is very likley not what you want. it will for sure be relatively slow.

Python Script Node

I’m mentioning this simply due to the fact that there is a major difference to the java snippet node, namely that you have access to the full table and can much more easily do actions spanning several rows or move back and forth between rows. It’s more complex but more versatile.

2 Likes

Hi @Nuke_Attokurov

Please have a look at the workflow solution posted in the thread below:

Hope it helps.

Best

Ael

3 Likes

Thank you very much. Exactly what is needed.

3 Likes

Glad to read it helped. Thanks @Nuke_Attokurov for your prompt feedback and for your kind comment.

Best wishes

Ael

Good afternoon. Could you help me understand how this piece of Java code works in more detail. I understand that I must now learn Java. It is necessary. But at first, could you tell me more about this code.

Java snippet:

logInfo( c_QUERY);

// Run a java app in a separate system process
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( c_QUERY);

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));

// Read the output from the command
System.out.println(“Here is the standard output of the command:\n”);
out_Result = “”;
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
out_Result += s;
}

// Read any errors from the attempted command
System.out.println(“Here is the standard error of the command (if any):\n”);
out_Error = “”;
while ((s = stdError.readLine()) != null) {
System.out.println(s);
out_Error += s;
}

logInfo(“Executed and waiting for end of process”);
out_comment = “Executed and waiting for end of process”;

int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);

logInfo(“Executed Succesfully”);
out_comment = “Executed Succesfully”;

} catch (Exception ex) {
ex.printStackTrace();

 logError("Error during Execution"); 
 out_comment = "Error during Execution"; 

}

Hello @Nuke_Attokurov ,
I’ll try to explain, hoping with sufficient detail.

The following lines of code picks up the Java Runtime object in order to launch an operating system executable file, specified in the c_QUERY string:

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( c_QUERY);

From this point on, the process starts and runs in parallel with the java code.
The following lines serve to grab the output generated by the process and send it to two buffers, the first
one for the process’ standard output and the second one for the process’ standard error:

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));

The process’ standard output buffer is read line by line and printed:

out_Result = “”;
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
out_Result += s;

The same happens for the process’ standard error.

Finally, the following lines wait for the process to terminate and then print its the exit value

int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);

Note that if the process generates output intermittently (i.e. with pauses) , the “while” cycle will exit after the first pause occurs, and when the process will resume sending output, this one will be lost.
The issue could be resolved using separate threads to read the standard output and standard error of the process.

HTH
Fabrizio

1 Like

Thank you very much.

Good afternoon. Here’s a good thing I thought about. I would like to consult with you. Do you think it is possible to write websites with code in javasnipet and whose support will be regulated through KNIME?

Hi @Nuke_Attokurov

This is a different question to the initial one you posted. I would suggest to post it in a new thread so that answers do not get mixed. Maybe you could also check one of the answers you got as “the solution” so that people know that the question was solved and can find the solution more easily.

Thanks & best regards,
Ael

2 Likes

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