writeProfileDScript creates the java.sh profile.d script This script sets JAVA_HOME, JRE_HOME, and updates PATH at application runtime
()
| 163 | // writeProfileDScript creates the java.sh profile.d script |
| 164 | // This script sets JAVA_HOME, JRE_HOME, and updates PATH at application runtime |
| 165 | func (z *ZingJRE) writeProfileDScript() error { |
| 166 | // Compute relative path from jreDir to javaHome |
| 167 | relPath, err := filepath.Rel(z.jreDir, z.javaHome) |
| 168 | if err != nil { |
| 169 | return fmt.Errorf("failed to compute relative path: %w", err) |
| 170 | } |
| 171 | |
| 172 | // Build the runtime JAVA_HOME path |
| 173 | // At runtime, DEPS_DIR will point to the app's dependency directory |
| 174 | javaHomePath := filepath.Join("$DEPS_DIR", "0", "jre", relPath) |
| 175 | |
| 176 | // Create the environment script content |
| 177 | envContent := fmt.Sprintf(`export JAVA_HOME=%s |
| 178 | export JRE_HOME=$JAVA_HOME |
| 179 | export PATH=$JAVA_HOME/bin:$PATH |
| 180 | `, javaHomePath) |
| 181 | |
| 182 | // Write the profile.d script using libbuildpack API |
| 183 | if err := z.ctx.Stager.WriteProfileD("java.sh", envContent); err != nil { |
| 184 | return fmt.Errorf("failed to write profile.d script: %w", err) |
| 185 | } |
| 186 | |
| 187 | // Also set environment for the staging process |
| 188 | os.Setenv("JAVA_HOME", z.javaHome) |
| 189 | os.Setenv("JRE_HOME", z.javaHome) |
| 190 | os.Setenv("PATH", fmt.Sprintf("%s/bin:%s", z.javaHome, os.Getenv("PATH"))) |
| 191 | |
| 192 | return nil |
| 193 | } |