Using Jbang to run Jython scripts with Maven dependencies

Werner Fouché <[email protected]> Sun, 9 Mar 2025 10:24:17 +0200
Newsgroups gmane.comp.lang.jython.user
Message-ID <CALMHTnxBCUmEqbSGFHJJRnurDKNFtx0qQBY=JU+j8sdE8=3o_g@mail.gmail.com>
--===============0467366296018240693==
Content-Type: multipart/alternative; boundary="00000000000083583b062fe495f5"

--00000000000083583b062fe495f5
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

 Jython scripts occasionally require access to JAR files from Maven
Central. Unlike Groovy, Jython lacks a built-in mechanism to resolve these
dependencies. However, one can bootstrap a Jython script along with its
Maven dependencies using JBang. This technique ensures that your Jython
scripts can seamlessly access the necessary dependencies from Maven
Central. See https://www.jbang.dev/ for information about JBang.

<Step 1> - create file *Jython.java* with JBang support and required Maven
dependencies.

----------
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.python:jython-standalone:2.7.4
//DEPS io.leego:banana:2.1.0
import org.python.util.jython;
public class Jython {
    public static void main(String... args) {
        jython.main(args);
    }
}
----------

<Step 2> - create a Jython script (*test.py*)

----------
from __future__ import print_function

import io.leego.banana.BananaUtils as BananaUtils
import io.leego.banana.Font as Font

text0 =3D "Jython 2.7"
text1 =3D BananaUtils.bananaify(text0, Font.STANDARD)

print(text1)
----------

<Step 3> -run the Jython script

$ jbang run Jython.java test.py

[jbang] Resolving dependencies...
[jbang]    org.python:jython-standalone:2.7.4
[jbang]    io.leego:banana:2.1.0
[jbang] Dependencies resolved
[jbang] Building jar for Jython.java...
Jython 2.7    # Displayed as banner text

Happy Jython scripting!
--=20
Werner Fouch=C3=A9

--00000000000083583b062fe495f5
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>
<span class=3D"gmail-blob-code-inner gmail-blob-code-marker">Jython scripts=
 occasionally require access to JAR files from Maven Central. Unlike Groovy=
, Jython lacks a built-in mechanism to resolve these dependencies. However,=
 one can bootstrap a Jython script along with its Maven dependencies using =
JBang. This technique ensures that your Jython scripts can seamlessly acces=
s the necessary dependencies from Maven Central. See </span><a href=3D"http=
s://www.jbang.dev/">https://www.jbang.dev/</a> for information about JBang.=
 <br></div><div><br></div><div>&lt;Step 1&gt; - create file <b>Jython.java<=
/b> with JBang support and required Maven dependencies.</div><div><br></div=
><div>----------</div><div>
///usr/bin/env jbang &quot;$0&quot; &quot;$@&quot; ; exit $?<br>//DEPS org.=
python:jython-standalone:2.7.4<br>//DEPS io.leego:banana:2.1.0<br>import or=
g.python.util.jython;<br>public class Jython {<br>=C2=A0 =C2=A0 public stat=
ic void main(String... args) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 jython.main(a=
rgs);<br>=C2=A0 =C2=A0 }<br>}</div><div>
---------- <br></div><div><br></div><div>&lt;Step 2&gt; - create a Jython s=
cript (<b>test.py</b>)</div><div><br></div><div>----------</div><div>from _=
_future__ import print_function<br></div><div><br></div><div>import io.leeg=
o.banana.BananaUtils as BananaUtils<br>import io.leego.banana.Font as Font<=
br></div><div><br></div><div>text0 =3D &quot;Jython 2.7&quot;<br>text1 =3D =
BananaUtils.bananaify(text0, Font.STANDARD)<br></div><div><br></div><div>pr=
int(text1)</div><div>----------</div><div><br></div><div>&lt;Step 3&gt; -ru=
n the Jython script</div><div><br></div><div>$ jbang run Jython.java test.p=
y</div><div><br></div><div>[jbang] Resolving dependencies...<br>[jbang] =C2=
=A0 =C2=A0org.python:jython-standalone:2.7.4<br>[jbang] =C2=A0 =C2=A0io.lee=
go:banana:2.1.0<br>[jbang] Dependencies resolved<br>[jbang] Building jar fo=
r Jython.java...</div><div></div><div>Jython 2.7=C2=A0=C2=A0=C2=A0 # Displa=
yed as banner text</div><div><br></div><div>Happy Jython scripting!</div><d=
iv><span class=3D"gmail_signature_prefix">-- </span><br><div dir=3D"ltr" cl=
ass=3D"gmail_signature" data-smartmail=3D"gmail_signature">Werner Fouch=C3=
=A9</div></div></div>

--00000000000083583b062fe495f5--


--===============0467366296018240693==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============0467366296018240693==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users

--===============0467366296018240693==--