I have a need for providing code snippets at runtime.
For this to work well, I basically need to call into the scripting language, and back into Java. For this to be usable in a debug scenario StackTraces must be usable too (so methods and linenumbers go directly to the script source like in modern JSP-pages) and Exceptions must bubble up correctly.
What scripting languages - where the source is read at runtime - can provide this? JSR-223 support is a bonus.
I think JRuby is best choice because:
Its performance as a scripting language is good.
It can execute in two mode (Compiled, Interpreted).
It supports Ruby on Rails and you can use JRuby for Rails applications in production environments.
You can invoke the classes of the Java Platform easily without any restriction.
I think Groovy would be the perfect fit in your case given it's super similarity and ease of integration with Java not to mention that it's pretty much the most mature JVM language out there with excellent support from the likes of SpringSource.
As an example of JSR support, Groovy has it.
Related
I have started porting my server side C# app to Java. One of the things it does is dynamic generation of code using the CodeDOM framework, compiling it to bytecode, and loading the class during runtime. There are also scenarios where we have used Expression Tree for lightweight dynamic generation of methods.
I did some research and I believe the closest I came is Javaassist. I would appreciate if there are any other frameworks that supports dynamic code generation and compilation. I am a fresh recruit in the Java world.
There's the Java Compilation API (for example, http://www.accordess.com/wpblog/an-overview-of-java-compilation-api-jsr-199/).
If you just need to evaluate expressions, consider any EL, like OGNL or MVEL.
Groovy, JRuby, and Scala are the most mainstream dynamic languages that allow a huge range of options, from simple expression evaluation to full-blown Java interaction.
To modify classes, you can use javaassit, asm, cglib(actually, it depends on asm), bcel, and etc.
Among them, I recommend asm, because of its better lightweight and better performance.
Using asm, you can generate some classes dynamically. But if you want to modify the classes, which are running in the jvm, you have to use Java Instrument API to retransform the existed classes.
You can see this paper for more info. http://www.cs.helsinki.fi/u/pohjalai/k05/okk/seminar/Aarniala-instrumenting.pdf
More doc about Instrument from oracle.
http://docs.oracle.com/javase/6/docs/technotes/guides/instrumentation/index.html
I have successfully used Janino for dynamic code generation. Like Javassist, it can compile Java source code in-memory. Also like Javassist, it lacks Java 1.5 language features, but apart from that it is easy to use.
The SimpleCompiler class is a good starting point.
I would look at lisp (or other dynamic languages) that have been targeted to jvm.
wiki article
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.
I have some batch data-manipulation scripts which support a small business website.
The scripts are a "rat's nest" of Perl, Java, and Stored Procedures, which run on a scheduled basis to update data based on various sources and algorithms.
I want to get rid of the Perl so that I can at least take advantage of transaction management by containing the entire process in a JVM-managed database connection from start to finish.
Which of the various Java dynamic/scripting language should I leverage which will meet the following criteria:
Straightfoward to migrate Perl code by providing similar expressive power, I/O, regex's, etc.
Good IDE support, including code completion and debugging preferably in Eclipse
Easy to install. Preferably the entire scripting engine should be in a single jar file.
Not an orphaned technology. I want to pick something that will still be around in 5 years.
Of course - clean integration with the rest of the Java code that I already have.
I hope my criteria are clear enough that this does not get tagged as a subjective question.
For me this is definitely a programming question. I see all those languages as just "useful java libraries".
Thanks!
To be fair, Jython, JRuby, Groovy would all be good choices. They all have the decent IDE support, and the syntax is as expressive, and more succinct than Perl. (Python and Ruby both owe a debt to Perl in their conception, so porting from Perl isn't too much of a headache)
Of course, Beanshell and Javascript (in the form of Rhino) will be adequate too, although I'd say that both their syntaxes are less expressive.
Judging on current usage trends, I'd say Jython and then JRuby would probably be the two with the most general support / longevity, in relation to the others.
Really the choice comes down to your level with each of these languages, but of all of them I'd suggest you use Jython, but if your more comfortable with Ruby, JRuby.
I vote Jython since it can interact with existing Java code and it's got a strong support base. Not mention its Python which is easy to learn and use. Eclipse has pretty good support for syntax highlighting, debugging, and auto-complete. Finally the install is super easy since it's a stand alone folder (no real "install").
I'll admit bias since I have used the other tools you mentioned much less than I have Jython but I have not needed to since Jython has fit the needs so well.
I should say that that several benchmarks indicate that Groovy is wins in speed, compared to Jython and BeanShell. The test was performed using DMelt (http://jwork.org/dmelt) framework where you can jump from one language to another, calling same Java libraries.
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)
I have an open source Java database migration tool (http://www.liquibase.org) which I am considering porting to .Net.
The majority of the tool (at least from a complexity side) is around logic like "if you are adding a primary key and the database is Oracle use this SQL. If database is MySQL use this SQL. If the primary key is named and the database is Postgres use this SQL".
I could fork the Java codebase and covert it (manually and/or automatically), but as updates and bug fixes to the above logic come in I do not want to have to apply it to both versions. What I would like to do is move all that logic into a form that can be compiled and used by both Java and .Net versions naively.
The code I am looking to convert does not contain any advanced library usage (JDBC, System.out, etc) that would vary significantly from Java to .Net, so I don't think that will be an issue (at worst it can be designed around).
So what I am looking for is:
A language in which I can code common parts of my app in and compile it into classes usable by the "standard" languages on the target platform
Does not add any runtime requirements to the system
Nothing so strange that it scares away potential contributors
I know Python and Ruby both have implementations on for the JVM and CLR. How well do they fit my requirements? Has anyone been successful (or unsuccesful) using this technique for cross-platform applications? Are there any gotcha's I need to worry about?
Check out the Fantom programming language. It has its own Java-like/C#-like syntax but can target either the Java VM or .NET CLR.
Their "Why Fantom" page gives a high-level overview of their approach to portability versus dynamic languages running on a VM.
You might have some luck using IKVM.NET. I'm not sure on its exact status, but it's worth a try if you're insistent on running Java code on the .NET Framework. It includes a .NET implementation of the Java base class library, so it seems reasonably complete.
The only other option I might suggest is porting the code to the J# language, a full .NET language (although not first class in the sense that C# or VB.NET is). The language was designed so that the differences with Java were minimal.
If you are thinking about an emdedded approach, you might look at Lua.