diff --git a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java index d2dac88609dd4..327fb0f615f73 100644 --- a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java +++ b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java @@ -51,7 +51,15 @@ public void scopeValues(ScriptEngine scriptEngine, Map scopeValu ImportCustomizer importCustomizer = new ImportCustomizer(); for (Map.Entry entry : scopeValues.entrySet()) { if (entry.getValue() instanceof Class clazz) { - importCustomizer.addImport(entry.getKey(), clazz.getCanonicalName()); + String canonicalName = clazz.getCanonicalName(); + try { + // Only add imports for classes that are available to the classloader + getClass().getClassLoader().loadClass(canonicalName); + importCustomizer.addImport(entry.getKey(), canonicalName); + logger.debug("Added import for {} as {}", entry.getKey(), canonicalName); + } catch (ClassNotFoundException e) { + logger.debug("Unable to add import for {} as {}", entry.getKey(), canonicalName, e); + } } else { scriptEngine.put(entry.getKey(), entry.getValue()); }