# Running java code at KNIME

**URL:** <https://forum.knime.com/t/running-java-code-at-knime/40811>\
**Category:** KNIME Analytics Platform\
**Created:** [March 17, 2022, 1:53pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811 "2022-03-17T13:53:27Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 17, 2022, 1:53pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/1 "2022-03-17T13:53:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![victor\_palacios](https://forum-cdn.knime.com/user_avatar/forum.knime.com/victor_palacios/32/37922_2.png) [@victor\_palacios](https://forum.knime.com/u/victor_palacios)\
**Post date:** [March 17, 2022, 2:06pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/2 "2022-03-17T14:06:36Z")

</div>

Have you tried Column Expression? For an example: [Column Expression with IF - #4 by elsamuel](https://forum.knime.com/t/column-expression-with-if/37043/4)

or Java Snippet: [Java Snippet (simple) – KNIME Hub](https://hub.knime.com/knime/extensions/org.knime.features.javasnippet/latest/org.knime.ext.sun.nodes.script.JavaScriptingNodeFactory)

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [March 17, 2022, 2:09pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/3 "2022-03-17T14:09:07Z")

</div>

> [@Nuke\_Attokurov](#):
>
> run java code inside KNIME

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

> **[Meta Collection about KNIME and Java – mlauber71](https://hub.knime.com/mlauber71/spaces/Public/_knime_and_java_meta_collection~xLnTTC6zyiEveNq5/current-state)**
>
> Meta Collection about KNIME and Java As a platform KNIME is well suited to collaborate with all kinds of tools and systems, including Java. Collection of Java …

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):

> [@Import class from external jar into a java snippet](https://forum.knime.com/t/import-class-from-external-jar-into-a-java-snippet/6052/4):
>
> No issues here. I'm attaching an example using the Apache Commons Math lib, but I've recently used different libraries as well without problems. My usual steps: Add the Java Snippet node to the workflow Open configuration Go to "Additional Libraries", add the jar file (in case you want the workflow to be portable, add the JAR file into the workflow and specify a "KNIME URL") Go to "Java Snippet" tab,&nbsp;import the classes in the "Your custom import" section, e.g. write&nbsp;"import org.apache.com…

---

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 17, 2022, 6:36pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/4 "2022-03-17T18:36:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [March 17, 2022, 7:01pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/5 "2022-03-17T19:01:07Z")

</div>

You can‘t. That would be a task for a [mentioned](https://forum.knime.com/t/how-can-i-execute-exe-in-knime/40810/2) [Bash node](https://kni.me/n/U2uB-qIId1RRfj0Y) or this:

> **[External Tool](https://hub.knime.com/knime/extensions/org.knime.features.ext.exttool/latest/org.knime.ext.exttool.ExtToolNodeFactory)**
>
> Allows running an external program on the data. It will write out the incoming datatable into a temporary file which can be used as an input to an external too…

---

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 18, 2022, 4:25am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/6 "2022-03-18T04:25:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kienerj](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kienerj](https://forum.knime.com/u/kienerj)\
**Post date:** [March 18, 2022, 6:00am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/7 "2022-03-18T06:00:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aworker](https://forum-cdn.knime.com/user_avatar/forum.knime.com/aworker/32/36421_2.png) [@aworker](https://forum.knime.com/u/aworker)\
**Post date:** [March 18, 2022, 9:03am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/8 "2022-03-18T09:03:56Z")

</div>

Hi @Nuke_Attokurov

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

> [@Ping bulk ULR with batch and Knime](https://forum.knime.com/t/ping-bulk-ulr-with-batch-and-knime/38646):
>
> Dears, I need to run a below batch file with KNIME, to get ping result of the many URL. Is there any way to running batch file and get result in Knime? @echo off for /f "delims=" %%a in ( ' type "e:\url-list.txt" ' ) do ping -n 1 %%a \>nul && (echo %%a ok) || (echo %%a failed to respond) pause Save as as a .BAT: BR, Milad

Hope it helps.

Best

Ael

---

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 18, 2022, 12:29pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/9 "2022-03-18T12:29:20Z")

</div>

Thank you very much. Exactly what is needed.

---

<div class="post-metadata">

**Author:** ![aworker](https://forum-cdn.knime.com/user_avatar/forum.knime.com/aworker/32/36421_2.png) [@aworker](https://forum.knime.com/u/aworker)\
**Post date:** [March 18, 2022, 12:44pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/10 "2022-03-18T12:44:31Z")

</div>

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

Best wishes

Ael

---

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 19, 2022, 7:20am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/11 "2022-03-19T07:20:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 19, 2022, 7:23am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/13 "2022-03-19T07:23:12Z")

</div>

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"; 

```

}

---

<div class="post-metadata">

**Author:** ![arofab](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/97f17d/32.png) [@arofab](https://forum.knime.com/u/arofab)\
**Post date:** [March 19, 2022, 1:00pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/14 "2022-03-19T13:00:13Z")

</div>

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:

```auto
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:

```auto
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:

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 19, 2022, 2:23pm UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/15 "2022-03-19T14:23:45Z")

</div>

Thank you very much.

---

<div class="post-metadata">

**Author:** ![Nuke\_Attokurov](https://forum-cdn.knime.com/user_avatar/forum.knime.com/nuke_attokurov/32/31004_2.png) [@Nuke\_Attokurov](https://forum.knime.com/u/Nuke_Attokurov)\
**Post date:** [March 20, 2022, 8:10am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/16 "2022-03-20T08:10:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aworker](https://forum-cdn.knime.com/user_avatar/forum.knime.com/aworker/32/36421_2.png) [@aworker](https://forum.knime.com/u/aworker)\
**Post date:** [March 21, 2022, 8:59am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/17 "2022-03-21T08:59:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [June 19, 2022, 8:59am UTC](https://forum.knime.com/t/running-java-code-at-knime/40811/18 "2022-06-19T08:59:42Z")

</div>

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