title: How to connect NodeJS and Java?
body: <p>I need for my Node application to be able to send and recieve data to/from an already running Java app or to launch Java app from Node.</p>
  <p>So, is there a way to send data from NodeJS to Java and back?</p>
  <p>My goal is to have something like this:</p>
  <pre class="lang-java prettyprint-override"><code>// In Java:
  J2JSInterface.setReciever(msg -&gt; System.out.println(&quot;Message from JS: &quot; + msg));
  J2JSInterface.send(&quot;Hello from Java!&quot;);
  
  // In Java console:
  // Message from JS: Hello from JS!
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>// In Node:
  JS2JInterface.setReciever(msg =&gt; console.log(`Message from Java: ${msg}`))
  JS2JInterface.send(&quot;Hello from JS!&quot;)
  
  // In Node console:
  // Message from Java: Hello from Java!
  </code></pre>
  <p>P.S.: I Tried using JNI and Node Api in one C++ library together, (<a href="https://gist.github.com/Mrcavas/4be02db1b4c0e01bb952b52847bacd45" rel="nofollow noreferrer">code</a>) but it won't work because Node and Java will load two independent instances of the library and these instances can't interact with each other. (or can they?)</p>