How do I get Java and Javascript working together?

I have been working for the past year on a java application, and so far it have been working well. But today my boss wanted to run some javascript code in my program. We talked and researched on how we could achieve this. Sadly nothing we have tried work, so when I got home I decided to see if I could reprogram the java application into a applet, and then invoke the javascript functions that way. But I have a hard time getting the applet to even run. All I get is java.lang exceptionininitializererror. I been doing some research and many advice to stay away from applets.

So I’m not sure what to do, I could talk to my boss, and see if I could reprogram the whole thing in HTML5 using that and Javascript, while I can do that, I don’t have that much time until a client is going to look at the program. Is there another way, or do I simply have to work overtime to do it?

I think you’re looking for Nashorn

I have tried that, sadly the Javascript functions have document and window inside of them, and from my testing, Nashorn don’t like those.

one could say : nashorn/rhino is ECMAScript minus the browser api. or : javascript is an implementation of ECMA.

practically, browsers “inject” the document and window objects/api, which is not available in java nashorn/rhino. there is no “window” in plain java.

There are different options here.
You could change the JavaScript code to make it work in Nashorn.
You could also translate your Java code to JS.
The third option would be to find a way to allow Java and JS to communicate e.g. through XMLHttpRequest or WebSockets.
I suggest you also post your question on stackoverflow, preferably with a more detailed description and example code.

Normally I hate when people ask this question, but why does your boss want “some” javascript to run in the application? I would seriously question this decision on a project you’ve been working on for a year and nobody foresaw the need.

That being said, DrZoidberg’s third option is probably the best solution (design-wise).

Answer some more questions:

  • where does your application run? (Clients PC, server?)
  • how is it started? (Script, service, clickable jar)
  • does it have an UI?
  • what does the javascript do?
  • why should it be integrated?
  • what does your application actually do?
  • is it run by a single client or should it support multiple users in parallel?

You don’t have to give away everything, just enough for us to see, how the overall picture looks.

I suspect that the implementation somehow does not match the architectural requirements, when you run into those troubles.

  • where does your application run? (Clients PC, server?)
    The application run on a clients PC, on start up it connects to a PHP script using TCP sockets.

  • how is it started? (Script, service, clickable jar)
    The client is simply started by double clicking the jar, and reads a .properties to get the IP and Port it should connect to.
    The server is started by a cmd command “php -f <path to a .php file>”

  • does it have an UI?
    Yes, most of the time it shows a list of users, and sometimes questions.
    The interactions are limited to Yes, No, Pin, and RFID Tags

  • what does the javascript do?
    Text to speech. My boss and I tested several ways to get this feature into the java application, at one time we used google text to speech site, to download the audio files. But now google have added methods to prevent that, and ever since we have tried to find a alternative which supports our native language.

  • why should it be integrated?
    The application will be used to conduct surveys where most members have poor eyesight, can’t read, or have other handicaps

  • what does your application actually do?
    Its main function is to stamp in and out for shifts, and as well as conduct surveys when it needs to.

  • is it run by a single client or should it support multiple users in parallel?
    The server support multiple clients to be connected at once, and aside from the surveys, show mostly the same data on all of them.

I talked to my boss today, and we think using javascript and canvas would be the best solution. While I don’t like abandoning the java client, at the moment it seems to be the only solution.

Why not try (marytts)

http://mary.dfki.de/index.html

Wait, what are you using the javascript for? Text to speech is not an answer. And why would you need the window/document/DOM?

It makes no sense.

Thanks, I will try it.

“Text to speech is not an answer”, I beg to differ, http://responsivevoice.org/api/ if you search in the code, you will see that it uses both the window and document objects.

Ok, so the goal is to include text-to-speech into your application. If marytts does not match your needs, another option would be to write a small jni wrapper around c based libraries. There are some open source ones and also a (free?) one by microsoft.