Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 19, 2025

Fix JavahExecutable to work on Java 10+

Summary

Successfully fixed the issue where JavahExecutable fails on Java 10+ because the javah executable was removed from the JDK.

Problem

Java 10 removed the standalone javah tool and replaced it with javac -h for generating JNI header files. This caused the maven-native plugin to fail when running on Java 10+ systems without a separately installed javah.

Solution Implemented

The solution implements automatic detection and fallback with full backward compatibility:

Key Features

  1. Java Version Detection: Detects if running on Java 10+ by parsing system property
  2. Javah Availability Check: Attempts to execute javah to verify it's available
  3. Automatic Fallback: Uses javac -h when on Java 10+ and javah is unavailable
  4. Source File Discovery: Finds Java source files from class names using project source roots
  5. Error Handling: Provides clear error messages when source files cannot be found

Technical Changes

  • JavahExecutable.java: Added version detection, availability check, and javac support
  • JavahConfiguration.java: Added sourceRoots field to support finding source files
  • NativeJavahMojo.java: Updated to pass project source roots to configuration
  • JavahExecutableTest.java: Added test for javac behavior with source roots; fixed existing tests to work on macOS/Java 10+

Testing

  • ✅ All 11 existing tests pass
  • ✅ New test for javac -h with source roots passes
  • ✅ Full build succeeds (mvn clean install)
  • ✅ Manual integration test confirms header generation works
  • ✅ Code review completed with no issues found
  • ✅ Spotless formatting applied and verified
  • ✅ Tests fixed to work on macOS/Java 10+ without javah

Backward Compatibility

  • Fully backward compatible with existing usage
  • Uses javah when available (Java 8, 9, or separately installed)
  • Only uses javac fallback when javah is truly unavailable
  • No breaking changes to API, configuration, or behavior

Files Modified

  1. maven-native-api/src/main/java/org/codehaus/mojo/natives/javah/JavahConfiguration.java
  2. maven-native-components/maven-native-javah/src/main/java/org/codehaus/mojo/natives/javah/JavahExecutable.java
  3. maven-native-components/maven-native-javah/src/test/java/org/codehaus/mojo/natives/javah/JavahExecutableTest.java
  4. native-maven-plugin/src/main/java/org/codehaus/mojo/natives/plugin/NativeJavahMojo.java
  5. pom.xml (formatting fixes applied)

Verification

The implementation has been thoroughly tested and verified to work correctly on both older Java versions (with javah) and newer Java versions (with javac -h fallback). Tests now pass on macOS/Java 10+ environments. All spotless formatting checks pass.

Fixes #17

Original prompt

This section details on the original issue you should resolve

<issue_title>JavahExecutable fails on Java10</issue_title>
<issue_description>Java 10 got rid of the javah executable and replaced it with javac -h.</issue_description>

Comments on the Issue (you are @copilot in this section)

@dantran @hendriks73 is this fixed in alpha-9? @ There is one project - https://github.com/Glavo/gjavah it looks like an implementation of javah in pure java. It is mit licensed. Maybe it can be used instead of javah?

Fixes #17


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 5 commits October 19, 2025 10:55
- Added Java version detection to determine if running on Java 10+
- Added check to see if javah executable is available
- Modified JavahExecutable to use javac -h when javah is not available
- Added sourceRoots configuration to find source files for javac
- Updated NativeJavahMojo to pass source roots to configuration
- Implemented findSourceFiles to locate Java sources from class names

Co-authored-by: slachiewicz <[email protected]>
- Added unit test for javac -h with source roots
- Fixed NullPointerException when project.getCompileSourceRoots() returns null
- All tests now pass including new test for Java 10+ behavior

Co-authored-by: slachiewicz <[email protected]>
Copilot AI changed the title [WIP] Fix javah executable fails on Java 10 Fix JavahExecutable to support Java 10+ by falling back to javac -h Oct 19, 2025
Copilot AI requested a review from slachiewicz October 19, 2025 11:12
Copilot finished work on behalf of slachiewicz October 19, 2025 11:12
@slachiewicz slachiewicz marked this pull request as ready for review October 19, 2025 11:37
@slachiewicz

This comment has been minimized.

This comment has been minimized.

Copilot finished work on behalf of slachiewicz October 19, 2025 17:26
@slachiewicz

This comment has been minimized.

The tests were failing on macOS (Java 10+) because the code automatically
detects Java 10+ and tries to use javac instead of javah. Since the tests
don't provide source files, this caused "Cannot find source files" errors.

Fixed by explicitly setting javahPath in the affected tests to force them
to use javah, bypassing the automatic Java version detection. This allows
the tests to focus on testing the command-line generation logic without
being affected by the runtime environment.

Co-authored-by: slachiewicz <[email protected]>

This comment has been minimized.

Copilot finished work on behalf of slachiewicz October 26, 2025 09:44
@slachiewicz

This comment has been minimized.

This comment has been minimized.

Copilot AI and others added 2 commits October 28, 2025 22:12
Applied spotless formatting to fix self-closing XML tags that need
spaces before the closing />. This resolves all spotless check violations.

Co-authored-by: slachiewicz <[email protected]>
Copilot finished work on behalf of slachiewicz October 28, 2025 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JavahExecutable fails on Java10

2 participants