Answers: <p>It looks like Groovy can't find your class at the path specified.</p>
  
  <p>Try renaming the file common.groovy to match the class name and move it to the common folder:</p>
  
  <pre><code>src/org/common/CommonFuncs.groovy
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-02-20T03:10:35.360Z
Answers: I was creating a filename with lowercase style, but remembering what u mentioned, helped me a lot, specially the words highlighted in the jenkins documentation :
  `The basename of each .groovy file should be a Groovy (~ Java) identifier, conventionally camelCased.` , reference : https://www.jenkins.io/doc/book/pipeline/shared-libraries/#directory-structure
Answers: Manuel Lazo
Answers: CC BY-SA 4.0
Answers: 2020-05-14T07:08:32.830Z
Answers: 2020-05-14T07:08:32.830Z
Answers: Guido Vaccarella
Body: <p>Here is the folder structure in my shared library repo:</p>
  
  <pre><code>$ tree src vars
  src
  ├── org
      └── common.groovy
  
  vars
  ├── testSrc.groovy
  </code></pre>
  
  <p>src has:</p>
  
  <pre><code>$ cat src/org/common.groovy
  package org.common
  
  class CommonFuncs {
      def sayHi() {
          echo "Hi from CommonFuncs!"
      }
  
      def sayHello(str) {
          echo "Hello ${str}"
      }
  }
  </code></pre>
  
  <p>and vars:</p>
  
  <pre><code>$ cat vars/testSrc.groovy
  def call() {
      def tst = new org.common.CommonFuncs()
      return tst.sayHi()
  }
  </code></pre>
  
  <p>pipeline script in Jenkins has:</p>
  
  <pre><code>@Library('jenkins-library') _
  pipeline {
      agent { label 'my_servers' }
  
      stages {
          stage('test') {
              steps {
                  testSrc()
              }
          }
      }
  }
  </code></pre>
  
  <p>When I run job, I get below error:</p>
  
  <pre><code>org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
  /var/jenkins_home/jobs/Test/jobs/mave/jobs/test_src/builds/8/libs/jenkins-library/vars/testSrc.groovy: 2: unable to resolve class org.common.CommonFuncs 
   @ line 2, column 15.
         def tst = new org.common.CommonFuncs()
                   ^
  </code></pre>
  
  <p>What is wrong in here? I am new to groovy, and this is my first attempt to create a class in shared library src/, any help is appreciated.</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-05-13T13:42:02.013Z
Comments: is it possible to have the class in src/ and use it in vars/?
Comments: rodee
Comments: CC BY-SA 4.0
Comments: 2020-05-13T16:36:15.287Z
Comments: Try renaming common.groovy in CommonFuncs.groovy and moving it to src/org/common/.
Comments: Guido Vaccarella
Comments: CC BY-SA 4.0
Comments: 2020-05-13T17:06:15.893Z
Comments: That worked, renamed the groovy file to match class name. thank you.
Comments: rodee
Comments: CC BY-SA 4.0
Comments: 2020-05-13T18:48:27.847Z
Comments: @GuidoVaccarella, can you add it as answer?
Comments: rodee
Tags: <jenkins-pipeline><groovy>
Title: Unable to use the class defined in src in vars of jenkins shared library
UserDisplayName: rodee
ThreadText: # Unable to use the class defined in src in vars of jenkins shared library
  
  (Asked by: username_0 on 2020-05-12)
  
  Here is the folder structure in my shared library repo:
  
  ```
  $ tree src vars
  src
  ├── org
      └── common.groovy
  
  vars
  ├── testSrc.groovy
  
  ```
  
  src has:
  
  ```
  $ cat src/org/common.groovy
  package org.common
  
  class CommonFuncs {
      def sayHi() {
          echo "Hi from CommonFuncs!"
      }
  
      def sayHello(str) {
          echo "Hello ${str}"
      }
  }
  
  ```
  
  and vars:
  
  ```
  $ cat vars/testSrc.groovy
  def call() {
      def tst = new org.common.CommonFuncs()
      return tst.sayHi()
  }
  
  ```
  
  pipeline script in Jenkins has:
  
  ```
  @Library('jenkins-library') _
  pipeline {
      agent { label 'my_servers' }
  
      stages {
          stage('test') {
              steps {
                  testSrc()
              }
          }
      }
  }
  
  ```
  
  When I run job, I get below error:
  
  ```
  org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
  /var/jenkins_home/jobs/Test/jobs/mave/jobs/test_src/builds/8/libs/jenkins-library/vars/testSrc.groovy: 2: unable to resolve class org.common.CommonFuncs 
   @ line 2, column 15.
         def tst = new org.common.CommonFuncs()
                   ^
  
  ```
  
  What is wrong in here? I am new to groovy, and this is my first attempt to create a class in shared library src/, any help is appreciated.
  
  **Tags:** jenkins-pipeline, groovy
  
  > **Comments:**
  > **username_0**: is it possible to have the class in src/ and use it in vars/?  
  > **username_1**: Try renaming common.groovy in CommonFuncs.groovy and moving it to src/org/common/.  
  > **username_0**: That worked, renamed the groovy file to match class name. thank you.  
  > **username_0**: @username_1, can you add it as answer?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-05-14. Score: 3 | Accepted answer
  
  It looks like Groovy can't find your class at the path specified.
  
  Try renaming the file common.groovy to match the class name and move it to the common folder:
  
  ```
  src/org/common/CommonFuncs.groovy
  
  ```
  
  > **Comments:**
  > **username_2**: I was creating a filename with lowercase style, but remembering what u mentioned, helped me a lot, specially the words highlighted in the jenkins documentation : `The basename of each .groovy file should be a Groovy (~ Java) identifier, conventionally camelCased.` , reference : https://www.jenkins.io/doc/book/pipeline/shared-libraries/#directory-structure  
  
  ---
  
  