Is it possible to merge Java with Javascript?

Asked

Viewed 181 times

0

For example, if I have a function soma(a, b) in Javascript and I want to use this function in a Java code, is it possible? If so, how?

Javascript:

function soma(a, b) {
    return a + b;
}

Java:

public class SomaJS {
    public static void main(String[] args) {
        int a = args[0];
        int b = args[1];
        int res = soma(a, b); // Função JavaScript

        System.out.println(res);
    }
}
  • 5

    What is the need for this?

  • 1

    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?

  • 1

    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

  • 1

    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.

  • 2

    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

  • 2

    In summary @Ninjatroll: don’t do it. It will turn into a monster that will devour you...

  • 1

    Haha, so I think the question gets more out of curiosity, thanks for the advice

  • 3

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

  • That solves the question, I would only have to translate and answer here. OK :D

  • 1

    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

Show 5 more comments

1 answer

5


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.

  • Wow, what an incredible answer! I had no idea I could do these things

  • 1

    sorry - I would really like to put some examples, but it would take me a lot of time - but yes, you can do all this. A long time ago (about 10 years) I researched a lot of this to call a language from another, but always involving Python, which is my "home". Today, however, the idea of Microservices allows any-language for any language - and is so Voce look for a tutorial from Google Pubusb, or Amazon SQS to see how to do.

  • I’d like to make an answer from running Java about Javascript: a different view than the one you present here. While here you’re talking about JS on top of JVM, I’d like to show you how to run Java on top of any JS engine. And obviously no RPC involved, otherwise it wouldn’t be fun =) Who knows at the end of the day I can make that answer?

  • Python’s Runtime compiles, with LLVM for webassembly, and can run on a browser or Node - the project is this one: https://github.com/iodide-project/pyodide - the same approach can be taken for any Java Runtime - possibly there is one. In this case it is the original Runtime, which runs in javascript as if the webassembly opcodes were a CPU. There’s no reason why it doesn’t work - it might be hard just to get the details of compiling some Java Runtime.

  • but - running a JVM on top of a javascript Runtime is one thing - having an interface on that JVM to interact with the Javascript namespace (and possibly the host page DOM) is another - there has to be specific code. Let’s see if there are any cool projects out there - waiting for your experiments!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.