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.
Related
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 :)
I have a tool backed by Velocity templates which statically creates a file (Ruby like syntax) after capturing inputs from Web App for use with some other tool (Vagrant - built in Ruby). However, now I wish to support the reverse of what is being done till now so that I can browse a previously created Vagrant file in my tool and have the settings defined there-in populate my Java Beans and end up at the Web App's UI.
Is there any way to interpret the Ruby Syntax like:
config.vm.define :firstMachine do |master|
master.vm.hostname = "boxupp.test.first"
master.vm.box = "Ubuntu"
master.vm.box_url = "http://www.google.com"
master.vm.provider "virtualbox" do |prov|
prov.customize ["modifyvm", :id, "--memory", "384"]
end
end
and populate the corresponding values in the Java Beans.
I stumbled across JRuby but it looks like another Scripting platform.
Thanks for your time !!
If you just want to read / parse the syntax, then you would need to write (or find) a Ruby parser written in Java.
If you mean interpret ... as in execute / run ... Ruby code in Java, then you either need:
a Ruby interpreter that you can call from Java; see https://github.com/jruby/jruby/wiki/JRubyAndJavaCodeExamples
or a way to turn Ruby code into a real Java method; see https://github.com/jruby/jruby/wiki/GeneratingJavaClasses
I stumbled across JRuby but it looks like another Scripting platform.
Ermm ... it is an implementation of the Ruby language.
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
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 :)