Skip to content

Commit 4c883ed

Browse files
Merge pull request #7 from EmilyFlarionIO/master
fix: JAVA_HOME points to JRE directory on OpenJDK8, should instead point to JDK directory
2 parents e5354f6 + aae2c9f commit 4c883ed

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

.github/workflows/ci-workflow.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@ jobs:
1818
os: [ ubuntu-latest, macos-latest, windows-latest ]
1919

2020
steps:
21-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
2222
- name: Set up JDK 17
23-
uses: actions/setup-java@v3
23+
uses: actions/setup-java@v4
2424
with:
2525
java-version: '17'
2626
distribution: 'adopt'
2727
- name: Build Rust with Cargo
2828
run: cargo build --verbose
2929
- name: Test Rust with Cargo
30-
run: cargo test --verbose -- --nocapture
30+
run: cargo test --verbose -- --nocapture
31+
32+
# This is a good test for the locate-jdk-only feature, as the JRE is in a different path on JDK 8
33+
test-locate-jdk:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v4
37+
- name: Set up JDK 8
38+
uses: actions/setup-java@v4
39+
with:
40+
java-version: '8'
41+
distribution: 'temurin'
42+
- name: Test Rust with Cargo
43+
run: JAVA_HOME="" cargo test --features=locate-jdk-only --verbose -- --nocapture

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ glob = "0.3"
2626
docopt = { version = "1.1", optional = true }
2727

2828
[features]
29-
build-binary = ["docopt"]
29+
build-binary = ["docopt"]
30+
locate-jdk-only = []

src/lib.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,19 @@ The latter two commands should return something like:
7878
7979
> /usr/lib/jvm/java-11-openjdk-amd64/lib
8080
81+
## Extra Features
82+
83+
* `locate-jdk-only`: Attempts to locate the JDK by searching for the Java compiler,
84+
as opposed to searching for the runtime.
85+
This solves issues in JDK 8 where the JRE resides in a subdirectory and not in the JDK root,
86+
so development files are not found in JAVA_HOME as would be expected.
87+
8188
## License
8289
8390
At your option, under:
8491
85-
* Apache License, Version 2.0, (http://www.apache.org/licenses/LICENSE-2.0)
86-
* MIT license (http://opensource.org/licenses/MIT)
92+
* Apache License, Version 2.0, (<http://www.apache.org/licenses/LICENSE-2.0>)
93+
* MIT license (<http://opensource.org/licenses/MIT>)
8794
8895
*/
8996

@@ -96,6 +103,11 @@ use glob::{glob, Pattern};
96103

97104
pub mod errors;
98105

106+
#[cfg(not(feature = "locate-jdk-only"))]
107+
const LOCATE_BINARY: &str = "java";
108+
#[cfg(feature = "locate-jdk-only")]
109+
const LOCATE_BINARY: &str = "javac";
110+
99111
/// Returns the name of the jvm dynamic library:
100112
///
101113
/// * libjvm.so for Linux
@@ -129,7 +141,7 @@ pub fn locate_java_home() -> Result<String> {
129141
#[cfg(target_os = "windows")]
130142
fn do_locate_java_home() -> Result<String> {
131143
let output = Command::new("where")
132-
.arg("java")
144+
.arg(LOCATE_BINARY)
133145
.output()
134146
.map_err(|e| JavaLocatorError::new(format!("Failed to run command `where` ({e})")))?;
135147

@@ -184,7 +196,7 @@ fn do_locate_java_home() -> Result<String> {
184196
#[cfg(not(any(target_os = "windows", target_os = "macos")))] // Unix
185197
fn do_locate_java_home() -> Result<String> {
186198
let output = Command::new("which")
187-
.arg("java")
199+
.arg(LOCATE_BINARY)
188200
.output()
189201
.map_err(|e| JavaLocatorError::new(format!("Failed to run command `which` ({e})")))?;
190202
let java_exec_path = std::str::from_utf8(&output.stdout)?.trim();
@@ -276,4 +288,13 @@ mod unit_tests {
276288
fn locate_java_from_exec_test() {
277289
println!("do_locate_java_home: {}", do_locate_java_home().unwrap());
278290
}
291+
292+
#[test]
293+
fn jni_headers_test() {
294+
let java_home = do_locate_java_home().unwrap();
295+
assert!(PathBuf::from(java_home)
296+
.join("include")
297+
.join("jni.h")
298+
.exists());
299+
}
279300
}

0 commit comments

Comments
 (0)