How to specify jdk version, Jenkins keeps using default Java

I been using Jenkins 2.346.1 which by default the controller uses java 8. I have added a java 7 jdk via Global Tools Configuration. I’m trying to use Grails to build a WAR file, that needs java 7, I’ve created both a freestyle project and a pipeline project, that should explicitly be using Java 7. But every time the freestyle project builds it use the java 8, and so does the pipeline.

Here is my pipeline-line code:

pipeline {
agent any
stages {
stage (‘checkout svn’) {
steps {
script{
def svnVar = checkout([$class: ‘SubversionSCM’,
additionalCredentials: ,
excludedCommitMessages: ‘’,
excludedRegions: ‘’,
excludedRevprop: ‘’,
excludedUsers: ‘’,
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: ‘’,
locations: [[cancelProcessOnExternalsFail: true,
credentialsId: ‘e6c2f984-a684-4401-93af-69c39ad4aa74’,
depthOption: ‘infinity’,
ignoreExternalsOption: true,
local: ‘.’,
remote: ‘https://svn.chpc.utah.edu/repo/mhp/medhomexml/trunk’]],
quietOperation: true,
workspaceUpdater: [$class: ‘UpdateUpdater’]])
svnRevision = svnVar.SVN_REVISION
}

        }
    }
    stage ('change name') {
        steps {
            script {
                currentBuild.displayName = "v${svnRevision}"
            }
        }
    }
    stage('inject GWT_HOME') {
           steps {
                     sh "export GWT_HOME=/opt/gwt-2.6.1/"
                     echo "$GWT_HOME"
                }
    }
    stage ('run ant') {
        tools {
            jdk 'java-8.0.292'
        }
        steps {
            sh "ant -Dsvn.revision=${svnRevision} deploydev" 
            sh 'ant -version'
        }
    }
    stage('grails war') {
        tools {
           jdk 'java-7-sdk'
            
        }
        steps {
            sh "echo workspace: ${WORKSPACE}"
            sh "echo svnRevision: ${svnRevision}"
            sh 'java -version'
            sh 'javac -version'
            sh """echo $JAVA_HOME
            
            cd grails 
            java -version
            export JAVA_HOME=/usr/local/sdkman/candidates/java/7.0.352-zulu/
            /opt/grails-2.3.11/bin/grails -Dgrails.work.dir=${WORKSPACE}/target/ -Dgrails.env=prod -Dgrails.project.war.file=/var/lib/jenkins/workspace/pipeline-test/target/medhome-prod.${svnRevision}.war war --non-interactive --plain-output --refresh-dependencies
            """
        }
    }
}

}

This is causing issues when the war application runs. Any idea on how to fix this?

Any idea how I could fix this issue?

Hello @echujonut and welcome to this community :wave: .

Could you try with moving

tools {
            jdk 'jdk 'java-7-sdk''
        }

to the agent definition?

Thanks for the help.

I’m not using any agents besides the controller, is that going to be an issue?
I have this:

pipeline {
agent any {
tools {
jdk ‘java-8.0.292’
}
}
stages {
stage (‘checkout svn’) {

After running the build, it stills seem that is building the WAR with java-8, any other things I should be looking for?

As far as I know, It’s not a good idea to keep an agent on the controller, security-wise.