Java + Scripting languages (JSR 223) - java

I'm designing a Framework which I want to attach to the scripting language API to make it more versatile and easier to use (for some things I really prefer the scripting way myself ;). With languages like JRuby, Jython or Rhino (JavaScript) there are interpreters for many popular scripting languages available and as far as I read, all of them implement the Java Scripting language API to embed them in your Java application.
Do you have experience with running it that way? I'm especially interested in handling e.g. associative arrays (or Java Beans).
How is the performance (e.g. compared to a CGI like approach or the native Java way)? Would it be easy to switch between the different interpreters (of course it is an API specification but I still don't know on how to handle language specific issues)?

I have run Rhino, Jython, JRuby, and Groovy. There is the obvious language difference between them all and performance is pretty slow across the board. I've found that Groovy was the easiest to create a domain specific language (DSL) for my application. Groovy was also the easiest language to control in terms of package accessibility and runtime variables but required using Groovy API to do it instead of JSR-223.
I feel like the Groovy tooling/documenation/api meshes better with the JVM, but certainly ruby/python have quite a following and the syntax may be more comfortable for some. Ultimately I'd try them all out in your framework and choose one. Multiple scripting languages sounds nice but might be a headache to debug/support/transition.
after though: You could check into BeanShell

Related

Fastest scripting language for Java?

I'm making a falling sand game in Java. I want users to be able to write their own engine for the game and I thought a scripting language might work for that. I've tried out a small script with jython and it's many times slower than that java version.
I need a scripting language that has fast loops and/or fast array access since that's what the game will be doing a lot of. Or if you have any suggestions on another way to let users write their own engine for the game.
I'm also not entirely sure this can work (well). If you don't think it can please let me know why and maybe some possible alternatives.
JavaCC
You could write you're own simple game language with JavaCC. This does give's you all the flexibily you possibly want and native Java speed (well that actualy depends on you're implementation). But you need to keep you're syntax simple or else it will take some efford...
Java
Java? Yes Java! Why not let users extend you're game by Java code? In Java 6 there is a Java compiler API:
javax.tools API.
Beanshell vs Rhino
Some performace comparison between BeanShell and the Rhino (Javascript) interpreters (Rhino is the winner):
thoughtworker.in
Pankaj Kumar's Weblog
You might look at BeanShell for this. In my use it's been pretty much as fast as native Java (as that is what it becomes) and is pretty accessible as it is Java, albeit locked around Java 1.4.
RelProxy is Java compiler on the fly and an automatic hot class reloader, pure Java code can be executed like a scripting language.
Because Java is faaast Java is then maybe the fastest scripting language, optionally you can use .class files like a sort of compiled cache to avoid compiling on load.

Best dynamic language to pair with Java on a Java project

What is the best dynamic language to pair with Java on a large Java project?
We are considering using a dynamic language for tests, controllers, services. Some options are Groovy, JRuby or Jython. What are the pros and cons of each for this? Ideally we'd be able to call Java from the dynamic language as well as call the dynamic language from Java.
EDIT: If it helps, we're using Hibernate with PicoContainer and Webwork.
Thanks,
Alex
There are really three dynamic languages that offer a very seamless interop with Java - scala, groovy and clojure. From there, I'd ask your team which language they would rather work in or have them try a prototype in each language and see what they think.
If the team efficiency isn't important in the beginning, look to what problem each language attempts to solve:
Groovy is going to be very loose but natural to experienced Java developers and allows fast prototype development due to it's duck typing.
Scala is going to enable you to write DSLs making it a good for frameworks and tools where you want to solve the problem in a language more akin to how you would describe the problem.
Clojure is going to impose lisp's functional programming and immutable state concepts and could be a very natural fit for problems in AI, natural language processing, etc.
Finally, I've gone down the path of looking for the perfect language to base projects on and have found there is no perfect language. All of the languages I've mentioned above compile to native JVM byte code and are quite solid. Sometimes you just need to pick a language that might not be as cool as the others but gets you on the way to solving your problem.
I recommend Groovy, principally because it interoperates seamlessly with Java, and is almost a superset of Java, so very easy for a Java developer to learn. I have absolutely no evidence to support this, but based on hearsay, guesswork, and personal experience, I suspect the Groovy community is much larger than that of either JRuby or Jython.
Incidentally, Groovy++ is way too immature to consider for production use, in my opinion.
The answer is, of course, going to depend somewhat on matters of taste and flexibility. If there are folk who don't have experience with Ruby or Python then Groovy is going to have a syntax much closer to Java (in fact it is a superset of Java), and consequently be a much easier sell.
I can't really speak to JRuby as I haven't used it.
Groovy gives you probably the easiest interop with Java of the three you listed. It also has a very nice BDD library in EasyB which I like a lot. On the negative side I don't think the features or syntax of Groovy really hang together very well. It can kind of feel like a whole bunch of separate extensions to Java.
Jython is of course Python so the syntax is different, but also has all the consistency of Python. Interop is very good at the script level but at least used to be a bit awkward if you wanted to write pre-compiled classes in Jython that you call from Java. The other main pro over Groovy for me is that is that you have a real REPL to interact with the Java project.
I would also mention Clojure, the syntax is even more different but the Java interop is excellent, probably the best of all, and again you have a REPL. On the down-side if folk have trouble adjusting to Ruby or Python syntax then a Lisp is probably right out.
Clojure is probably the best dynamic language for controllers and services. (depending on what you mean with "services".
Scala and Groovy++ has the best java interop, but those are not dynamic (well in Groovy++ you decide for yourself what is typed.). Scala has the look and feel of a dynamic language. Scala has good testing frameworks http://www.scalatest.org/ and Specs and AKKA is very mature for services and also has Java APIs
I'd suggest going with Jython. The syntax is clean, and you get whatever additional power/conveniences that Python gives you.
For example, if you were to go with Groovy, you are basically limited to only what Java will give you. Jython would add the powers of Python to that as well.
If it helps any, I've used Jython with Hibernate, SOAP, Corba, and EJBs and it is much easier than doing the same with just plain Java.

Use cases for Clojure in projects

Of course I'm not talking about using Clojure just because I can.
I'm curious about where/when to use Clojure and what is the value it brings to a project.
Suppose I'm doing Java web development, where I can use Clojure? any use cases are in your mind?
Thanks.
I'm a long time rubyist and I believe that I'm far more productive in a dynamic language that has good facilities for interactive development (i.e. a repl).
Clojure gives me this productivity benefit while offering much better performance than ruby, better concurrency features, and does not require crossing a bridge of any form to interact with java libraries. It can also be compiled into java classes so I can make libraries for java people without them needing to know or care that it's clojure and not java code they are running.

creating own scripting language in Java desktop app?

I have a simple web application testing desktop application. I am wondering how I can create my own scripting language for it (Domain Specific Language).
The purpose of this is to offer a very intuitive scripting even for non programmers, and able to define user scenario specific details.
I hope this is clear, basically the question is, how do I create a simple-to-use scripting language that can be translated into Java code.
Perhaps, they can use netbean's IDE to write the scripts.
For ex)
my script:
load "http://www.google.com";
click "Search" button;
java code:
browser.navigate("http://www.google.com";
browser.wait
browser.element("Search").click();
The best tool/framework for languages which extend beyond very simple grammars is to use ANTLR. Its a full featured lexer+parser combination, great Java integration, and tons of examples with good documentation.
Do realize that this isn't a trivial project to get right. You should use an existing language if your time is limited
The best solution would be to reuse one the existing scripting languages for Java; for example BeanShell. Implementing your own scripting language would be whole a lot of work. And if you've never done any language design and implementation work before the results are likely to be "less than stellar".
Perhaps, they can use netbean's IDE to write the scripts.
Expecting users of your application (especially non-programmers) to install and use a particular IDE sounds like a really bad idea.
... that can be translated into Java code.
Translating to Java code complicates things as well. It is probably better to interpret a scripting language and access Java APIs using a combination of staticly typed method calls and (only where necessary) the Java reflection APIs.

Scripting in Java [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Me and some friends are writing a MORPG in Java, and we would like to use a scripting language to, eg. to create quests.
We have non experience with scripting in Java. We have used Python, but we are very inexperienced with it. One of us also have used Javascript.
What scripting language should we use?
What scripting language should we not use?
I'm responsible for a fairly large hybrid Java/Jython system. We use java for core API development, then wire Java objects together using Jython. This is in a scientific computing environment where we need to be able to put together ad-hoc data analysis scripts quickly.
If I were starting this system from scratch today, I would not choose Jython as the scripting language. I like Python fine, but I frequently encounter awkward mismatches between the Python type system and the Java type system. For example, if you just want a hashtable, should you use a Python dictionary or a Java HashMap? The decision might be different depending on whether you are just using the data structure locally in Python code or passing it across the Java boundary. Jython does a certain amount of type coercion for you, but it's not perfect. It's annoying to even have to think about issues like this when the purpose of using a scripting language in the first place is to enhance your productivity.
I assume JavaScript or JRuby would have similar issues. Today I would choose a scripting language that is specifically targeted to the JVM and leverages the Java type system. The obvious candidates are Groovy and Beanshell; Groovy seems to have been picking up momentum lately so I'd look most closely at it.
I agree with Viktor's Jython suggestion. Other than that and JavaScript (which you've mentioned, and is built into Java 6+ via the javax.script package), Groovy and JRuby are also worth looking at too.
By the way, you should look at Wyvern, also an MMORPG written in Java and using Jython for scripting. Steve Yegge, its author, has much to say about it from time to time. :-)
Java supports a variety of (scripting) languages, some are listed in Wikipedia here and here. You probably should choose language with powerful DSL and metaprogramming capabilities, such as Clojure.
But if you need something simpler, JavaScript might be a viable alternative.
How about Jython?
http://www.jython.org/Project/
what about creating your own specialized scripting language? If your app is written with java, you can use ANTLR (http://www.antlr.org/) to create your language parsing code.
The reason I say this is because a general purpose scripting language may provide too much power (because the script it to be used for quests only i assume).
But if making your own language is too hard then any of the above suggestions works - you just have to figure out how to bind the game's runtime into the script. I also suggest Lua (http://www.lua.org/) as another choice that lots of games use.
Short version
Don’t use a scripting language! Instead focus on configurability (which is something that a non-programmer can do well).
Longer version
One oft-used argument in favour of having a scripting language is that it allows for lesser programmers to more trivial tasks. Don't belive this, it will not save you any time, since trivial tasks are already accomplished by real programmers in no time. Aim for configurability instead of scripting, and you will have a much lower risk of bleeding over complex algorithms and concepts into the incapable hands of game designers. :)
Lack of hotswapping (edit-and-continue) would have been a reason to implement a scripting language in an MMOG (you don’t want to reload the whole game for a minor code change), but using Java, with built-in hotswap, you really have no reason for adding a scripting language on top.
I have spent years pondering these questions; in the day I implemented a complete scripting language, IDE, VM, debugger, etc for an MMOG myself. Since, I have grown wiser.
If you still choose to go down the infinitely crappy path of no return, keep the following in mind.
Pick a mature language which has been around for a while.
Auto testing, debugging and editing will suck bigtime until you make your own tools/plugins/start hacking around in the VM.
To date, I have never seen a DSL that improved the situation (getting a more maintainable product). Myself, I integrated Python into my indie game engine, but eventually came to my senses and ripped it out. "Stackless Python" is just a way of saying "unmaintainable but fast". Please, anyone correct me if I'm wrong?
See Java: Scripting language (macro) to embed into a Java desktop application
You have quite a few options:
Groovy - http://groovy.codehaus.org/
Jython - http://www.jython.org/Project/
JRuby - http://jruby.codehaus.org/
Possibly even BeanShell (http://www.beanshell.org/)
I'm a fan of Python myself so I'd recommend Jython, but they're probably all reasonable options.
I would have to recommend Javascript for this purpose. Mozilla Rhino http://www.mozilla.org/rhino/ is an excellent implementation that would fit your needs perfectly.
I recommend Javascript over Jython or JRuby because of familiarity. Trivial Javascript follows a very familiar syntax that anybody can use. However if someone wants to do something more intense, Javascript is a very powerful functional programming language.
I regularly use Groovy and Ruby professionally and believe that their purpose is best for writing parts of applications with particularly complex logic where Java is cumbersome to write. Javascript is a much better choice as an embedded, general scripting language to use in a game. I haven't used Python, but it's syntactically similar to Ruby and I would believe it's purpose would also be similar.
LuaJ seems to be a nice way to embed Lua into Java:
http://sourceforge.net/projects/luaj/
I am a big fan of Python/Jython due to the clean syntax - which may suit you if you have some python experience.
Otherwise Groovy which is based on Java syntax and may be an easier learning curve if most of your developers are Java guys. It also has the advantage of closer ties with the Java language and libraries.
Beanshell is good if you have simple scripting in mind - it doesn't support classes. However I don't think it has had any support over the last few years (the JSR seemed to kill it off...) so is perhaps a bad choice if support is important to you.

Categories