1

Probably this is is a very basic question. Apologies for that. I have been trying at various ends to find an answer for this.

My question is like this:

I have a java code relating to reading an inbox. I need to call this code/method/class in the new javascript file, which I will be developing.

Basically, I need to use the methods in the java file onto the new javascript file

Can anyone please help?

6
  • Just to be clear - you're trying to get a JAVA and a JAVASCRIPT method to work together? i.e. you're using DIFFERENT languages? Because if so, I don't think you can do this without some sort of external library. What's your setup? Can you provide any more info about your situation? Commented Feb 12, 2019 at 9:40
  • 1
    Java and Javascript are different languages and don't have anything in common except Java word, are you aware of this? What did make you think its possible in the first place? You can try something like jsweet.org to translate Java, it isn't guaranteed to be workable. Commented Feb 12, 2019 at 9:40
  • In what environment? In the JVM? In a browser? In a browser-hosted page/app that calls Java running on a web server? Something else? Commented Feb 12, 2019 at 9:40
  • @estus - The JVM has support for compiling and running JavaScript code out-of-the-box. So yes, it's entirely possible to call Java code from JavaScript code directly, if they're both running in the JVM. (Though I doubt that's what the OP is trying to do. :-) ) Commented Feb 12, 2019 at 9:41
  • @Thornkey - I have a java code which will read the mailboxes but I don't know how to call this code into my javascript file. Basically, the entire framework written is in javascript but since it became bit tough to 'reading inboxes' due to authentication, we wrote it in java just to check if we can integrate it in here and start working. Commented Feb 12, 2019 at 9:48

1 Answer 1

3

There are certain options, as this questions is primarily opinion based, so here is my opinion.

  1. Using Ajax. You can call your back end methods through Ajax.

  2. Another option is using DWR, Direct Web Remoting, its a sort of ajax but made easy. The java methods are made to be used by javascript. Full example: https://www.javaworld.com/article/2071890/web-app-frameworks/ajax-made-simple-with-dwr.html

function updateResults() {
  DWRUtil.removeAllRows("apartmentsbody");

  var bedrooms = document.getElementById("bedrooms").value;
  var bathrooms = document.getElementById("bathrooms").value;
  var price = document.getElementById("price").value;

  ApartmentDAO.findApartments(fillTable, bedrooms, bathrooms, price);

  $("resultTable").style.display = '';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Since Java code should be called from a JavaScript framework AJAX is an absolutely viable option.
Thanks. I will try out this method :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.