Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: JAVA_HOME points to JRE directory on OpenJDK8, should instead point to JDK directory #7

Merged
merged 9 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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>)

*/

@@ -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<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})")))?;

@@ -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();
@@ -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());
}
}