First, you're right that Jython is Python running in the JVM. And it's not just running in the JVM, it can interact with it in pretty much all the ways you want—use Java classes as Python classes, implement Java interfaces in a Python class and pass it to Java code, etc.
However, unlike some of the other JVM languages, Jython doesn't make any attempt to be semantically equivalent to Java (or to a superset of it). And of course Python and Java have very different idiomatic styles.
So, in short, just about anything is possible, but not everything is pleasant, as Marcin says.
For specifics about how Spring works with Jython, a quick Google search turned up See how Spring Python works with Jython. And in fact, it's part of a blog called "Spring Python", which is part of a site also called "Spring Python". It seems like this may be a port of Spring to Python rather than about using Jython with Spring. ("This project takes the concepts of Spring and applies it to the language and environment of Python. This includes pragmatic libraries and useful abstractions that quickly gets you back to working on the code that makes you money.") So, that might be another alternative for you.
The next search result was Jython Spring MVC Controllers.
And there were half a dozen other promising results. So, I think you'll have no problem finding information and examples.
Answer from abarnert on Stack Overflowjava - Including Python Script in Spring Boot Application with Jython fails - module not found - Stack Overflow
jetty - Jython module not found when packaged with Spring Boot? - Stack Overflow
jython 2.7 - Where to put python module in spring boot application using Jython2.7? - Stack Overflow
Running Python Script with WebApp with Spring Boot Backend
Spring boot already has a mechanism for this: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html#howto-extract-specific-libraries-when-an-executable-jar-runs
84.6 Extract specific libraries when an executable jar runs Most nested libraries in an executable jar do not need to be unpacked in order to run, however, certain libraries can have problems. For example, JRuby includes its own nested jar support which assumes that the jruby-complete.jar is always directly available as a file in its own right.
To deal with any problematic libraries, you can flag that specific nested jars should be automatically unpacked to the ‘temp folder’ when the executable jar first runs.
For example, to indicate that JRuby should be flagged for unpack using the Maven Plugin you would add the following configuration:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <requiresUnpack> <dependency> <groupId>org.jruby</groupId> <artifactId>jruby-complete</artifactId> </dependency> </requiresUnpack> </configuration> </plugin> </plugins> </build>And to do that same with Gradle:
springBoot { requiresUnpack = ['org.jruby:jruby-complete'] }
In the case of jython you will use org.python:jython-standalone
I have had similar problem and this is how I have solved it in a not so elegant way. First of all I packaged jython distribution's python libraries ( Lib directory ) as a zip-file into my project into the resources.
In spring class I reference it:
private org.springframework.core.io.Resource jython = new ClassPathResource("lib.zip");
And then in the code write it out from the spring boot jar into a temp zipfile:
Path jythonTempFile = Files.createTempFile(null, "lib.zip");
jythonTempFile.toFile().deleteOnExit();
InputStream jythonInputStream = jython.getInputStream();
Files.copy(jythonInputStream, jythonTempFile, StandardCopyOption.REPLACE_EXISTING);
String jythonPath = jythonTempFile.toFile().getAbsolutePath();
Then path to that tempfile is used when setting up the Python interpreter:
PythonInterpreter interp = new PythonInterpreter();
interp.exec("sys.path.append('" + jythonPath + "')");
This way you can also add other classes and python libraries into your Jython interpreter.
Why is this not elegant? Now those libraries are twice inside the spring boot application, once in the jython.jar and once as a zip.
I did not have time to figure out way to get a reference into the jython libraries inside boot in bullet proof way otherwise.
Hello everyone.. i want to ask is it possible to add a python script like this?
I have a functional spring boot + react app for users and they already using it. But they have a new change request to add a button to download the file as an excel with templates and data was fetch from the db. My friend help me by building a python script to generate this file (since we think python is good for working with data)
Is it possible to add a button in my react front end to run the python script and download the data as excel?
