# Importing Java Libraries and accessing Java Classes from script console

**URL:** <https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047>\
**Category:** Ignition\
**Tags:** scripting, ignition81\
**Created:** [August 16, 2022, 2:46pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047 "2022-08-16T14:46:33Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![derrickdvdsn](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@derrickdvdsn](https://forum.inductiveautomation.com/u/derrickdvdsn)\
**Post date:** [August 16, 2022, 2:46pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/1 "2022-08-16T14:46:33Z")

</div>

How to access the getAvailableConnections() method from com.inductiveautomation.ignition.gateway.gan using the script console ?

When I try this

“from com.inductiveautomation.ignition.gateway import gan” I get the following error

Traceback (most recent call last):  
File “”, line 1, in   
ImportError: cannot import name gan

Also if I manually import the Java classes with a custom module,  
I get “getAvailableConnections(): expected 1 args; got 0” when I run  
[print GatewayNetworkManager.getAvailableConnections()] from the script console.

---

<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:** [August 16, 2022, 2:52pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/2 "2022-08-16T14:52:57Z")

</div>

You don’t, that’s something that is only available in 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:** [August 16, 2022, 2:57pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/3 "2022-08-16T14:57:07Z")

</div>

Maybe you can take a few steps back and talk about what you’re ultimately trying to accomplish.

---

<div class="post-metadata">

**Author:** ![derrickdvdsn](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@derrickdvdsn](https://forum.inductiveautomation.com/u/derrickdvdsn)\
**Post date:** [August 16, 2022, 3:12pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/4 "2022-08-16T15:12:40Z")

</div>

Kevin,

My end goal is to create a script that can auto approve incoming connections from a dockerized instance of Ignition.

I am trying to figure out if I can access these classes and methods using the python script instead of creating a custom module.

Thanks in Advance

---

<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:** [August 16, 2022, 3:22pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/5 "2022-08-16T15:22:41Z")

</div>

If you want to accept all incoming connections automatically it would be easier to just set the Connection Policy setting for the gateway network to “Unrestricted” instead of “ApprovedOnly”.

---

<div class="post-metadata">

**Author:** ![derrickdvdsn](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@derrickdvdsn](https://forum.inductiveautomation.com/u/derrickdvdsn)\
**Post date:** [August 16, 2022, 3:34pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/6 "2022-08-16T15:34:58Z")

</div>

Kevin,

Need to approve the certificates also automatically because for every pod restart a new certificate will be generated

---

<div class="post-metadata">

**Author:** ![derrickdvdsn](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@derrickdvdsn](https://forum.inductiveautomation.com/u/derrickdvdsn)\
**Post date:** [August 24, 2022, 10:01am UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/7 "2022-08-24T10:01:27Z")

</div>

Kevin,  
I have created a module based on the ignition-scripting-function sdk where I am trying to read the Incoming connection Id and output it to the logger but I am not sure how or from where I can call this method? What’s the correct way to read the output of a method that is available only in the gateway scope?

```auto
GatewayScriptModule

package com.inductiveautomation.ignition.examples.scripting;

import com.inductiveautomation.ignition.gateway.gan.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GatewayScriptModule extends AbstractScriptModule {

    private final Logger logger = LoggerFactory.getLogger("GAN");
    @Override
    protected int multiplyImpl(int arg0, int arg1) {
        return arg0 * arg1;
    }

    public long connId(){
        long conn = new IncomingConnection().getId();

        if ((System.currentTimeMillis() / 1000)%10 == 0){
            String s =String.valueOf(conn);
            logger.info(s);}
        return conn;
    }

}

```

```auto
GatewayModuleHook

package com.inductiveautomation.ignition.examples.scripting;

import com.inductiveautomation.ignition.common.licensing.LicenseState;
import com.inductiveautomation.ignition.common.script.ScriptManager;
import com.inductiveautomation.ignition.common.script.hints.PropertiesFileDocProvider;
import com.inductiveautomation.ignition.gateway.clientcomm.ClientReqSession;
import com.inductiveautomation.ignition.gateway.model.AbstractGatewayModuleHook;
import com.inductiveautomation.ignition.gateway.model.GatewayContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GatewayHook extends AbstractGatewayModuleHook {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final GatewayScriptModule scriptModule = new GatewayScriptModule();

    @Override
    public void setup(GatewayContext gatewayContext) {
        logger.info("setup()");
    }

    @Override
    public void startup(LicenseState licenseState) {
        logger.info("startup()");
    }

    @Override
    public void shutdown() {
        logger.info("shutdown()");
    }

    @Override
    public void initializeScriptManager(ScriptManager manager) {
        super.initializeScriptManager(manager);

        manager.addScriptModule(
                "system.example",
                scriptModule,
                new PropertiesFileDocProvider());
    }

    @Override
    public Object getRPCHandler(ClientReqSession session, String projectName) {
        return scriptModule;
    }
}

```

---

<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:** [August 24, 2022, 12:45pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/8 "2022-08-24T12:45:32Z")

</div>

See this topic:

> [@ScriptContext through client scope does not return project, but does in Perspective](https://forum.inductiveautomation.com/t/scriptcontext-through-client-scope-does-not-return-project-but-does-in-perspective/62604):
>
> I am using com.inductiveautomation.ignition.common.script.ScriptContext#defaultProject() to get the project from the client calling a script, so that I can make sure I read the resource out of the right project. What I am running into is that if I run this in the Gateway scope (A Perspective Button) this seems to work fine. However when I try this out of the client scope (Script console) I am getting a null in response? I am following the recommendation from [this forum post](https://forum.inductiveautomation.com/t/executing-a-custom-script-as-a-gateway-script-with-hidden-parameters/50315/4), however potentiall…

Especially note the comment about the lameness of the script module example for this case.

---

<div class="post-metadata">

**Author:** ![derrickdvdsn](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@derrickdvdsn](https://forum.inductiveautomation.com/u/derrickdvdsn)\
**Post date:** [August 24, 2022, 6:56pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/9 "2022-08-24T18:56:54Z")

</div>

@pturmel Thank you for your response. I am still new to Ignition’s SDK development and Java and I can’t seem to figure out why you redirected me to that post. Please correct me if I am wrong but my understanding of the RPC is to serve as a connection between the Designer/Client and the Gateway but since I am running only in the Gateway scope, I am not sure if the RPC is the problem in my instance.

Also when I try to call the connId() method and output it’s contents to the logger, I get the following error

```auto
Error running action 'dom.onClick' on yt@D/root/Button: Traceback (most recent call last): File "<function:runAction>", line 2, in runAction at simpleorm.dataset.SFieldScalar.getRawFieldValue(SFieldScalar.java:132) at simpleorm.dataset.SRecordInstance.getObject(SRecordInstance.java:228) at simpleorm.dataset.SRecordInstance.getLong(SRecordInstance.java:456) at com.inductiveautomation.ignition.gateway.gan.IncomingConnection.getId(IncomingConnection.java:35) at com.inductiveautomation.ignition.examples.scripting.GatewayScriptModule.connId(GatewayScriptModule.java:16) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) simpleorm.utils.SException$Error: simpleorm.utils.SException$Error: Cannot get unretrieved/unset field [IncomingConnection].[F IncomingConnection.wsincomingconnection_ID]

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 24, 2022, 7:03pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/10 "2022-08-24T19:03:48Z")

</div>

Aside from any other issues, what you’re ultimately trying to do won’t work as-is. You can’t just create a new instance of `com.inductiveautomation.ignition.gateway.gan.IncomingConnection` and expect things to work.

You need to ask the PersistenceInterface (available from the GatewayContext you get in `setup`) for existing IncomingConnection records. But even if you get them, I’m not sure just blindly setting the security status (if that’s what you’re ultimately trying to do) will actually do anything.

---

<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:** [August 24, 2022, 7:03pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/11 "2022-08-24T19:03:56Z")

</div>

The incoming connection is the session, which is available in the gateway hook when the RPC subsystem looks up your script module. But the sample code doesn’t make unique RPC handlers per session, so that information is lost. You are **creating** a new connection object in your code, but that would have no bearing on the caller of your script.

Oh, and the script console is not in gateway scope. It is in designer scope, which is much like vision client scope.

---

<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:** [August 24, 2022, 7:05pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/12 "2022-08-24T19:05:05Z")

</div>

Oooo! Incoming GAN connection. Yeah, what Paul said.

---

<div class="post-metadata">

**Author:** ![derrickdvdsn](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@derrickdvdsn](https://forum.inductiveautomation.com/u/derrickdvdsn)\
**Post date:** [August 25, 2022, 10:05am UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/13 "2022-08-25T10:05:30Z")

</div>

@pturmel and @paul-griffith , thank you for your response, My ultimate goal is to have the remote certificates for Incoming GAN connection be approved without manual intervention. What’s your recommendation on trying to implement this? Can you point me in the right direction.

Also, I don’t see the Incoming Remote certificates for GAN in the internal db?

@pturmel is calling the method via button in perspective session considered a gateway scope?

---

<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:** [August 25, 2022, 12:24pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/14 "2022-08-25T12:24:37Z")

</div>

> [@derrickdvdsn](#):
>
> considered a gateway scope?

Yes.

---

<div class="post-metadata">

**Author:** ![derrickdvdsn](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@derrickdvdsn](https://forum.inductiveautomation.com/u/derrickdvdsn)\
**Post date:** [August 26, 2022, 12:58pm UTC](https://forum.inductiveautomation.com/t/importing-java-libraries-and-accessing-java-classes-from-script-console/63047/15 "2022-08-26T12:58:36Z")

</div>

@pturmel and @paul-griffith , can you please tell me how to use Squery to query existing records from the internal db that shows all the fields?

When I try the following

```auto
	from com.inductiveautomation.ignition.gateway import IgnitionGateway
	from com.inductiveautomation.ignition.gateway.gan import WSConnectionSettings
	from com.inductiveautomation.ignition.gateway.gan import IncomingConnection
	from simpleorm.dataset import SQuery,SQueryMode
	
	gateway = IgnitionGateway.get()
	pi = gateway.getPersistenceInterface()
	gan_manager = gateway.getGatewayAreaNetworkManager()
	connections = str(gan_manager.getInboundConnections())
	
	query = SQuery(IncomingConnection.META)
	record = pi.query(query)
	
	logger = system.util.getLogger("myLogger")
	logger.info(str(record))

```

I get  
`[[IncomingConnection 5]]`

which is correct because in the internal db, I see this

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/8/b87630472180ac62cc02f14d3e912d475f66ecc4.png)

but how can I get all the other columns in the output?
