Can Electron apps be integrated with java code?
Asked Answered
P

6

24

As node.js still lacks important functionality which exists in Java, I would like to use Java instead of node.js, and create the client using a web language (html, js, css..). Electron is cross platform and so does java so it seems fit to have a solution getting the best of both worlds.

Does someone know of a way to integrate electron with java or have a different solution to the problem?

Puncture answered 11/8, 2017 at 15:19 Comment(1)
I've been wondering if it's possible to use github.com/joeferner/node-java to ship a JAR of my Java services inside Electron's Node.js backend via JNI (not command line)Grabowski
C
16

I made something similar, Java back-end with Electron GUI.

You can do it in more ways, it depends on what you need. You can create a jar file and then execute it like terminal:

https://nodejs.org/api/child_process.html

Or you can open a socket communication and talk on a Port. (A lot of documentation: Java (web)socket - Node.js client.io)

In this second way, you can do everything you want, but you have to create your communication protocol.

Your path is not foolish, I am very satisfied of the communication and usage in my work with Java + Electron .

Congeal answered 15/8, 2017 at 10:1 Comment(3)
What do you mean with "create" your communication protocol? Wouldn't it work using WS protocol like this? ws://localhost:8080/ws/wsexampleKid
yes, I mean that you have, in case, to create a proper socket or WS and use it to send messages. So, create a dictionary of messages-actions or something like that, a communication protocol to be used on the socket :)Congeal
you can just build a CLI in java and invoke it like any commandWrand
D
12

I've created a small PoC where Java process is integrated with Electron front-end: https://github.com/jreznot/electron-java-app There you will find a simple TODO List application built with Vaadin/Jetty and Electron.

Donnell answered 6/11, 2017 at 17:26 Comment(0)
L
1

Personally i made my back-end java communicate with the front-end by creating a file with te data then sending it to the main.js to be processed.

Litchi answered 3/8, 2021 at 23:21 Comment(1)
This was my first approach as well and worked well for sometime, but when the functionality began to grow I realized that this process was too slow for production applications and also insecure if don't manage you files correctly.Kid
M
0

DBus and winDbus seems to be an option here. It creates nice abstraction and separation between "frontend" and backend

https://sourceforge.net/projects/windbus/

I'm going to test it on my own soon

Mercuri answered 3/9, 2021 at 8:54 Comment(2)
Hi there. Have you tested it? I want to make the same Electron + Java application and can't choose the best approach for the communication problem. Should I create a service and call it from Electron, or should I use RPC, or DBus... I can't decide.Tews
@Tews Making Dbus a component of an application that is not intended to be a desktop GUI sounds like taking it too far. Far better to use something like ZeroMQ for comms between Java and Electron, and Google Protocol Buffers for message definition. That gives a ton of options for what the comms actually is (ipc, tcp, probably not inproc in this case)Daiquiri
G
0

Hypothetically, one could build an Electron app that bundles a custom JAR (made in a Gradle project in a sub-folder of the project) using node-java to call the custom API from Node.js.

In the ideal implementation:

  • Node.js can parse custom types exposed by the Java code.
  • Node.js can call functions exposed by the Java code, passing in parameter values of said custom types and receiving deals of custom types.
  • Node.js can pass in callback functions as parameter values and receive callbacks from the Java code.

One downside of embedding a JAR will be debugging its failures while running from within the Node.js engine. However, exposing a rich yet simple external API from our Java code can be a win-win for both the Node.js developer consuming the library and the Java developer producing the inner business logic and unit testing.

Grabowski answered 25/8, 2023 at 23:34 Comment(2)
Is thus a question or an answer?Entozoon
I've revised thus to be an answer.Grabowski
C
0

I use http Ajax to call java service back-end now.

Coriolanus answered 16/3 at 12:10 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Inveracity
Can you expand on how you use Ajax to call the Java service? Can you give more details of the Java service? Can you post some sample code demonstrating how to use Ajax to call a Java service? In any case, I suggest that you take the tour and visit the help center in order to learn how this website works.Escutcheon

© 2022 - 2024 — McMap. All rights reserved.