Fast, reliabe & crossplatform Javascript API/Library for Java? (for game dev.)

Hello dear JGO :slight_smile:

I looked all over the net, and information seems to be really sparse, or just a very short snipped without a serious explanation.
Usually people point to Oracle’s Rhino or Nashorn. Rhino is ‘old’ and slow, and Nashorn is pretty much brand new as i understand.

I need an API/Library that is fast, reliable, crossplatform and is easily embedabble withing java.(i.e easy to call functions from one to the other etc)

Also, if anyone could tell me if it is a good idea to develop with the latest Java version in LWJGL.
I generally don’t think i should use the cutting edge JDK’s since it will probably cause more bad than good, but i don’t know.
The need to use the latest oracle JDK also makes me want to stay away from Nashorn.

And yes, it really, really needs to be JavaScript :stuck_out_tongue:

Edit: My problem at hand needs reformulation. Here is what i need JavaScript for initially but not exclusively.

I need Javascript to process entites of my EntityComponentSystem.
By that i mean(Pseudoish-code):


for(int i = 0; i < EntityAmount; i++){
     Entity entity = World.get(i);

     ScriptEngine.eval(entity, world);
}

I want to attach a specific script to an entity, and let the script to the work instead of hardcoding.

Thank you very much in advance, your helpt is greatly appreciated!

Depending on what exactly you’re looking for, you might want to check out:

Processing.js: Processing was originally built on top of Java, so it shares its syntax, but it’s designed to make it easy to get something visual and interactive up and running without any boilerplate code. Processing.js is the JavaScript version of that, so you can write Java-esque Processing code and export it as JavaScript.

libGDX: libGDX is the go-to Java game development framework, and you can export libGDX code as JavaScript, as a mobile application, or as a regular Java project.

GWT: GWT is a library and set of tools that allows you to write Java code that is then exported as a JavaScript web application.

If you want to use javascript embedded into java, then nashorn and rhino are pretty much your only options. I also don’t see any real road blocks regarding java 1.8. You also don’t need the Oracle version. OpenJDK should do fine.

Stay away from Rhino, go with JDK 8 and Nashorn. It’s massively faster, is 100% ECMAScript 5.1 compatible and has additional features that make Java interop easier. Also make sure to keep your JDK updated; Nashorn is under active development, there have been many fixes and performance improvements and more are coming.

LWJGL works just fine with JDK 8.

Thank you all very much for the fast answers yesterday ! :slight_smile: i wanted to answer yesterday but didnt have the time to answer properly…

@KevinWorkman

  1. Processing isnt what i need, interesting thing though!
  2. libGDX is what i started with, but i want to do all of that stuff on my own, i don’t have fun coding in libGDX :stuck_out_tongue:
  3. Please excuse my basic knowledge, i case i don’t correctly understand what is is, but isnt that just for the web?
    Your suggestions are very appreciated!

@cylab
I just read about the OpenJDK yesterday, i’ll read into that soon. Is there any drawback on using OpenJDK that you know of? (instead of Oracle/Sun)

@Spasi
I tried to compile a test.js with the ScriptEngine, and it worked fine with Rhino (EngineByName(“javascript”)), then i tried Nashorn(EngineByName(“nashorn”)) and it always crashed. I guess i’ll need a more recent Eclipse version + a cleanup of my installed JRE/JDK, hopefully im able to get the damn thing to compile.

To be honest, i still am rather clue-less about JS. I hoped that there is a simple way of including a LIB, and using it without any hassle.
Also, since i need JS for 2D/3D game applications, i have no means of comparing other than going down that rabbit hole myself, i guess when someone says ‘half as slow as V8’, it is fast ‘enough’… but they don’t programm videogames.

Of what i know now (still reading docs…), Nashorn is going to be a joy to use, but is super young. Is there any comparable alternative i could have a look at?
My googleFu didn’t bring me ANY serious alternative, I don’t want you guys to figure it out for me! If you guys tell me that Nashorn is fine performance and crossplatform-whise, i’ll give it a shot and stop searching for the ‘better option’, because apparently, there isnt anything specifically better suited to gamedevs!?!?

*For anybody reading, this is an interesting read: https://blogs.oracle.com/nashorn/entry/nashorn_performance_work_in_the

Thank you very much for your patience and knowledge! I really need a push in the right direction. <3<3<3

I’m still pretty unclear on exactly what you need. Why do you need a JavaScript API for game development in Java? What goal are you trying to accomplish? What problem are you trying to solve?

You hinted at wanting to do what libGDX is doing under the hood, but note that libGDX isn’t simply calling JavaScript from Java! In fact, I believe it’s using GWT behind the scenes.

[quote]I’m still pretty unclear on exactly what you need
[/quote]
I need Javascript to process entites of my EntityComponentSystem.
By that i mean(Pseudoish-code):


for(int i = 0; i < EntityAmount; i++){
     Entity entity = World.get(i);

     ScriptEngine.eval(entity, world);
}

I want to attach a specific script to an entity, and let the script to the work instead of hardcoding.
I am sorry, i really should have made this clear in my first post! :x

I updated my first post with the problem at hand, it really makes much more sense…

[quote]You hinted at wanting to do what libGDX is doing under the hood
[/quote]
No, it is just that i’m not having fun with using code other people wrote. (Sometimes i have to though ,and sometimes im lazy)

[quote]In fact, I believe it’s using GWT behind the scenes.
[/quote]
I’ll have a look into GWT soon, i just don’t know what kind of stuff it’s all used for. But libGDX is definitely a place to ‘take ideas’ from.
But sometimes i like to have low-level code knowledge :stuck_out_tongue:

I think either Rhino or Nashorn is what you want (as suggested earlier). More specifically, Nashorn is what you want, unless there’s some specific reason that it doesn’t meet your needs.

I use JavaScript embedded in Java for exactly what you describe (behaviors for entities and other general game logic). I’m using Rhino because that’s what I started with, but would use Nashorn if I were starting now (and I may switch to it).

I wouldn’t worry too much about performance unless you actually run into problems. Also, which platforms are you targeting? (Maybe you mentioned this already.)

[quote]I wouldn’t worry too much about performance unless you actually run into problems.
[/quote]
True, i am also not trying to optimize prematurely, but i am a little paranoid when i get into new topics :slight_smile:
I guess i will stick with Nashorn for now, i just tried the OPenJDK 8.60 64 bit, but havent done any performance tests yet, maybe i can post some results once i get some stuff running. As they also try to improve the ‘warmup’ problems, i’m looking forward to see how good Nashorn is going to run soon without long warmup.

[quote]Also, which platforms are you targeting…?
[/quote]
Well, for the time beeing PC,MAC,LINUX. And i’ll worry about iOS when the time comes, whenever that will be :stuck_out_tongue:

How is Rhino working for you? Do you have some kind of raw numbers, concerning the amount of entites and performance problems with JS? I’d be really interested in how Rhino performs, allthough i’m not going to use it.

What I’m doing with JS isn’t really taxing enough for performance to matter, so for my purposes Rhino is working fine (I haven’t had any need to perform any diagnostics or collect any performance-related data). Again though, I’m just using it because it’s what was available when I put the scripting system in place.

Nashorn is still unreliable. I tried Nashorn a few months ago and was disappointed that it threw the following exception

java.lang.RuntimeException: Method code too large! 

I was attempting to run the katex math html rendering library (https://github.com/khan/katex/releases) server-side using java.
Apparently this exception is because javascript methods can be too long for the JVM:
http://stackoverflow.com/questions/26329503/troubleshooting-nashorn-method-code-too-large-exception

@Jesse
I guess that performance won’t really be a concern most of the time anyway, sometimes it is just the rest of my ‘performance paranoia’ kicking in :stuck_out_tongue:

@CommanderKeith

[quote]Nashorn is still unreliable. I tried Nashorn a few months ago and was disappointed that it threw the following exception
[/quote]
That sucks, but it is very good to know that this is even an issue, thank you very much!
I know from reading some of their update posts, that they work really hard on it.
That’s what i was afraid of, the issues one has with a work in progress. Though, i love how the java guys communicate, they show that sometimes, they are struggling, like all the rest.

[quote]I tried Nashorn a few months ago…
[/quote]
Sounds like you are using some other scripting Lib/API for the time being? I’d be very interested in what you use then !? :slight_smile:

The first question to ask yourself is: Do I really need dynamic typing. If no then do something else.

Sounds like you are using some other scripting Lib/API for the time being? I’d be very interested in what you use then !? :slight_smile:
[/quote]
For scripting you might like to try janino which is pure java. I highly recommend it, especially if you’re not keen on javascript’s loose syntax. Janino is as fast as java since it’s compiled.
http://docs.codehaus.org/display/JANINO/Home

I’m not sure if you’re interested in html maths rendering, but I continue to use MathJax which is ok but it re-flows the html page for the client which is not ideal.

Cheers,
Keith

@Roquen

[quote]Do I really need dynamic typing
[/quote]
It would be awesome to have, but i don’t know at what price that’s coming yet. I guess anything extra, like dynamic typing, comes at a performance penalty!?

@CommanderKeith
Thank you very much! I’ll also look into that, always good to have some kind of suggestion/reference.

[quote]I’m not sure if you’re interested in html maths rendering
[/quote]
Not quite yet, but i have to look at something during my dinner :stuck_out_tongue:

I advise you to look at the Oracle BCL for Java SE and JavaFX if you plan to embed it into your application:
http://www.oracle.com/technetwork/java/javase/terms/license/index.html