Skip to content

Instantly share code, notes, and snippets.

@shannah
Created February 17, 2024 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shannah/e0ea66ff216ce84f464dced84d71a5a6 to your computer and use it in GitHub Desktop.
Save shannah/e0ea66ff216ce84f464dced84d71a5a6 to your computer and use it in GitHub Desktop.
Script to inject
bash update_jnf_linkage.sh /path/to/JavaNativeFoundation.framework.zip /path/to/MyApp.app
Download a prebuild JavaNativeFoundation.framework.zip at https://drive.google.com/file/d/1j9HCMNvlplIBdgG6zitfZqxYVM1seB-J/view?usp=drive_link
Or build it yourself from sources https://github.com/apple/openjdk/tree/xcodejdk14-release/apple/JavaNativeFoundation
#!/bin/bash
# Check if correct arguments are given
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <path-to-JavaNativeFoundation.zip> <path-to-App.app>"
exit 1
fi
JNF_ZIP_PATH="$1"
APP_PATH="$2"
JNF_FRAMEWORK_NAME="JavaNativeFoundation.framework"
APP_FRAMEWORKS_DIR="$APP_PATH/Contents/Frameworks"
TEMP_DIR="$APP_PATH/Contents/Frameworks/temp_jnf_extraction"
# Ensure the Frameworks directory exists
mkdir -p "$APP_FRAMEWORKS_DIR"
# Extract JavaNativeFoundation.framework to the temporary directory
ditto -x -k "$JNF_ZIP_PATH" "$APP_FRAMEWORKS_DIR/"
JRE_LIB_DIR="$APP_PATH/Contents/Java/jre/lib"
cd "$JRE_LIB_DIR"
rm -rf "$JNF_FRAMEWORK_NAME"
RELATIVE_PATH_TO_FRAMEWORKS="../../../Frameworks"
# Create a symlink in the JRE's "lib" directory pointing to the extracted JavaNativeFoundation.framework
ln -s "$RELATIVE_PATH_TO_FRAMEWORKS/$JNF_FRAMEWORK_NAME" "$JNF_FRAMEWORK_NAME"
cd - > /dev/null
# Function to update linkage
update_linkage() {
local file="$1"
# Check if the file links to the system JavaNativeFoundation
if otool -L "$file" | grep -q '/System/Library/Frameworks/JavaVM.framework/Versions/A/Frameworks/JavaNativeFoundation.framework/Versions/A/JavaNativeFoundation'; then
# Change linkage to use the version in @executable_path/../Frameworks/
install_name_tool -change "/System/Library/Frameworks/JavaVM.framework/Versions/A/Frameworks/JavaNativeFoundation.framework/Versions/A/JavaNativeFoundation" "@executable_path/../Frameworks/JavaNativeFoundation.framework/Versions/A/JavaNativeFoundation" "$file"
echo "Updated linkage for $file"
fi
}
export -f update_linkage
# Update dependencies in the JRE directory
find "$JRE_LIB_DIR" \( -name "*.dylib" -o -name "*.so" -o -perm +111 \) -type f -exec bash -c 'update_linkage "$0"' {} \;
# Additionally check and update the Contents/MacOS and Contents/Frameworks directories
APP_MACOS_DIR="$APP_PATH/Contents/MacOS"
# Function to update app dependency
update_app_dependency() {
local search_dir="$1"
find "$search_dir" \( -name "*.dylib" -o -name "*.so" -o -perm +111 \) -type f -exec bash -c 'update_linkage "$0"' {} \;
}
update_app_dependency "$APP_MACOS_DIR"
update_app_dependency "$APP_FRAMEWORKS_DIR"
echo "Finished updating dependencies."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment