Skip to content

Instantly share code, notes, and snippets.

@f2janyway
Last active August 31, 2024 08:11
Show Gist options
  • Select an option

  • Save f2janyway/fcb33dc9a6cc23e5e917de4488f2a61f to your computer and use it in GitHub Desktop.

Select an option

Save f2janyway/fcb33dc9a6cc23e5e917de4488f2a61f to your computer and use it in GitHub Desktop.
Build failed on iosApp of KMP project by "Command PhaseScriptExecution failed with a nonzero exit code"

It shows "Unable to locate a Java Runtime"

This answer from gpt solved the issue.

my kdoctor all checked already

Answer

The error you're encountering, "Unable to locate a Java Runtime," suggests that the build script requires Java to be available, but Xcode isn't able to find the Java installation. This could be happening because the JAVA_HOME environment variable isn't correctly set in the context of Xcode, or Xcode can't access Java due to a configuration issue.

Here are the steps you can follow to resolve this issue:

1. Ensure Java is Properly Installed

  • Verify that Java is correctly installed by running:
    java -version
  • Ensure it returns the expected Java version.

2. Set JAVA_HOME in Xcode's Environment

  • Xcode doesn't always inherit the environment variables from your shell. You may need to explicitly set JAVA_HOME in Xcode's build settings or within the script that fails.

Option 1: Set JAVA_HOME in the Build Script

  • Edit the build script (e.g., Script-BEA8885189D408D600647BDC228A6A20.sh) that is failing.
  • Add the following line at the beginning of the script:
    export JAVA_HOME=$(/usr/libexec/java_home)
  • This command will dynamically set JAVA_HOME to the correct value based on the installed JDK.

Option 2: Set JAVA_HOME in Xcode Build Settings

  • Open your project in Xcode.
  • Go to Project > Build Settings.
  • Add a new "User-Defined" setting by clicking the "+" at the top and name it JAVA_HOME.
  • Set the value to /opt/homebrew/opt/openjdk.

3. Ensure Xcode Uses the Correct Java Version

  • Run the following command to set the default Java version in your shell:
    sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
  • This command creates a symlink to the correct JDK in the system’s Java Virtual Machines directory.

4. Uncheck "Based on Dependency Analysis" in Script Phase

  • Open your Xcode project.
  • Go to the Build Phases tab.
  • Locate the [CP-User] Build shared phase (or the one with the script causing issues).
  • Uncheck the "Based on dependency analysis" option.

5. Add Script Output Files

  • In the Build Phases section, for the script that runs Java, you can specify output files. This will prevent the script from running unnecessarily.
  • For example, if the script generates files like .jar or .class, you can add these paths as output files.

6. Clean and Rebuild

  • After making these changes, perform a clean build:
    Product > Clean Build Folder (Shift + ⌘ + K)
  • Then, try building the project again.

7. Verify Paths in the Script

  • Make sure that the script is using the correct paths and that any paths involving Java are correctly pointing to the right Java installation.

By ensuring that JAVA_HOME is correctly set and accessible by Xcode, and addressing the dependency analysis warning, you should be able to resolve the build error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment