skip to Main Content

I am trying to run an application that needs Java as a prerequisite on a github actions runner. My yaml script is like:

steps:
  .
  .
  - name: Setup Java
    uses: actions/setup-java@v2
    with:
      distribution: 'adopt'
      java-version: '21'
  .
  .
  - name: Step that needs Java
    uses: ...
    with:
      ...

The Java setup goes fine, but the step that needs Java fails with an error saying
ERROR: The JAVA_HOME environment variable is not defined correctly. JAVA_HOME is set to /opt/hostedtoolcache/Java_Adopt_jdk/21.0.4-7.0.LTS/x64 and it does not exist.

The correct path is /opt/hostedtoolcache/Java_Adopt_jdk/21.0.4-7.0.LTS/x64/bin What do I do to append /bin to the end of JAVA_HOME?

2

Answers


  1. Your setup is correct. JAVA_HOME is also correct. If you run just the setup action actions/setup-java@v2, java is setup correctly and $JAVA_HOME/bin is in the PATH.

    The issue is most likely with the steps that follow java setup in your workflow.

    This workflow proves it:

    name: java
    
    on:
      workflow_dispatch
    
    jobs:
      java:
        runs-on: ubuntu-latest
        steps:
        - name: Setup Java
          uses: actions/setup-java@v2
          with:
            distribution: 'adopt'
            java-version: '21'
    
        - name: Show java
          run: |
            echo $JAVA_HOME
            echo $PATH
            java -version
    

    The output of the "Show java" step is like this:

    /opt/hostedtoolcache/Java_Adopt_jdk/21.0.4-7.0.LTS/x64
    
    /opt/hostedtoolcache/Java_Adopt_jdk/21.0.4-7.0.LTS/x64/bin:/snap/bin:/home/runner/.local/bin:/opt/pipx_bin:/home/runner/.cargo/bin:/home/runner/.config/composer/vendor/bin:/usr/local/.ghcup/bin:/home/runner/.dotnet/tools:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
    
    openjdk version "21.0.4" 2024-07-16 LTS
    OpenJDK Runtime Environment Temurin-21.0.4+7 (build 21.0.4+7-LTS)
    OpenJDK 64-Bit Server VM Temurin-21.0.4+7 (build 21.0.4+7-LTS, mixed mode, sharing)
    
    Login or Signup to reply.
  2. similar issue I am facing on below workflow:

      - name: Set up JDK 17     
           uses: actions/setup-java@v4
           with:
            java-version: '17'
            distribution: 'temurin'
       #step that needs java 
         - name: Use oxr463/setup-leiningen 
           uses: oxr463/[email protected]
           with:
            exec: |
                lein uberjar && 
                cp "$(find . -name '*.jar')" .
    

    Please do suggest how to fix this issue ?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search