You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use JBang to run Jython scripts (and resolve Maven dependencies) #371
The best way to reach Jython users is the mailing list jython-users. You can use that channel on your behalf. We could add the info to the user guide, but that doc has its own issue #338 and is not nearly as visible as the mailing list. IIRC we avoid having documentation on the website (for the maintenance burden). Apart from the two very basic examples on the front page, documentation consists of a link collection. Then, there is the wiki, but I doubt that it has much traffic (but can't know for sure). I'd go for the mailing list for now and maybe post a hint in #338 as a reminder to consider adding it to the user guide.
If this post is available somewhere as a link, like a blog post, we can make a "tweet" on X and bluesky. We could also link to the email in the mailing list archive if there is no preferable alternative.
If this post is available somewhere as a link, like a blog post, we can make a "tweet" on X and bluesky. We could also link to the email in the mailing list archive if there is no preferable alternative.
Thank you for writing this article! If you don't mind, I think it would be nice for Jython to repost it on social media where applicable!
That said, I'd like to point out one minor aspect I find a bit misleading. Framing it a shortcoming of Jython to lack the ability to pull dependencies from maven central, one could equally well call it a shortcoming of Java itself. After all, Java relies on the maven tool (or gradle FWIW), i.e. an external tool, for this. One is free to use maven to resolve dependencies of a Jython project (and pip for the Python deps btw) like for every other Java project. I understand the purpose for story telling and don't want to complain about this rare opportunity of sharing a Jython article these days. Just pointing it out. In a way, isn't it fair to say that jBang solves this problem for Java, rather than for Jython specifically?
The Java VM lacks a built-in mechanism for retrieving these dependencies
This still makes me wonder... Do other VM-based languages have something like this built-in? Python doesn't - pip is an external tool like maven. I don't know how Groovy, Ruby, Scala and Kotlin handle it. Not suggesting another edit! I'm just surprised that this functionality is now expected from a VM, or at least that its absence is considered a lack. Dependency management is a non-trivial can of worms and imposes another burden for VMs, which are complex pieces of technology already.
@Stewori , I've written Part 2 of the article series on using JBang with Jython. The article shows how the PEP 723 data specification is used to define the dependencies to be resolved, and it also includes an example using Spring RestClient to call a REST API. Jython scripts can now be made runnable and can be executed just like a normal shell as as ./.py
Great write-up. I did not verify its technical correctness though, but I trust you on this matter. Simple yet impressive examples, I like it. I suspect things wouldn't go so smooth for Python-based libs. Scripting the Java maven ecosystem easily in Python syntax may be indeed an underestimated usecase for Jython. Compatibility is straight forward for Java libs and one doesn't run into all the hassles of making all sorts of Python libs work. I once scripted a minimalistic Javafx app in Jython, roughly a simplified paint program. Maybe I could convert that into a follow-up example of your articles and integrate the JBang meta data as you demonstrate.
Re social media, I suggest to wait a couple of days before sharing the new article. To give the recent posts some more time to sink in.
@wfouche That was a very helpful article, thanks. I worked through some of the examples and played with a simple script launched under the Jython.java class you give.
I notice that it has not picked up the correct console encoding, but otherwise it is better than I expected.
This is is actually one more test failure (test_float_jy) than I get normally, but I attribute it tentatively to a refinement in the formatting of floating point numbers. (It has been slightly wrong in Java since the beginning, and has maybe been fixed or changed in Java 21.)
This is is actually one more test failure (test_float_jy) than I get normally
@jeff5 , Jython-cli can be run with Java 8 as follows:
$ jbang --java 8 jython-cli
Jython 2.7.4 (tags/v2.7.4:3f256f4a7, Aug 18 2024, 16:49:39)
[OpenJDK 64-Bit Server VM (Temurin)] on java1.8.0_452
Type "help", "copyright", "credits" or "license"for more information.
>>>
It was more an observation than a thing to fix. This is observable in Jython on Java 21, so not a facet of jython-cli. (17 is ok.) It may be the test that is the problem. The ability to choose a version is important.
Activity
Stewori commentedon Feb 12, 2025
The best way to reach Jython users is the mailing list jython-users. You can use that channel on your behalf. We could add the info to the user guide, but that doc has its own issue #338 and is not nearly as visible as the mailing list. IIRC we avoid having documentation on the website (for the maintenance burden). Apart from the two very basic examples on the front page, documentation consists of a link collection. Then, there is the wiki, but I doubt that it has much traffic (but can't know for sure). I'd go for the mailing list for now and maybe post a hint in #338 as a reminder to consider adding it to the user guide.
wfouche commentedon Mar 9, 2025
An email was sent to jython-users mailing list to make users aware of the technique to use to run Jython scripts with JBang.
Stewori commentedon Mar 10, 2025
If this post is available somewhere as a link, like a blog post, we can make a "tweet" on X and bluesky. We could also link to the email in the mailing list archive if there is no preferable alternative.
wfouche commentedon Apr 11, 2025
See the following Medium article - https://medium.com/@werner.fouche/running-jython-scripts-with-jbang-using-a-java-helper-program-9ab9f8e35ddc
Stewori commentedon Apr 12, 2025
Thank you for writing this article! If you don't mind, I think it would be nice for Jython to repost it on social media where applicable!
That said, I'd like to point out one minor aspect I find a bit misleading. Framing it a shortcoming of Jython to lack the ability to pull dependencies from maven central, one could equally well call it a shortcoming of Java itself. After all, Java relies on the maven tool (or gradle FWIW), i.e. an external tool, for this. One is free to use maven to resolve dependencies of a Jython project (and pip for the Python deps btw) like for every other Java project. I understand the purpose for story telling and don't want to complain about this rare opportunity of sharing a Jython article these days. Just pointing it out. In a way, isn't it fair to say that jBang solves this problem for Java, rather than for Jython specifically?
wfouche commentedon Apr 12, 2025
You're welcome to repost / share the article.
Fair point. I've updated the article and rephrased that aspect of the text. Thanks.
Stewori commentedon Apr 13, 2025
Thank you for the edit! Appreciated.
This still makes me wonder... Do other VM-based languages have something like this built-in? Python doesn't - pip is an external tool like maven. I don't know how Groovy, Ruby, Scala and Kotlin handle it. Not suggesting another edit! I'm just surprised that this functionality is now expected from a VM, or at least that its absence is considered a lack. Dependency management is a non-trivial can of worms and imposes another burden for VMs, which are complex pieces of technology already.
wfouche commentedon Apr 14, 2025
I know Groovy has Grape to resolve external MAven depedencies.
and Scala via Scala-CLI can also resolve Maven dependencies.
wfouche commentedon Apr 14, 2025
@Stewori , I've written Part 2 of the article series on using JBang with Jython. The article shows how the PEP 723 data specification is used to define the dependencies to be resolved, and it also includes an example using Spring RestClient to call a REST API. Jython scripts can now be made runnable and can be executed just like a normal shell as as ./.py
https://medium.com/@werner.fouche/running-jython-scripts-with-jbang-part-2-d13b3699c015
Would appreciate you feedback / comments on this article.
Stewori commentedon Apr 15, 2025
Great write-up. I did not verify its technical correctness though, but I trust you on this matter. Simple yet impressive examples, I like it. I suspect things wouldn't go so smooth for Python-based libs. Scripting the Java maven ecosystem easily in Python syntax may be indeed an underestimated usecase for Jython. Compatibility is straight forward for Java libs and one doesn't run into all the hassles of making all sorts of Python libs work. I once scripted a minimalistic Javafx app in Jython, roughly a simplified paint program. Maybe I could convert that into a follow-up example of your articles and integrate the JBang meta data as you demonstrate.
Re social media, I suggest to wait a couple of days before sharing the new article. To give the recent posts some more time to sink in.
jeff5 commentedon May 6, 2025
@wfouche That was a very helpful article, thanks. I worked through some of the examples and played with a simple script launched under the
Jython.javaclass you give.I think because that is so simple and works well (gives me access to all the Jython command line options) I don't yet see why the
jython-clion the repo is relatively complex. I plan to test locally with one of the options here: https://www.jbang.dev/documentation/guide/latest/alias_catalogs.html#local-alias-catalogsjeff5 commentedon May 6, 2025
When I have a copy of the catalog and CLI script locally,
With a copy of the tests locally too, I get almost the result I expect from standalone:
I notice that it has not picked up the correct console encoding, but otherwise it is better than I expected.
This is is actually one more test failure (
test_float_jy) than I get normally, but I attribute it tentatively to a refinement in the formatting of floating point numbers. (It has been slightly wrong in Java since the beginning, and has maybe been fixed or changed in Java 21.)wfouche commentedon May 7, 2025
The console encoding can be modified either from JBang or from Jython:
I've updated jython-cli.java to set the console encoding to UTF-8.
wfouche commentedon May 7, 2025
@jeff5 , Jython-cli can be run with Java 8 as follows:
jeff5 commentedon May 14, 2025
It was more an observation than a thing to fix. This is observable in Jython on Java 21, so not a facet of
jython-cli. (17 is ok.) It may be the test that is the problem. The ability to choose a version is important.