Skip to content

Commit

Permalink
Merge pull request #7 from EmilyFlarionIO/master
Browse files Browse the repository at this point in the history
fix: JAVA_HOME points to JRE directory on OpenJDK8, should instead point to JDK directory
  • Loading branch information
astonbitecode authored Jan 30, 2025
2 parents e5354f6 + aae2c9f commit 4c883ed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ glob = "0.3"
docopt = { version = "1.1", optional = true }

[features]
build-binary = ["docopt"]
build-binary = ["docopt"]
locate-jdk-only = []
29 changes: 25 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, (<http://www.apache.org/licenses/LICENSE-2.0>)
* MIT license (<http://opensource.org/licenses/MIT>)
*/

Expand All @@ -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
Expand Down Expand Up @@ -129,7 +141,7 @@ pub fn locate_java_home() -> Result<String> {
#[cfg(target_os = "windows")]
fn do_locate_java_home() -> Result<String> {
let output = Command::new("where")
.arg("java")
.arg(LOCATE_BINARY)
.output()
.map_err(|e| JavaLocatorError::new(format!("Failed to run command `where` ({e})")))?;

Expand Down Expand Up @@ -184,7 +196,7 @@ fn do_locate_java_home() -> Result<String> {
#[cfg(not(any(target_os = "windows", target_os = "macos")))] // Unix
fn do_locate_java_home() -> Result<String> {
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();
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 4c883ed

Please sign in to comment.