Unless you find a maintained Javascript Runtime project running on JVM, the only way to do that is to have two separate processes, with one of them acting in a "listening" mode - then you can use some "rpc" protocol - "Remote Procedure Call" that works in both languages.
But it seems that several projects have been done to run javascript directly in JVM - a quick google search returns "Rhino", "Ringojs", "Nashorn" and "Graal.js" - the wikipepedia page on liguagens in JVM indicates more information for 3 of these 4 projects: https://en.wikipedia.org/wiki/List_of_JVM_languages
In any of them must have a way of both exposing a javascript function to be called from Java, as well as instantiating objects and calling methods written in Java from Javascript.
The other way, as mentioned above, is to use an RPC protocol - a form of computation in which your objects are serialized in a process, in client language, received and processed in a worker in the other language, and returned by the same channel (or a similar).
To build such an architecture for a single monolithic system would be possible, but reckless - on the other hand, in paradigms of distributed systems, and Microservices, which are very usable, that’s exactly what happens.
The so-called methods may be exposed more conveninently (looking exactly like a function call), or less convenient (you have to manually prepare a payload for the transport protocol, which can be HTTP or other, and check the result of the network call, have a callback point to receive the result, etc...) but with Microservices you are in no way limited to just one language between two communicating services.
So it’s basically choosing a remote call protocol (Rest, SOAP, graphQL, xmlrpc, jsonrpc, Amazon’s SQS kit Developer, google’s Pubsub kit, redis, rabbitMQ, Cap'n Proto, Google buffers Protocol, or any protocol that is a layer above those that is available in both Java and Javascript and go this way.
(I could put each of these terms on google, and put a link here, but it gives anyone interested in studying to do this).
Most of these protocols allow deploying on separate servers, but they will also work on the same machine. Some, such as those using Amazon or Google infrastructure, may need a local communication app emulator to work locally - but this emulator, as a rule, will be available as a Docker image, so even this is quiet.
What is the need for this?
– Andre
I wanted to give a simple example to not complicate things, but just the fact of joining Java and Javascript by itself is quite useful does not find?
– NinjaTroll
It is not the most functional, for any of the languages. Even if it is possible, it is unlikely that there is any real gain from it
– Arthur Siqueira
So could you please help me set a better example? I don’t know how to efficiently relate these two, since I don’t even know how to merge them.
– NinjaTroll
Example: You have a database access function using PHP and PDO, and you want to use this function on a Node.js server. Even if it is possible, it is gambiarra, and in the future will turn into noodle, or give you a certification of Senior Specialist in XGH
– Arthur Siqueira
In summary @Ninjatroll: don’t do it. It will turn into a monster that will devour you...
– Arthur Siqueira
Haha, so I think the question gets more out of curiosity, thanks for the advice
– NinjaTroll
Calling the function directly in the code, as you put it, probably can’t. Maybe the most you can do is that. Anyway, I think it’s worth rethinking because you want to do it. It has great chances of being a problem X/Y and have a better solution :-)
– hkotsubo
That solves the question, I would only have to translate and answer here. OK :D
– NinjaTroll
There is an argument about this in guj forum, besides, apparently, you can mix scripts to your Java code. I don’t recommend either use, but be curious
– Arthur Siqueira