# Add Jenkins Shared Libraries with classes reference in a Jenkinsfile dynamically

**URL:** <https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271>\
**Category:** Ask a question\
**Tags:** question, sig-platform\
**Created:** [January 10, 2023, 11:40am UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271 "2023-01-10T11:40:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarvabhowma1995](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/sarvabhowma1995/32/2038_2.png) [@Sarvabhowma1995](https://community.jenkins.io/u/Sarvabhowma1995)\
**Post date:** [January 10, 2023, 11:40am UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/1 "2023-01-10T11:40:26Z")

</div>

Hi,

I have constructed a Shared Library in which I want to have class definitions, below is an example of shared lib code.

```auto
package apicode
class ApiCode implements Serializable {

    private final Map data
    private final DSL steps

    ApiCode(Map data, DSL steps) {
        this.data = data
        this.steps = steps
    }
}

```

I have placed this class definition in a shared library git repo, with the path `apiCode/src/apicode/apiCode.groovy`. I am importing the shared library using the following step at the top of my Jenkins file, outside any node or pipeline directive:

```auto
library identifier: 'apiCode@master', retriever: modernSCM(
    scm: [
       $class: 'GitSCMSource', 
        credentialsId: '<my-credentials-id>',
        remote: '<my-git-remote>',
        traits: [gitBranchDiscovery()]
    ],
    libraryPath: 'apiCode/'
)
import apicode.*

```

When I use the above class from the shared library it’s throwing an exception saying that the class is not found.

Below is my simple Jenkins pipeline file to call the class.

```auto
library identifier: 'apiCode@master', retriever: modernSCM(
   scm: [
       $class: 'GitSCMSource', 
        credentialsId: '<My GIT Credentials>',
        remote: '<my shared lib gitbranch>',
        traits: [gitBranchDiscovery()]
    ],
    libraryPath: 'apiCode/'
)
import apicode.*

pipeline {
    agent any
    stages {
        stage("test1") {
            steps {
               script {
                    def data = [:]
                    ApiCode foo = new ApiCode(data, steps)
                }
            }
        }
    }
}

```

It’s throwing the below error.

```auto
WorkflowScript: 18: unable to resolve class ApiCode
 @ line 18, column 29.
               ApiCode foo = new ApiCode(env.data, steps)
                               ^

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:981)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:626)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox$Scope.parse(GroovySandbox.java:163)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:562)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:514)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:336)
    at hudson.model.ResourceController.execute(ResourceController.java:107)
    at hudson.model.Executor.run(Executor.java:449)
Finished: FAILURE

```

So can anyone please help me how to get this class loaded in my Jenkins job dynamically?

---

<div class="post-metadata">

**Author:** ![PierreBtz](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/pierrebtz/32/55_2.png) [@PierreBtz](https://community.jenkins.io/u/PierreBtz)\
**Post date:** [January 10, 2023, 11:52am UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/2 "2023-01-10T11:52:07Z")

</div>

I would suggest starting by following the naming conventions for naming your class, ie it should be `Api`. I’m not sure of all the conversions made by Groovy behind the scenes but it’s entirely possible that might be the cause for this error.

---

<div class="post-metadata">

**Author:** ![Sarvabhowma1995](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/sarvabhowma1995/32/2038_2.png) [@Sarvabhowma1995](https://community.jenkins.io/u/Sarvabhowma1995)\
**Post date:** [January 10, 2023, 12:31pm UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/3 "2023-01-10T12:31:22Z")

</div>

Thank you for the message @PierreBtz

I actually changed the original name from my actual code to api for reference here and I am sure the naming convention is not the problem here.

I think Jenkins is not able to load the classes from dynamically loaded shared libraries. because of that, I am unable to use the class from the dynamically loaded shared library.

I actually tested the same by adding the global library path under Jenkins configuration and used the below simple code to load the class into my Jenkins and it worked perfectly fine. and the issue is coming only when I am loading it dynamically.

```auto
@Library('apiCode')_
import apicode.ApiCode

```

---

<div class="post-metadata">

**Author:** ![Sarvabhowma1995](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/sarvabhowma1995/32/2038_2.png) [@Sarvabhowma1995](https://community.jenkins.io/u/Sarvabhowma1995)\
**Post date:** [January 10, 2023, 1:04pm UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/4 "2023-01-10T13:04:11Z")

</div>

I just changed my example pretty much similar to my actual code (Just names changed)

---

<div class="post-metadata">

**Author:** ![PierreBtz](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/pierrebtz/32/55_2.png) [@PierreBtz](https://community.jenkins.io/u/PierreBtz)\
**Post date:** [January 10, 2023, 1:37pm UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/5 "2023-01-10T13:37:35Z")

</div>

For dynamic libraries, please refer to [this document](https://www.jenkins.io/doc/book/pipeline/shared-libraries/#loading-libraries-dynamically), specifically:

> However you may use library classes dynamically (without type checking), accessing them by fully-qualified name from the return value of the `library` step. `static` methods can be invoked using a Java-like syntax:

```auto
library('my-shared-library').com.mycorp.pipeline.Utils.someStaticMethod()

```

> You can also access `static` fields, and call constructors as if they were `static` methods named `new`:

```auto
def useSomeLib(helper) { // dynamic: cannot declare as Helper
    helper.prepare()
    return helper.count()
}

def lib = library('my-shared-library').com.mycorp.pipeline // preselect the package

echo useSomeLib(lib.Helper.new(lib.Constants.SOME_TEXT))

```

---

<div class="post-metadata">

**Author:** ![Sarvabhowma1995](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/sarvabhowma1995/32/2038_2.png) [@Sarvabhowma1995](https://community.jenkins.io/u/Sarvabhowma1995)\
**Post date:** [January 10, 2023, 2:32pm UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/6 "2023-01-10T14:32:17Z")

</div>

> [@PierreBtz](#):
>
> ibrary classes dynamically (without type checking), accessing them by fully-qualified name

Thank you @PierreBtz

I have read the document you have provided and do not understand it properly on how I can integrate it with my example.

Would you mind helping me with an example that I have provided above .?

---

<div class="post-metadata">

**Author:** ![Sarvabhowma1995](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/sarvabhowma1995/32/2038_2.png) [@Sarvabhowma1995](https://community.jenkins.io/u/Sarvabhowma1995)\
**Post date:** [January 10, 2023, 3:54pm UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/7 "2023-01-10T15:54:12Z")

</div>

@PierreBtz

I have used a multi-branch pipeline and used the `Pipeline Libraries` plugin to achieve this. After configuring this I am able to import the class simply with the import command. 🙂

---

<div class="post-metadata">

**Author:** ![myusuf3](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/myusuf3/32/3112_2.png) [@myusuf3](https://community.jenkins.io/u/myusuf3)\
**Post date:** [January 10, 2023, 6:01pm UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/8 "2023-01-10T18:01:05Z")

</div>

What did you end up doing?

---

<div class="post-metadata">

**Author:** ![Sarvabhowma1995](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/sarvabhowma1995/32/2038_2.png) [@Sarvabhowma1995](https://community.jenkins.io/u/Sarvabhowma1995)\
**Post date:** [January 11, 2023, 9:00am UTC](https://community.jenkins.io/t/add-jenkins-shared-libraries-with-classes-reference-in-a-jenkinsfile-dynamically/5271/9 "2023-01-11T09:00:31Z")

</div>

> [@Sarvabhowma1995](#):
>
> Pipeline Libraries

I have used the below plugin, which will give me an option to implicitly configure the libraries for individual multibranch pipelines. after configuring it I can simply import the class from the shared library with the import command.

**Library name:** [Pipeline: Groovy Libraries](https://plugins.jenkins.io/pipeline-groovy-lib/)  
**Option Name in the multibranch pipeline to configure implicitly shared libs:** Pipeline Libraries (check the Load implicit option) and provide the shared libraries git details.
