Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
# The exact Temurin build to bundle, in OpenJDK notation, such as 21.0.8+9 or 21.0.13.1+2.
# Its first 3 numbers are the ones of the version in pyproject.toml, which `test_java_version` checks.
# setup-java installs this build or fails: it never falls back to another build with the same numbers, such as one preinstalled on the runner.
TEMURIN_VERSION: 21.0.8+9
TEMURIN_VERSION: 21.0.13.1+2
steps:
- uses: actions/checkout@v7.0.1
with:
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ pip install jdk4py
## Usage

```python
>>> from jdk4py import JAVA, JAVA_HOME, JAVA_VERSION
>>> from jdk4py import JAVA, JAVA_FULL_VERSION, JAVA_HOME, JAVA_VERSION
>>> JAVA_HOME
PosixPath('/Users/johndoe/dev/jdk4py/jdk4py/java-runtime')
>>> JAVA
PosixPath('/Users/johndoe/dev/jdk4py/jdk4py/java-runtime/bin/java')
>>> JAVA_VERSION
(21, 0, 8)
(21, 0, 13)
>>> JAVA_FULL_VERSION
'21.0.13.1+2'
>>> from subprocess import run
>>> some_java_options = ["-Xmx16G", "-Xms2G"]
>>> run(
Expand All @@ -35,5 +37,15 @@ PosixPath('/Users/johndoe/dev/jdk4py/jdk4py/java-runtime/bin/java')

`jdk4py`'s version contains 4 numbers:

- The first 3 numbers correspond to the JDK version.
- The fourth number is the library API version.
- The first 3 numbers are the version line of the bundled JDK, such as 21.0.8. `JAVA_VERSION` holds them.
- The fourth number counts the releases of jdk4py in that line. A new release can bundle another build of the JDK, such as an OpenJDK patch release, or change the library.

The fourth number is not part of the JDK version, even when they look alike.
`JAVA_FULL_VERSION` holds the exact version of the bundled JDK with its build number, such as 21.0.13.1+2: the version can have more than 3 numbers.

| jdk4py | JDK |
| --------- | ----------- |
| 21.0.13.1 | 21.0.13.1+2 |
| 21.0.8.2 | 21.0.8+9 |
| 21.0.8.1 | 21.0.8+9 |
| 21.0.8.0 | 21.0.8+9 |
21 changes: 17 additions & 4 deletions build_java_runtime.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
from shutil import rmtree
from pathlib import Path
from shutil import rmtree, which
from subprocess import check_call

from jdk4py import JAVA_HOME
from jdk4py import JAVA_HOME, _get_release_property
from jdk4py._added_modules import ADDED_MODULES
from jdk4py._included_locales import INCLUDED_LOCALES


def build_java_runtime() -> None:
jlink = which("jlink")
if jlink is None:
message = "jlink not found."
raise RuntimeError(message)
# The `release` file of the JDK that jlink comes from has the version with its
# build number, such as 21.0.13.1+2-LTS. jlink only keeps the version without it,
# so it is added back for `JAVA_FULL_VERSION`.
source_java_home = Path(jlink).resolve().parent.parent
runtime_version = _get_release_property(source_java_home, "JAVA_RUNTIME_VERSION")

rmtree(JAVA_HOME, ignore_errors=True)

check_call( # noqa: S603
[ # noqa: S607
"jlink",
[
jlink,
"--no-man-pages",
"--strip-debug",
"--add-modules",
",".join(ADDED_MODULES),
f"--include-locales={','.join(INCLUDED_LOCALES)}",
"--release-info",
f'add:JAVA_RUNTIME_VERSION="{runtime_version}"',
"--output",
JAVA_HOME,
],
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ keywords = ["jdk", "java", "jvm", "jre"]
name = "jdk4py"
readme = "README.md"
requires-python = ">=3.10"
version = "21.0.8.2"
version = "21.0.13.1"

[project.urls]
Repository = "https://github.com/activeviam/jdk4py"
Expand Down
26 changes: 26 additions & 0 deletions src/jdk4py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""JDK packaged for Python."""

import re as _re
from functools import cache as _cache
from importlib.metadata import version as _version
from pathlib import Path as _Path

Expand All @@ -13,3 +15,27 @@
int(number) for number in _VERSION.split(".")
]
JAVA_VERSION = _JAVA_MAJOR, _JAVA_MINOR, _JAVA_PATCH


def _get_release_property(java_home: _Path, key: str, /) -> str:
release_file = java_home / "release"
for line in release_file.read_text(encoding="utf-8").splitlines():
line_key, _, value = line.partition("=")
if line_key == key:
return value.strip('"')
raise RuntimeError(f"No {key} in {release_file}.")


@_cache
def _get_java_full_version() -> str:
runtime_version = _get_release_property(JAVA_HOME, "JAVA_RUNTIME_VERSION")
# Drop the optional suffix after the build number, such as `-LTS` in 21.0.8+9-LTS.
return _re.sub(r"(\+\d+)-.*$", r"\1", runtime_version)


def __getattr__(name: str) -> str:
# `JAVA_FULL_VERSION` is read from the Java runtime on first access:
# the runtime does not exist yet when `build_java_runtime.py` imports this module.
if name == "JAVA_FULL_VERSION":
return _get_java_full_version()
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
11 changes: 9 additions & 2 deletions tests/test_jdk4py.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from subprocess import run

from jdk4py import JAVA, JAVA_VERSION
from jdk4py import JAVA, JAVA_FULL_VERSION, JAVA_VERSION
from jdk4py._included_locales import INCLUDED_LOCALES

_TEST_RESOURCES_DIRECTORY = Path(__file__).parent / "resources"
Expand All @@ -19,7 +19,14 @@ def test_java_version() -> None:
assert match, f"Unexpected output:\n{completed_process.stdout}"
version = match.group("version")
assert isinstance(version, str)
assert tuple([int(number) for number in version.split(".")][:3]) == JAVA_VERSION
assert tuple(int(number) for number in version.split(".")[:3]) == JAVA_VERSION
build_match = re.search(r"\(build (?P<build>[^)]+)\)", completed_process.stderr)
assert build_match, f"Unexpected output:\n{completed_process.stderr}"
# The build can end with an optional suffix, such as `-LTS` in 21.0.13.1+2-LTS.
assert re.fullmatch(
rf"{re.escape(JAVA_FULL_VERSION)}(-.+)?", build_match.group("build")
)
assert JAVA_FULL_VERSION.startswith(f"{version}+")


def test_jar_execution() -> None:
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.