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 :)
Related
I am using a java application with a web UI(jsp/js). I am trying to embed a perl editor into my UI and also need a few set of my own defined syntax apart from the perl default syntaxes to be interpreted and executed by the perl engine, which I would port with my app.
Is it possible to edit the default perl(or any scripting lang) intepreter to add a few of my custom syntaxes into it.
If the first is possible, how can i embed the intepreter into my java based UI, so that I can execute any perl script using my custom intepreter within my java container (Tomcat etc)
Yes, since version 5.14, Perl has offered an official API for defining new keywords in modules. See for example, Switch::Plain, Kavorka, or Moops for examples of what can be achieved with it. (Though actually even without delving into that, Perl's syntax is quite extendable just using exporters, prototypes, operator overloading, etc.)
There's a document called perlembed which deals with embedding Perl in a C. You can probably extend that idea to Java using JNI.
In scripting languages like Ruby/Python/Perl, we can start an interactive session and create new variables, and essentially execute whatever statements.
But in Java, I only know of a way to print arbitrary expressions - in Eclipse's debug-expression view.
There is no way to create new vars, and later utilize that var (though you can assign to pre-existing vars).
Is there any way to run Java in a interpreted IDE environment just like scripting languages?
Check out Groovy. If you want to stick to pure Java, I think Groovy will happily execute any valid Java code. You can poke at it via the GroovySH interactive shell.
Check out Beanshell It has been around a while. Also you can look at IDEOne website for an online IDE-like environment for several languages (not quite a shell). And yeah, I meant to mention Groovy too, which has the groovy shell.
As of Java 9 there will be a REPL available to allow you to start an interactive Java session.
It's called JShell, and you can read the original proposal: JEP 222: jshell: The Java Shell (Read-Eval-Print Loop)
There are a number of blogs and articles exploring some of the features of JShell, if you'd like to see it in action.
I'm looking for an interactive shell that I can bolt into a Java application for use in debugging and scripting. I'm not terribly interested in scripting in Java, so some more shell-like syntax would be fine, but I would like to be able to load and run 'external commands' on the fly (which would be Java .class files).
Unfortunately my platform is very limited in space --- double digits megabytes of heap space. As such languages like Groovy, Jython and JRuby are unsuitable.
I have found BeanShell, which looks okay, but it appears to have been dead for years and the syntax is rather cumbersome for command line use --- e.g. it would be nice to have stuff like 'help' typed in produce an error message, rather than silence.
Are there any other systems I should look at?
Edit: ...aaaaand I've just discovered that BeanShell2 requires reflection abilities that my platform doesn't have, and not even RetroWeaver can help me with this. I shall try BeanShell 1, but I'm not confident.
beanShell is just fine. It is done, not dead. And there seems to be a beanshell 2.0 branch.
You can take a look at Rhino.
Rhino is an open-source implementation of JavaScript written entirely
in Java. It is typically embedded into Java applications to provide
scripting to end users. It is embedded in J2SE 6 as the default Java
scripting engine.
Cliche can expose user-specified methods that can be called during runtime.
Does anybody know of an implementation of POSIX shell like language for scripting things in Java?
If this is not available, does anybody know if there is a ANTLR or JavaCC grammar available somewhere which I might have missed?
edit:
I know that I have Jython, JRuby, Groovy, JavaScript available for scripting, but none of them have bash like syntax.
This would not be used for scripting together Java code, but to allow people to run predefined commands which would manipulate with a big third party Media Asset Management system.
I would like to run things like:
ls | grep "something" > output
Where ls and grep would be Java commands. (This is just for illustrative purposes)
Thanks
All the POSIX shell implementations I know of are written in C, but I haven't particularly researched the question.
POSIX shell (I assume that's what you mean by bash-like) syntax is fairly complex. There is no clear separation between lexing and parsing. On the other hand the parsing hardly requires any backtracking. So a parser generator might not help so much.
EDIT since you've clarified you want shell syntax:
I think your best bet is to use an existing shell. Here are a few architecture considerations:
You can just link your application into an existing shell. Add built-ins that manipulate your asset management system. There may be licensing issues. This gives one application instance per shell instance.
You can use a simple client-server architecture, where the server is part of the application and just responds to simple commands with no control logic, and the client is linked into the shell and doesn't access application data directly. Several shells (bash, ksh, zsh) already have means for TCP access.
You might not need to reinvent a communication protocol; consider going over HTTP(S), for which server and client implementations are readily available. In fact you might even get away with having only scripts around wget or curl on the client side, so wouldn't need to patch the shell at all (this would make keeping complex state on the client side).
If you need to patch the shell (say, to add builtins), consider zsh, which has a module system. Your application (or the client part) would appear as a module that defines builtins and whatever else you need (for example the zsh distribution includes modules for things like ftp and mmap, ).
Groovy allows scripting - and the syntax is close to Java
I'm not exactly sure what you're looking for -- something like JShell? Something like Rhino (Javascript implementation in Java)? Something like Groovy (a dynamic language that produces code for the JVM)?
You could try the Groovy Shell http://groovy.codehaus.org/Groovy+Shell
Or beanshell http://www.beanshell.org/
Both are dynamic languages that run in the jvm, and have a syntax close to java while letting you call any existing java classes.
You can try using the Apache Commons CLI library that has implementations for parsing POSIX, GNU like command line arguments.
crashub bash
There is a pure Java bash interpreter implementation here:
https://github.com/crashub/bash
It doesn't currently implement a full bash based shell, but it might be a good start for the syntax parsing part of the problem.
Check out this duscussion: http://www.antlr.org/pipermail/antlr-interest/2006-May/016235.html
I guess you don't need a full grammar. Define a subset of a language, experiment a little bit. Maybe you don't even need antlr and you can write a parser by hand. Or use scala, you can turn it into any language you need.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Java Python Integration
I have a large existing codebase written in 100% Java, but I would like to use Python for some new sections of it. I need to do some text and language processing, and I'd much rather use Python and a library like NLTK to do this.
I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this?
If not, what would be the best method to interface between Java and Python, such that (ideally) I can call a method in Python and have the result returned to Java?
I'm aware of the Jython project, but
it looks like this represents a way to
use Java and its libraries from within
Python, rather than the other way
round - am I wrong about this?
Yes, you are wrong. You can either call a command line interpreter to run python code using Jyton or use python code from Java. In the past there was also a python-to-Java compiler, but it got discontinued with Jython 2.2
I would write a Python module to handle the text and language processing, and then build a small bridge in jython that your java program can interact with. The jython bridge will be a very simple one, that's really only responsible for forwarding calls to the python module, and return the answer from the python module to the java module. Jython is really easy to use, and setup shouldn't take you more than 15 minutes.
Best of luck!
I don't think you could use NLTK from Jython, since it depends on Numpy which isn't ported to the JVM. If you need NLTK or any other native CPython extension, you might consider using some IPC mechanism to communicate between CPython and the JVM. That being said, there is a project to allow calling CPython from Java, called Jepp:
http://jepp.sourceforge.net/
The reverse (calling Java code from CPython) is the goal of JPype and javaclass:
sourceforge.net/projects/jpype/
pypi.python.org/pypi/javaclass/0.1
I've never used any of these project, so I cant't vow for their quality.
Jython is a Python implementation running on the JVM. You can find information about embedding Python in an existing Java app in the user guide.
I don't know the environment that you're working in, but be aware that mixing languages in the same app can quickly lead to a mess. I recommend creating Java interfaces to represent the operations that you plan to use, along with separately-packaged implementation classes that wrap the Python code.
IN my opinion, Jython is exactly what you are looking at.
It is an implementation of Python within the JVM; as such, you can freely exchange objects and, for instance, inherit from a Java class (with some limitations).
Note that, its major strength point (being on top of of JVM) is also its major drawback, because it cannot use all (C)Python extension written in C (or in any other compiled language); this may have an impact on what you are willing to do with your text processing.
For more information about what is Jython, its potential and its limitations, I suggest you reading the Jython FAQ.
Simply run the Python interpreter as a subprocess from within Java.
Write your Python functionality as a proper script, which reads from stdin and writes to stdout.
Use the Java Runtime class to spawn a subprocess that runs your Python script. This is very simple to do and provides a very clean interface.
Edit
import simplejson
import sys
for request in sys.stdin.readlines():
args = simplejson.loads( request )
result = myFunction( args['this'], args['that'] )
sys.stdout.writeline( simplejson.dumps( result ) + "\n" )
The interface is simple, structured and very low overhead.
Remember to first check from those paying for the development that they're OK with the codebase needing a developer who knows both Python and Java from now on, and other cost and maintainability effects you've undoubtedly already accounted for.
See: http://www.acm.org/about/se-code 1.06, 2.03, 2.09, 4.03, 4.05, 6.07