diff --git a/.github/workflows/ci-workflow.yml b/.github/workflows/ci-workflow.yml index e04180c..03ac950 100644 --- a/.github/workflows/ci-workflow.yml +++ b/.github/workflows/ci-workflow.yml @@ -18,13 +18,26 @@ jobs: os: [ ubuntu-latest, macos-latest, windows-latest ] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up JDK 17 - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: '17' distribution: 'adopt' - name: Build Rust with Cargo run: cargo build --verbose - name: Test Rust with Cargo - run: cargo test --verbose -- --nocapture \ No newline at end of file + run: cargo test --verbose -- --nocapture + + # This is a good test for the locate-jdk-only feature, as the JRE is in a different path on JDK 8 + test-locate-jdk: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 8 + uses: actions/setup-java@v4 + with: + java-version: '8' + distribution: 'temurin' + - name: Test Rust with Cargo + run: JAVA_HOME="" cargo test --features=locate-jdk-only --verbose -- --nocapture diff --git a/Cargo.toml b/Cargo.toml index c99373a..45a35e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,5 @@ glob = "0.3" docopt = { version = "1.1", optional = true } [features] -build-binary = ["docopt"] \ No newline at end of file +build-binary = ["docopt"] +locate-jdk-only = [] diff --git a/src/lib.rs b/src/lib.rs index db9b754..14c870d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,12 +78,19 @@ The latter two commands should return something like: > /usr/lib/jvm/java-11-openjdk-amd64/lib +## Extra Features + +* `locate-jdk-only`: Attempts to locate the JDK by searching for the Java compiler, + as opposed to searching for the runtime. + This solves issues in JDK 8 where the JRE resides in a subdirectory and not in the JDK root, + so development files are not found in JAVA_HOME as would be expected. + ## License At your option, under: -* Apache License, Version 2.0, (http://www.apache.org/licenses/LICENSE-2.0) -* MIT license (http://opensource.org/licenses/MIT) +* Apache License, Version 2.0, () +* MIT license () */ @@ -96,6 +103,11 @@ use glob::{glob, Pattern}; pub mod errors; +#[cfg(not(feature = "locate-jdk-only"))] +const LOCATE_BINARY: &str = "java"; +#[cfg(feature = "locate-jdk-only")] +const LOCATE_BINARY: &str = "javac"; + /// Returns the name of the jvm dynamic library: /// /// * libjvm.so for Linux @@ -129,7 +141,7 @@ pub fn locate_java_home() -> Result { #[cfg(target_os = "windows")] fn do_locate_java_home() -> Result { let output = Command::new("where") - .arg("java") + .arg(LOCATE_BINARY) .output() .map_err(|e| JavaLocatorError::new(format!("Failed to run command `where` ({e})")))?; @@ -184,7 +196,7 @@ fn do_locate_java_home() -> Result { #[cfg(not(any(target_os = "windows", target_os = "macos")))] // Unix fn do_locate_java_home() -> Result { let output = Command::new("which") - .arg("java") + .arg(LOCATE_BINARY) .output() .map_err(|e| JavaLocatorError::new(format!("Failed to run command `which` ({e})")))?; let java_exec_path = std::str::from_utf8(&output.stdout)?.trim(); @@ -276,4 +288,13 @@ mod unit_tests { fn locate_java_from_exec_test() { println!("do_locate_java_home: {}", do_locate_java_home().unwrap()); } + + #[test] + fn jni_headers_test() { + let java_home = do_locate_java_home().unwrap(); + assert!(PathBuf::from(java_home) + .join("include") + .join("jni.h") + .exists()); + } }