- 
                Notifications
    
You must be signed in to change notification settings  - Fork 9
 
External libraries
You may want to use third-party code in your add-on. Chorus supports both JVM (.jar) and JavaScript (.js) libraries.
The loadJar(jar) function loads and injects the classes contained in the JAR file into the classpath. jar refers to a path starting from the add-on's folder (chorus/addons/your-addon/).
This should be called either at the very start of your file or in onInit.
Now that the classpath is updated it's possible to load classes by using the type(class) function.
Example:
loadJar('mylib.jar');
// Loading the class
const MyObject = type('my.package.MyObject');
// Instantiating an object
const myObject = new MyObject(params); // new (type('...'))(params) is valid too
// Calling a static method
MyObject.staticMethod(); // type('...').staticMethod() is valid tooIn case any error regarding missing internal resources occurs while using loadJar consider switching to a class loader approach using createClassLoader(jar);:
const loader = createClassLoader('mylib.jar');
const MyObject = type('my.package.MyObject', loader);A JavaScript file can be imported either locally or remotely.
A local file can be imported via the loadJS(js) function. As for loading JARs, js is a relative path that starts from your add-on's folder.
loadJS('myscript.js');Loading libraries from the internet should always be avoided due to big delays and mutability of the target file. However, if you really need to load a remote library, just call the load(url) function:
load('https://.../myscript.js');Note that not every JavaScript library might work (especially those related to the DOM) and that it is not granted that a NPM module will load properly.