# Add .JAR for using classes in Jython scripts

**URL:** <https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529>\
**Category:** 3rd Party Modules\
**Created:** [January 9, 2017, 10:37am UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529 "2017-01-09T10:37:10Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 2, 2019, 2:43pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/21 "2019-05-02T14:43:40Z")

</div>

His office module uses poi version 3.14.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 2, 2019, 2:43pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/22 "2019-05-02T14:43:59Z")

</div>

> [@code\_skin](#):
>
> How come `import org.apache.poi` fails to import in the designer if the reporting modules adds it to the D scope?

I don't know. What exactly are you trying to import and where?

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [May 2, 2019, 2:51pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/23 "2019-05-02T14:51:50Z")

</div>

From the designer’s Script Console:  
`from org.apache.poi.xssf.usermodel import XSSFWorkbook`  
returns  
`ImportError: No module named poi`

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 2, 2019, 2:54pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/24 "2019-05-02T14:54:44Z")

</div>

Do you have the Reporting module installed?

Also: XSSFWorkbook is not part of poi-3.14 jar

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [May 2, 2019, 2:57pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/25 "2019-05-02T14:57:24Z")

</div>

Yes, the reporting module is installed.

If this is not apart of 3.14 then the Perfect Abstraction module must be using a different version.  
The link above from @bfuson has sample code that uses XSSFWorkbook.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 2, 2019, 2:59pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/26 "2019-05-02T14:59:15Z")

</div>

It’s not using a different version, you can unzip it and look yourself. It does include _additional_ poi jars, though…

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [May 2, 2019, 3:05pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/27 "2019-05-02T15:05:41Z")

</div>

Thanks for your patience Kevin. I’m following now.

---

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [September 2, 2022, 9:15pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/28 "2022-09-02T21:15:01Z")

</div>

Is this still correct today? You cannot make a java library available in the gateway scope from a .jar within a module without making system.\* methods for every method in the library?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 2, 2022, 9:41pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/29 "2022-09-02T21:41:43Z")

</div>

You might be able to lift the contents of a JAR provided by your module into a higher ClassLoader using the `export` element in module.xml:

```auto
<export scope="G">some-library.jar</export>

```

Careful, it’s probably super dangerous and might cause a memory leak if you restart the module.

---

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [September 2, 2022, 9:46pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/30 "2022-09-02T21:46:43Z")

</div>

I want to use the library on a production system. This sounds too risky. Dropping the jar into lib/core/common after every upgrade sounds less risky, but still a pain because you have to remember to do that.

Is there a reason why there is not native support for adding a jar to the gateway scope?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 2, 2022, 9:49pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/31 "2022-09-02T21:49:01Z")

</div>

> [@Jonathan](#):
>
> Is there a reason why there is not native support for adding a jar to the gateway scope?

It's dangerous. Modules have isolated ClassLoaders by default. If they all shared a ClassLoader, or when you start exporting stuff to a higher ClassLoader, you now have to worry about making sure that you don't export a library or class that the gateway or _any_ other module uses, or that you don't export an incompatible version, etc. And it's easy to cause a memory leak when a module can reach up and register something like this in the gateway, which can then in turn be referenced and hung onto by scripting. 😱

edit: and it's just not supported outside of modules because it makes upgrade logic go from dead simple to complicated enough it'll probably be wrong in some cases. Right now it's easy: wipe out the library directories and then replace them. No state to keep track of. No figuring out what versions of what libraries are in what version of Ignition, or diffing to figure out if a user added one.

People already regularly screw up zip distribution upgrades by thinking they can just unzip on top of the existing install, but when a library goes from `foo-1.0.0.jar` to `foo-1.1.0.jar` you suddenly have 2 different versions of the same library on the ClassPath and get non-deterministic behavior.

---

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [September 2, 2022, 9:57pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/32 "2022-09-02T21:57:31Z")

</div>

Thanks for the thorough response. If we go with dropping the jar in lib/core/common, other than the pain of us having to keep track of adding it back in after upgrades, what risk are we taking on? Is this something that could stop working in future releases of Ignition?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 2, 2022, 9:59pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/33 "2022-09-02T21:59:30Z")

</div>

I think that’s pretty unlikely to stop working. The biggest risk is procedural, which you already understand. The library doesn’t come along with backups or upgrades.

You previously mentioned gateway scope, but are now talking about `lib/core/common`. If you _only_ need it in gateway scope you can use `lib/core/gateway`. By putting it in common it’s also forcing the library to be downloaded by the Designer and Clients.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 2, 2022, 10:23pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/34 "2022-09-02T22:23:39Z")

</div>

You might find this topic helpful, particularly the public release of my `FieldClassBuilder`:

> [@Machine Learning in Ignition using MATLAB](https://forum.inductiveautomation.com/t/machine-learning-in-ignition-using-matlab/56053/4):
>
> So, showing the whole thing is not on the table. Sorry. But one utility class I wrote will help you through the trickiest part of it: [FieldClassBuilder.java](https://www.automation-pros.com/streamer/FieldClassBuilder.java) (Note the MIT License.) This builder will assemble any desired classes from anywhere in your module, including any dynamically loaded classes you might access, into a generated class with a public static final field for each supplied class. The output from build() is suitable for use with ScriptManager.addScriptModule() in your various …

Note that this permits controlled exposure of classes in your module's classloader (normal jar) to any desired `system.*` namespaces without having to manually construct them when you change jar versions. I use this to accept many different versions of an externally-installed OpenCV jar (due to licensing, it cannot be packaged in a module) but still expose (nearly) all of its member classes.

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [September 3, 2022, 10:16am UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/35 "2022-09-03T10:16:56Z")

</div>

Manually dropping jars in lib/core/gateway works fine but needs restarting of the gateway to be visible to scripts. On upgrading Ignition version these jars will still be visible. However, if you need to upgrade the jars then you have to delete the current one and add new ones and obviously restart the gateway.

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [September 4, 2022, 2:47pm UTC](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529/36 "2022-09-04T14:47:31Z")

</div>

> [@pturmel](#):
>
> to any desired `system.*` namespaces without having to manually construct them when you change jar versions

This is the way.

[Previous page](https://forum.inductiveautomation.com/t/add-jar-for-using-classes-in-jython-scripts/12529.md?page=1)
