I'm reading about new Java 8 features and came to know it comes with Nashorn (Javascript engine). Can someone explain me why would/should I use a Javascript engine inside Java. What programming problems it would solve ? any examples
Thanks,
Ravi
Simply, it would allow you to create standalone JavaScript programs and allow embedding JavaScript in Java. Java must be compiled into what is known as a "machine language" before it can be run. JavaScript is text-based and interpreted, and is interpreted by browsers. So being able to use this technology inside Java is a pretty cool thing. JRuby is another example of a scripting language that can run inside Java, as well as it's Python equivalent Jython. They are all just wrappers for languages to use inside Java. It is already possible to run dynamic languages on top of the JVM, but the goal is to ease new dynamic language implementations and increase their performance.
One advantage I can think of is using JavaScript for the scripting of a Java game.
Hope it helps :)
Related
So, I just realized that Java has a built in API system that works with JavaScript and apparently other scripting languages that are JSR-223 compliant. Now, I'm designing a game engine and I'm wondering if I should just use the integrated JavaScript support or figure out how to use Lua.
QUESTION: Can the Java Scripting API easily support Lua? If so, how?
If you feel like it: Would it be unknown to do the scripting side of a game engine in JavaScript?
EDIT: I need scripting capabilities for my engine to allow the creation of AIs, special voxels (Like blocks in minecraft) and other add-ons to the game. I'm not set on doing all this creation in a scripting language because the difficulty of doing so but a scripting language appears as a good alternative to making JARs for all game content. I'm simply exploring what I can do with Java.
The Java Scripting framework relies on the code implementing JSR-223 being written in Java. As such lua won't work.
However, you can use an implementation of lua written in Java such as luaj which has JSR-223 support included.
Note: luaj is not a complete clone of lua but it's pretty functional and allows you access to java classes etc.
On the subject of choosing a scripting language, as you are using Java as your system programming language, some of the benefits of using lua as a scripting language don't apply e.g. fast, small footprint, excellent C integration.
So really it becomes a personal preference. Javascript would be probably be fine, as would Jypthon, JRuby or luaj.
It is not a wise idea to make a game engine in JS. Despite possible(see Node.js / Three.js), JS is rather slow because is interpreted. Node.js has JIT compilation, but it's still 3-4x(best case) slower than C++ equivalent.
I never used Lua or Java Scripting API, I cannot give you an answer on this, but I think the speed of JS will change your mind.
I need to call some javascript code from my java app, can I do such a thing?
thanks
adi
You can do this using a third-party library like Rhino, but there is no straightforward way to invoke JavaScript code from Java. Though the two have similar names, they have about as much in common as a car and a caramel.
More generally, having programs written in one language interact with languages written in another is often tricky due to the internals of the two programming language implementations not being compatible with another. There are many exceptions to this rule and a lot of effort has been invested in making projects work in multiple languages, but there's often a high startup cost.
Yes, you can, either by grabbing Rhino from Mozilla and using its integration libraries or by using the JDK 1.6 "ScriptEngine" facility.
The version of Rhino (the Mozilla-authored Java-implemented JavaScript engine) included with JDK 6 is pretty old and buggy, be warned.
LiveConnect does this nicely, see the references and examples for JSObject. You'll probably be interested in JSObject.eval, which will give you the ability to execute JavaScript code under the context of any JavaScript object.
What I'm looking for is a scripting engine for Java that would allow users to write simple scripts to control the behavior and events for a game. Something that:
is simple - something easy to pick up, especially for people with some basic programming/scripting experience
provides lots of control - I can easily start/stop/pause scripts and control how much execution time each gets, perhaps how much memory space they can use
is separated from the Java environment itself - No access from scripts to any Java objects or classes, only to those functions I explicitly provide
I've considered the Rhino JavaScript engine, and it would suit my purposes, but from what I've read (example), it's designed to integrate with Java so much that sandboxing it securely would be tricky. I'd rather start with an engine that gives scripts no access to anything by default, than have a fully open one that I have to close up. The scripts might not always be trusted, so the environment should not be easy to break out of.
I'd also consider developing my own language with something like ANTLR, but it's probably more effort than I want to put in.
Any suggestions?
Have you considered Lua?
Google docs preview of a pdf on the subject
Lyrio, G.H.S.O; Seixas, R.B.; Using Lua as Script Language in Games Coded in Java, Proceedings of The North American Simulation and AI in Games Conference - GAMEON-NA, EUROSIS, Montreal, Canada, 2008.
You should give a try to Groovy, a scripting language that easily integrates with the Java platform.
Its syntax is 100% compatible with Java, but it also has a simplified syntax that makes it a suitable language for DSLs implementation.
I don't know for sure if you can stop/pause the execution of Groovy code from a Java program, you should read the Groovy API.
When executing Groovy code from within a Java program, you can specify the context passed to the script and you can query the context modified by the script for output variables. The script can be completely isolated from the underlying Java environment by creating a GroovyShell with an appropriate CompilerConfiguration.
JACL is one such language. It is based on TCL. Whatever you do, don't invent another language. There are plenty of good choices out there.
In my opinion not only language, but way of interfacing is important. JSR 223 is most compatible between different languages, but "native" seems the best (i.e. full object integration of groovy)
Do you know of a runtime written in Java/J2ME, that is capable of reading and executing a script/binary file?
Wikipedia has a complete list. However, you sound like you're probably interested in Jython and JRuby.
I wrote just such a language designed to be small enough for J2ME, and to not use reflection/code generation/etc...
http://www.hecl.org
It's open source under a liberal license, so you're welcome to take it, study it, include it in your own programs, or hack it to make it behave like you want.
For 'regular' Java, there are other languages that do more and are faster and more complete.
I know of an x86 emulator written in Java, JPC
Many JVM Languages - Clojure, for example. There are pretty much hundreds of JVM languages floating around, most of which were implemented in Java - Scala, Rhino, etc.
In terms of unique languages, the major ones are Clojure and Scala. Additionally, there are ports of many major languages to the JVM platform, mostly high-level languages. These include Ruby -> JRuby, Python -> Jython, and JavaScript -> Rhino. A more complete list is here.
This is an impressive list of programming languages for the Java virtual machine :
Programming languages for the Java Virtual Machine JVM
The problem is that j2me can be too limited in its use of reflection to enable this, so you need to investigate your specific target.
In terms of java in general, there are many, such as JRuby, Beanshell, Jython, etc.
I just listened to a Software Engineering Radio podcast where a Sun developer talked about Maxine which is a JVM that is mostly implemented in Java itself. It was a very interesting interview and technology.
So it's feasible that someday Java itself (meaning the standard JVM) will be implemented in Java much like C compilers are written in C (after a bit of bootstrapping).
I'd like to use the Bean Scripting Framework to make some of my Java classes be available to users at my company who wish to write scripts + who may be familiar with Javascript or Python (via Jython) or Ruby (via JRuby).
I can't really find much tutorial documentation on how to get BSF working... what I would like to do is make a Java app that runs a shell, either in Javascript or Jython or JRuby, and exposes some Java classes of mine to the scripting language.
Any pointers? I've glanced through some of the docs at the BSF Resources page but I have a feeling I'm missing something obvious to get started.
(like there must be some shell already out there, complete w/ a rudimentary debugger...)
edit: To clarify -- I know how to run bsf.jar, it works fine. And I know how to run the Javascript shell with Rhino -- but that's specific to Javascript and has nothing to do with BSF. My question is, is there a language-agnostic shell that works with BSF?
To use BSF you need bsf.jar and the library for your scripting language of choice on the classpath. To execute Javascript, no additional libraries are required.
To open an interactive Javascript console try:
java org.mozilla.javascript.tools.shell.Main
Why do you need BSF?
Any JVM language can access Java classes directly. JRuby's way, Jython's. And any JVM language will have support for debugging somehow (an example here).
Check out dynamic JVM programming languages like Groovy or JRuby! You can use your java classes without any modification.
I don't know any language agnostic shell.
You could try jline + javax.script + $language to write it yourself. Hope it helps :)