M
M
MoZg9112018-10-17 10:00:33
JavaScript
MoZg911, 2018-10-17 10:00:33

Interaction of Java and JS applications?

There is an application in which you can execute scripts in JS. The task is to send e-mail from this application. I want to write an application for sending email in Java. The application runs on OS Windows.
How to organize the interaction of JS and java programs? What types of interaction exist?
In JS, you can create an ActiveXObject object and work with its fields and methods. Is it possible to organize work with a Java application in a similar way?
Or is it necessary to create a web service and send all requests to the Java application as http requests (not very convenient for local use)?
PS. Roman Roman- many thanks for the detailed answer. The fact is that I need to call the java method from js (a third-party program with its own functionality), and not call js from java. A program that executes a js script throws a "Java - definition is missing" error when I try to instantiate a java class in the script via Java.type

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alex_137, 2022-01-26
@alex_137

Hello. Probably the answer is not relevant in time, but I will comment. Might be useful to someone:
I used Java FX and its WebView element, got the browser window object and called back functions in javascript.
Getting the window object.

browser.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
  @Override
  public void changed(ObservableValue ov, State oldState, State newState) {
     if(newState == Worker.State.SUCCEEDED) {
          JSObject jsobj = (JSObject) browser.getEngine().executeScript("window");
 jsobj.setMember("bridge", jl);
       }
   }
});

We create the so-called bridge
private Bridge jl = new Bridge();
public class Bridge {
   public String elementId() {      //отправляем обратно в SVG 
     System.out.println("receive id");
      return numberID;
  }
   public String elementIdText() {
      if (numberID.startsWith("tspan")) {
         String answer = "Java Callback";
         iclick = true;
         answer+=' '+ numberID;
        return answer;
     }
     return "Finish";
  }
}

javascript - reverse functions
function doCallbackText() {
   var SVG = document.getElementById('Capa_1');
   var path = bridge.elementId();   //читаем из приложения
  var path1 = bridge.elementIdText();
  if (path1!= "Finish") SVG.getElementById(path).textContent = path1;
   console.log(path1+"Вставляю текст");
}

For more details and other methods see here: https://www.ap-impulse.com/java-and-javascript-con...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question