Skip to content

Use JBang to run Jython scripts (and resolve Maven dependencies) #371

Open

Description

@wfouche

See jbangdev/jbang#1926.

How can this information be shared with the Jython community?

Documentaiton updates, or web site updates, etc.?

Activity

  1. Stewori commented on Feb 12, 2025

    @Stewori
    Member

    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.

  2. wfouche commented on Mar 9, 2025

    @wfouche
    MemberAuthor

    An email was sent to jython-users mailing list to make users aware of the technique to use to run Jython scripts with JBang.

  3. Stewori commented on Mar 10, 2025

    @Stewori
    Member

    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.

  4. wfouche commented on Apr 11, 2025

    @wfouche
    MemberAuthor

    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.

    See the following Medium article - https://medium.com/@werner.fouche/running-jython-scripts-with-jbang-using-a-java-helper-program-9ab9f8e35ddc

  5. Stewori commented on Apr 12, 2025

    @Stewori
    Member

    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?

  6. wfouche commented on Apr 12, 2025

    @wfouche
    MemberAuthor

    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!

    You're welcome to repost / share the article.

    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?

    Fair point. I've updated the article and rephrased that aspect of the text. Thanks.

  7. Stewori commented on Apr 13, 2025

    @Stewori
    Member

    Thank you for the edit! Appreciated.

    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.

  8. wfouche commented on Apr 14, 2025

    @wfouche
    MemberAuthor

    This still makes me wonder... Do other VM-based languages have something like this built-in?

    I know Groovy has Grape to resolve external MAven depedencies.

    and Scala via Scala-CLI can also resolve Maven dependencies.

  9. wfouche commented on Apr 14, 2025

    @wfouche
    MemberAuthor

    @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.

  10. Stewori commented on Apr 15, 2025

    @Stewori
    Member

    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.

  11. jeff5 commented on May 6, 2025

    @jeff5
    Member

    @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 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-cli on 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-catalogs

  12. jeff5 commented on May 6, 2025

    @jeff5
    Member

    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:

    PS jbtest> jbang jython-cli -m test.regrtest -e -u network
    == 2.7.4 (tags/v2.7.4:3f256f4a7, Aug 18 2024, 10:30:53)
    == [OpenJDK 64-Bit Server VM (Eclipse Adoptium)]
    == platform: java21.0.7
    == encodings: stdin=cp850, stdout=cp850, FS=utf-8
    == locale: default=('en_GB', 'UTF-8'), actual=(None, None)
    ...
    34 fails unexpected: ...
    

    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.)

  13. wfouche commented on May 7, 2025

    @wfouche
    MemberAuthor

    I notice that it has not picked up the correct console encoding, but otherwise it is better than I expected.

    The console encoding can be modified either from JBang or from Jython:

    • jbang --java-options=-Dpython.console.encoding=UTF-8 jython-cli ...
    • jbang jython-cli -Dpython.console.encoding=UTF-8 ...

    I've updated jython-cli.java to set the console encoding to UTF-8.

  14. wfouche commented on May 7, 2025

    @wfouche
    MemberAuthor

    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.
    >>> 
  15. jeff5 commented on May 14, 2025

    @jeff5
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions