Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Java Applicaton and a JavaScript running in a browser communicate with each other?

I have a JavaScript embedded in a website, running in a normal browser, and I have a Java Application running on the same machine. I am looking for a way have both communicate with each other.

I assume that creating files in the local file system from inside JavaScript running in a browser is out of question.

The only way I came up with would be to use a server that both programs can send messages to, and from which they poll for new messages.

Is there any other other way to accomplish this?

like image 587
Majiy Avatar asked Jul 12 '26 03:07

Majiy


1 Answers

A couple of ways I saw in practice:

  • You Java Application may listen to some local port which your JS will access for instance via XHR. You'll need to mind cross-site scripting (your JS may need to be loaded from that local URL), but this is doable. The easiest would probably be to run an embedded HTTP server.
  • Your Java Application may be registered as a protocoll handler in the OS. Then JS would open links registered with the application thus sending data to it.

As @PavelHoral is pointing out, CORS is a way to workaround same-origin policy.

like image 183
lexicore Avatar answered Jul 14 '26 15:07

lexicore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!