@@ -9,18 +9,9 @@ The `SwiftJavaPlugin` automates `swift-java` command line tool invocations durin
99To install the SwiftPM plugin in your target of choice include the ` swift-java ` package dependency:
1010
1111``` swift
12- import Foundation
12+ // swift-tools-version: 6.3
1313
14- let javaHome = findJavaHome ()
15-
16- let javaIncludePath = " \( javaHome ) /include"
17- #if os (Linux )
18- let javaPlatformIncludePath = " \( javaIncludePath ) /linux"
19- #elseif os (macOS )
20- let javaPlatformIncludePath = " \( javaIncludePath ) /darwin"
21- #elseif os (Windows )
22- let javaPlatformIncludePath = " \( javaIncludePath ) /win32"
23- #endif
14+ import PackageDescription
2415
2516let package = Package (
2617 name : " MyProject" ,
@@ -44,9 +35,11 @@ let package = Package(
4435 // ...
4536 ],
4637 swiftSettings : [
47- .unsafeFlags ([" -I\( javaIncludePath ) " , " -I\( javaPlatformIncludePath ) " ])
38+ // Some swift-java generated code is not yet compatible with swift 6
39+ .swiftLanguageMode (.v5 )
4840 ],
4941 plugins : [
42+ // Include here the plugins you need
5043 .plugin (name : " JavaCompilerPlugin" , package : " swift-java" ),
5144 .plugin (name : " JExtractSwiftPlugin" , package : " swift-java" ),
5245 .plugin (name : " SwiftJavaPlugin" , package : " swift-java" ),
@@ -56,71 +49,4 @@ let package = Package(
5649)
5750```
5851
59- ``` swift
60-
61- // Note: the JAVA_HOME environment variable must be set to point to where
62- // Java is installed, e.g.,
63- // Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home.
64- func findJavaHome () -> String {
65- if let home = ProcessInfo.processInfo.environment[" JAVA_HOME" ] {
66- print (" JAVA_HOME = \( home ) " )
67- return home
68- }
69-
70- // This is a workaround for envs (some IDEs) which have trouble with
71- // picking up env variables during the build process
72- let path = " \( FileManager.default .homeDirectoryForCurrentUser .path () ) .java_home"
73- if let home = try ? String (contentsOfFile : path, encoding : .utf8 ) {
74- if let lastChar = home.last , lastChar.isNewline {
75- return String (home.dropLast ())
76- }
77-
78- return home
79- }
80-
81- if let home = getJavaHomeFromLibexecJavaHome (),
82- ! home.isEmpty {
83- return home
84- }
85-
86- fatalError (" Please set the JAVA_HOME environment variable to point to where Java is installed." )
87- }
88-
89- /// On MacOS we can use the java_home tool as a fallback if we can't find JAVA_HOME environment variable.
90- func getJavaHomeFromLibexecJavaHome () -> String ? {
91- let task = Process ()
92- task.executableURL = URL (fileURLWithPath : " /usr/libexec/java_home" )
93-
94- // Check if the executable exists before trying to run it
95- guard FileManager.default.fileExists (atPath : task.executableURL ! .path ) else {
96- print (" /usr/libexec/java_home does not exist" )
97- return nil
98- }
99-
100- let pipe = Pipe ()
101- task.standardOutput = pipe
102- task.standardError = pipe // Redirect standard error to the same pipe for simplicity
103-
104- do {
105- try task.run ()
106- task.waitUntilExit ()
107-
108- let data = pipe.fileHandleForReading .readDataToEndOfFile ()
109- let output = String (data : data, encoding : .utf8 )? .trimmingCharacters (in : .whitespacesAndNewlines )
110-
111- if task.terminationStatus == 0 {
112- return output
113- } else {
114- print (" java_home terminated with status: \( task.terminationStatus ) " )
115- // Optionally, log the error output for debugging
116- if let errorOutput = String (data : pipe.fileHandleForReading .readDataToEndOfFile (), encoding : .utf8 ) {
117- print (" Error output: \( errorOutput ) " )
118- }
119- return nil
120- }
121- } catch {
122- print (" Error running java_home: \( error ) " )
123- return nil
124- }
125- }
126- ```
52+ > Note: Depending on the use case, swift-java may require running Gradle or accessing files outside the Swift package. Ensure that your environment allows Gradle to run, and add the ` --disable-sandbox ` parameter when invoking the ` swift ` command to build the package.
0 commit comments