What is Jython and is it useful at all? [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I know Python, but what is Jython?
When will I need Jython?
What are the drawbacks?
I assume it is slow?
Please detail it out! thanks.

Quoting Wikipedia:
Jython, successor of JPython, is an implementation of the Python programming language written in Java.
As for what uses it may have:
Jython programs can seamlessly import and use any Java class. Except for some standard modules, Jython programs use Java classes instead of Python modules.
Performance measurements of Jython, IronPython and CPython show that both Jython and IronPython outperform CPython for some cases with large datasets.

If you know Python and has bought into the "pythonic" way of doing things, then Jython allows you to bring that philosophy to the JVM stack. If you do this, it is much more than just adding scripting capability.
In our latest projects, all the custom and business logic is built in Jython, at the same time we can still leverage some of the great tried and tested Java libraries like Solr, Jasperreports, Quartz, Jetty, Velocity to name a few.
It does get compiled to bytecode, however, an extra layer is being added, but is no different to using an ORM instead of straight JDBC for example.
What you gain in productivity far out weighs the minuscule lost in performance.
On the server side, Jython is rarely the bottleneck. For mini desktop apps, there may be issues, but very much dependent on what you are trying to do.
The latest JDK, together with containers like Jetty or Tomcat are very mature and stable, adding Python on top, in many cases, gives the best of both worlds.

When will I need Jython?
When you want to program in Python but need (or want) to have the result run on a Java virtual machine, or use existing Java components.
What are the drawbacks.
Jython may not be 100% compatible with Python, though any incompatibility would be considered a bug. If you later want/need to run on CPython, any code that uses Java components will have to be rewritten.
I assume it is slow?
That depends, as always, on your specific usecase. It may actually be faster than CPython in some cases; and of course it depends on the specific JVM you run under - these get better all the time.

Two other reasons:
Embedding scripting into large Java application.
Use Java threads to write multi-threaded programs in Jython.

Jython is a Python implementation which runs on top of JVM.
Theoretically, Jython is a bit slower, then CPython.
You can find some benchmarks here:
http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/

When will I need Jython?
For example to add a nice scripting language to your code.
What are the drawbacks?
The main drawback is that Jython lags behind the official CPython distribution. Currently, you can get a version of Jython that is compatible with Python 2.5.2 while CPython is at 3.1.
Also some esoteric modules aren't supported. Usually, you won't notice and/or be able to easily find a Java replacement.
I assume it is slow?
Compared to what? Usually, it's either fast enough or, when it isn't, you can replace a few lines of Python with about 1'000 lines of much faster Java.

When will I need Jython?
You can write administrative scripts for Websphere application server using Jython. The scripts are used to automate common administrative tasks and configure security in Websphere application server.
WebSphere utilizes the JMX API to create MBeans that are exposed as an API of internal WebSphere objects. As the Jython scripts are based on Java and they run on the JVM they can access those objects and can be utilized to configure WebSphere.
Besides it is fun to write code in Jython and you learn a new language.
Here is the details

When will I need Jython?
I need Jython to test JDBC drivers. Some of apps I use work use ODBC, some use JDBC. Using Jython I can test both type of drivers from one Python source
(to test ODBC I use JDBC-ODBC bridge).

The JVM does some nice dynamic optimization, but it's probably nothing to get excited about. JRuby sometimes runs faster on the JVM (http://www.engineyard.com/blog/2009/j-is-for-jvm-why-the-j-in-jruby/) by optimizing the code paths that run a lot, but that's Ruby, not Python.
Java libraries are the main reason. Lots of companies have big globs of Java code that can be scripted with Python. There's also a few open source frameworks (Struts and Eclipse spring to mind) which you can script with Jython.

To achieve a good speed performance or implementing a real multithreading program, calling python script directly from java (native) is the best way. Just prepare your python script then let java do the rest for concurrent invocation into your python script.

Related

How to make a Java program to generate another Java application at run time

Can I make a Java program to generate another java application at runtime.
I want to make a "installer" program, which takes user input and generates an application as per user requirement, instead of just configuring the pre-built application according to the user needs.
I came across this solution - how to compile & run java program in another java program?, but I don't want to make clients install JDK on there computer.
Dynamically create table and Java classes at runtime -
which also need JDK, but I got a work around:
ToolProvider.getSystemJavaCompiler() returns null - usable with only JRE installed?
Can I make a complete application using above methods?
Is it a bad idea to generate such program?
Can I make Spring and Hibernate applications like that?
Or is there any existing framework for doing so?
(if possible it should create tables in db and generate html files as well. I came across http://velocity.apache.org/, so is it possible to generate java code using that.)
Your goal doesn't make a lot of sense from a practical perspective. I hope that my answer will help you to understand why.
Can I make a java program to generate another java application at runtime.
Yes you can. But it is a lot of work, especially if the application if complicated.
I want to make a "installer" program, which takes user input and generate an application as per user requirement, instead of just configuring the pre-build application according to the user needs.
That is possible ... in theory.
The problem is that you have to write a program that is capable of reading and understanding the user's requirements, and can then converting those requirements into code. Normally ... this is what a programmer does. Writing a program to do what a programmer does is not practical. (My guess is that it is 20 or more years beyond the "state of the art" of artificial intelligence to do such a thing.)
Now if the problem domain was sufficiently restricted, and the requirements were tightly specified in an unambiguous notation, then it might be feasible to do this. However, benefits of generating a program rather than configuring an existing one (based on the same requirement notation) are pretty small. And probably not worth the effort.
... but I don't want to make clients install JDK on their computer.
If you are generating Java programs you need a Java compiler. So if you insist on using a JRE (in Java 8), you need to include a 3rd party Java compiler in your application.
However, for Java 9 onward this is moot:
Oracle no longer provides JRE distributions for Java 9+ so you would need to get your client to use a 3rd-party source for their JRE.
You could (should) be using the Java 9+ jlink utility to produce a custom JRE for you application, and that can include the standard Java compiler.
If you are trying to generate code at the bytecode level, your problem is immediately ten times harder.
Sorry, I am using Java 8
Are you aware that Java 8 is "end of life" for commercial use? That is likely to affect your clients.
Can I make a complete application using above methods?
Maybe yes, maybe no. It depends on the problem domain. The more complicated it is, and the more diverse / general the requirements, the harder it will be.
Is it a bad idea to generate such program?
Yes. It is a bad idea. It is a lot more work than writing an application that is configured in the conventional way. (Noting that the configuration could include writing plugins in Java, rules in some scripting language, and so on.)
I would advise only generating source code or bytecodes if you already have a conventional application with most / all of the required functionality that you can use as a prototype for the generated generated code. (If you can't write such a prototype by hand, then writing a generator that will create one is not realistic.)
And even when it is feasible, I would question the wisdom of building a generator. There doesn't seem to be a significant pay-off for the extra effort. (For example, where is the benefit for the end user?)
Can I make spring and hibernate application like that?
I don't see why you couldn't generate such an application. But see 1) and 2).
Or is there any existing frameworks for doing so?
There are frameworks that could be used in some cases:
Templating frameworks like Velocity1 can be used to generate Java source code.
Bytecode engineering frameworks could be used to generate code directly.
1 - Indeed, I have used Velocity for Java source code generation. It worked, though I'm not convinced it was an ideal solution.
Sure you can. You can also leverage a project like GraalVM to generate native binaries for a given platform.
However, it is a lot of work, and the end result won't probably be as useful as you think. Any use case you have in mind will probably be a lot better served by an app that you just configure to do different tasks, so your efforts are probably best spent in that direction.

Importing Java class into a python project

I've been trying to find a method to importing Java-ml into my python project. I have the jar file in the same path as my project.
I want to use it for kmeans clustering, since it allows me to change the distance metric. I am wondering though whether with the implementation that one of you suggest, whether I'll be able to pass a different java class as a parameter for the function?
I tried using:
import sys
sys.path.append(r"C:\Users\X\Desktop\X\javaml-0.1.7\javaml-0.1.7.jar")
import net.sf.javaml as jml
test = jml.clustering.Kmeans()
I considered using jython, however I am unsure of how it works, and it is unclear whether I could continue using idle and whether I would have to reprogram my project.
Lastly I considered using PyJNIus, however it is simply not working.
In short, you can't run Java code natively in a CPython interpreter.
Firstly, Python is just the name of the specification for the language. If you are using the Python supplied by your operating system (or downloaded from the official Python website), then you are using CPython. CPython does not have the ability to interpret Java code.
However, as you mentioned, there is an implementation of Python for the JVM called Jython. Jython is an implementation of Python that operates on the JVM and therefore can interact with Java modules. However, very few people work with Jython and therefore you will be a bit on your own about making everything work properly. You would not need to re-write your vanilla Python code (since Jython can interpret Python 2.x) but not all libraries (such as numpy) will be supported.
Finally, I think you need to better understand the K-Means algorithm, as the algorithm is implicitly defined in terms of the Euclidean distance. Using any other distance metric would no longer be considered K-Means and may affect the convergence of the algorithm. See here for more information.
Again, you can't run Java code natively in a CPython interpreter. Of course there are various third party libraries that will handle marshalling of data between Java and Python. However, I stand by my statement that for this particular use case you are likely better to use a native Python library (something like K-Medoid in Scikit-Learn). Attempting to call through to Java, with all the associated overhead, is overkill for this problem, in my opinion.
To "answer" your question directly, Jython will be your best bet if you simply want to import Java classes. Jython strives very hard to be as compatible with Python 2.x as possible and does a good job. So you won't have to spend too much time rewriting code. Just simply run it with Jython and see what happens, then modify what breaks.
Now for the Python answer :D. You may want to use scikit for a native implementation. It will certainly be faster than running anything in Jython.
Update
I think the Py4J module is what you're looking. It works by running a server in your Java code and the Python code will communicate with the Java server. The only good thing about "Py4J" is that it provides the boiler plate code for you. You can very easily setup your own client/server with no extra modules. However I still don't think it's a superior option compared to Pythons native modules.
References
How to import Java class w/ Jython
Scikit - K-Means

How to write Python middleware for Java/Scala backend? How to connect Java and Python?

Let's say I've got a Java/Scala backend and I want to develop middleware in Python for it. I have no experience with developing middleware, but I presume it means that from within Python I would need to issue commands to the running Java/Scala backend programs, and in turn receive feedback from that backend. For this to happen I need to interface the two languages and there are several options for interfacing Python and Java. According to these pages there are some options:
JPI
JNI
JPype
Jepp
Jython
JCC
Unfortunately, JPI, JNI, JPype and Jepp do not seem to be actively developed anymore (last update more than a year ago). Jython is still active, but I read that it is not possible to import all Python modules from within Jython. JCC seems to be some kind of C++ layer between Java and Python, which also seems counter intuitive.
I've also found some questions on SO about the topic, but most seem more than 3 years old, which in internet/Python land is of course a long long time.
Since I've got some experience with the ZeroMQ messaging library I could use that, but I'd have to adjust the Java programs for it (which wouldn't be that big of a trouble, but if there's a better solution..).
So my question is: taking into account that I have control over a Java/Scala backend, what is currently the best way to write a Python middleware layer for that backend?
You haven't given a lot of details about what you're trying to do. If your middleware runs in a separate process then it probably won't make a lot of difference what language your middleware is written in as long as they can talk to each other—and you can do that with ZMQ as you suggested.
If you're actually just making calls into Java/Scala libraries from a layer you're wanting to write in Python, I'd go with Jython. "Not being able to load all the modules" is only an issue if you were planning on using some of the modules that aren't available. Even for those modules that aren't available, you should be able to find suitable Java substitutes you can call.
Finally, if you really want to directly interface between Python and Java, you could look into Babel. I know some people who have used it for interfacing C, Java and Python programs successfully, and it seemed to work quite well for them.

Should I use Jython, JRuby, Beanshell, Groovy, Rhino, or what for this small task?

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.

Using Python from within Java [duplicate]

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

Categories

Resources