Jenkins: WorkflowScript: 1: unable to resolve class Declarative

TLDR: The line "Jenkinsfile (Declarative Pipeline)" in the Jenkins get started page is just a comment, don't paste it in the Jenkins file.

Following the steps in https://www.jenkins.io/doc/pipeline/tour/hello-world/ to try out Jenkins Pipeline, got below error during build:

Branch indexing
 > git rev-parse --is-inside-work-tree # timeout=10
Setting origin to https://github.com/feichashao/test-jenkins.git
 > git config remote.origin.url https://github.com/feichashao/test-jenkins.git # timeout=10
Fetching origin...
Fetching upstream changes from origin
 > git --version # timeout=10
 > git config --get remote.origin.url # timeout=10
 > git fetch --tags --progress -- origin +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
Obtained Jenkinsfile from 742cf2da4538685abe1cc518314f770a4fcd1e91
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 1: unable to resolve class Declarative
 @ line 1, column 26.
   Jenkinsfile (Declarative Pipeline)
                            ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    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:561)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE

I uploaded the Jenkinsfile to the github repo and provide it as a Git source when creating the Jenkins Job.

If I use the Scripted Pipeline, I got the same error.

Downgrade pipeline (to the same version), didn't help.

Uninstall Pipeline: Declarative and Pipeline, restart Jenkins (/restart), then reinstall. Still failed.

The first time I install Jenkins, there's some network issue when installing plugins, I retried installing the plugins at that time, everything looks good but failed. Try reinstalling the whole jenkins this time.

[root@jenkins-host ~]# rm -rf .jenkins/ .groovy/
[root@jenkins-host ~]# java -Dhttp.proxyHost=some.proxy.host -Dhttp.proxyPort=1234 -Dhttps.proxyHost=some.otherorsame.host -Dhttps.proxyPort=2345 -jar jenkins.war --httpPort=8080

Issue persist.

After reaching out to my colleague, my colleague pointed me out that the first line is just a comment. After removing the first line, and using a much simpler Jenkinsfile like below, the build got succeed.

node('master') {
    checkout scm
    stage('Build') {
            sh 'echo hello world'
    }
}