Currently, the shell environment is queried for the environment variables to be passed onto the process.
|
/// Environment variables captured from a login shell. |
|
/// |
|
/// The login-shell environment is useful for inheriting user profile setup |
|
/// (for example PATH modifications from shell startup files). |
|
/// Returns `nil` when the shell cannot be launched or returns no entries. |
|
public let defaultShellEnvironment: [String: String]? = { |
|
let task = Process() |
|
task.executableURL = URL(fileURLWithPath: SHELL) |
|
task.arguments = ["-l", "-c", "env -0"] |
|
|
|
let stdoutPipe = Pipe() |
|
task.standardOutput = stdoutPipe |
|
task.standardError = FileHandle.nullDevice |
|
|
|
let output: Data |
|
do { |
|
try task.run() |
|
// Drain stdout while the process is alive to avoid pipe backpressure deadlocks. |
|
output = stdoutPipe.fileHandleForReading.readDataToEndOfFile() |
|
task.waitUntilExit() |
|
} catch { |
|
return nil |
|
} |
|
|
|
guard task.terminationStatus == 0 else { |
|
return nil |
|
} |
|
|
|
var environment: [String: String] = [:] |
|
for rawEntry in output.split(separator: 0) { |
|
let entry = String(decoding: rawEntry, as: UTF8.self) |
|
guard let separator = entry.firstIndex(of: "=") else { |
|
continue |
|
} |
|
let key = String(entry[..<separator]) |
|
let value = String(entry[entry.index(after: separator)...]) |
|
environment[key] = value |
|
} |
|
|
|
return environment.isEmpty ? nil : environment |
|
}() |
Normal Mac apps do not inherit the environment variables from the default shell. Java applications should not be treated differently as a whole. Loading the environment variables should still be possible, but it should require setting a key in the Info.plist.
Any Java application can get the environment variables still by calling the default shell with any configuration. Just not with System.getenv().
Currently, the shell environment is queried for the environment variables to be passed onto the process.
NativeJavaApplicationStub/NativeJavaApplicationStub/Util/Shell.swift
Lines 25 to 65 in 436bf69
Normal Mac apps do not inherit the environment variables from the default shell. Java applications should not be treated differently as a whole. Loading the environment variables should still be possible, but it should require setting a key in the Info.plist.
Any Java application can get the environment variables still by calling the default shell with any configuration. Just not with
System.getenv().