Module java

Module java 

Source
Expand description

The Java module provides functionality for fetching and installing Java runtimes from Mojang’s Piston API. This enables automatic Java runtime management for Minecraft launchers.

§Structures

§JavaManifest

The root structure containing Java runtime information for all supported platforms.

pub struct JavaManifest {
	pub linux: Runtimes,
	pub linux_i386: Runtimes,
	pub macos: Runtimes,
	pub macos_arm64: Runtimes,
	pub windows_arm64: Runtimes,
	pub windows_x64: Runtimes,
	pub windows_x86: Runtimes,
}
FieldTypeDescription
linuxRuntimesRuntimes for Linux x64
linux_i386RuntimesRuntimes for Linux x86 (32-bit)
macosRuntimesRuntimes for macOS x64
macos_arm64RuntimesRuntimes for macOS ARM64 (Apple Silicon)
windows_arm64RuntimesRuntimes for Windows ARM64
windows_x64RuntimesRuntimes for Windows x64
windows_x86RuntimesRuntimes for Windows x86 (32-bit)

§Runtimes

Collection of available Java runtime variants for a platform.

pub struct Runtimes {
	pub alpha: Vec<JavaRuntime>,
	pub beta: Vec<JavaRuntime>,
	pub gamma: Vec<JavaRuntime>,
	pub delta: Vec<JavaRuntime>,
	pub gamma_snapshot: Vec<JavaRuntime>,
	pub epsilon: Vec<JavaRuntime>,
	pub legacy: Vec<JavaRuntime>,
	pub minecraft_java_exe: serde_json::Value,
}
FieldTypeDescription
alphaVec<JavaRuntime>Java runtime alpha (Java 16)
betaVec<JavaRuntime>Java runtime beta (Java 17)
gammaVec<JavaRuntime>Java runtime gamma (Java 17)
deltaVec<JavaRuntime>Java runtime delta (Java 21)
gamma_snapshotVec<JavaRuntime>Java runtime gamma snapshot
epsilonVec<JavaRuntime>Java runtime epsilon (Java 24+)
legacyVec<JavaRuntime>Legacy JRE (Java 8)
minecraft_java_exeserde_json::ValueMinecraft Java executable metadata

§JavaRuntime

Represents a specific Java runtime version with its manifest and availability information.

pub struct JavaRuntime {
	pub version: Version,
	pub manifest: Manifest,
	pub availability: Availability,
}
FieldTypeDescription
versionVersionVersion information
manifestManifestDownload manifest for the runtime
availabilityAvailabilityAvailability/rollout information

§Version

Version information for a Java runtime.

pub struct Version {
	pub name: String,
	pub released: chrono::DateTime<chrono::Utc>,
}
FieldTypeDescription
nameStringVersion name (e.g., “17.0.8”)
releasedDateTime<Utc>Release timestamp

§Manifest

Manifest metadata for downloading a Java runtime.

pub struct Manifest {
	pub sha1: String,
	pub size: usize,
	pub url: String,
}
FieldTypeDescription
sha1StringSHA1 hash of the manifest file
sizeusizeSize of the manifest file in bytes
urlStringURL to download the manifest

§Availability

Availability information for a Java runtime.

pub struct Availability {
	pub group: u32,
	pub progress: u32,
}
FieldTypeDescription
groupu32Availability group identifier
progressu32Rollout progress percentage

§JavaInstallationFile

Represents a single file in a Java runtime installation.

pub struct JavaInstallationFile {
	pub name: String,
	pub file_type: Option<FileType>,
	pub executable: Option<bool>,
	pub downloads: Option<Downloads>,
}
FieldTypeDescription
nameStringFile path relative to the installation directory
file_typeOption<FileType>Type of file (file, directory, or link)
executableOption<bool>Whether the file should be marked executable
downloadsOption<Downloads>Download information (absent for directories)

§Downloads

Download options for a Java installation file.

pub struct Downloads {
	pub lzma: Option<DownloadItem>,
	pub raw: DownloadItem,
}
FieldTypeDescription
lzmaOption<DownloadItem>LZMA compressed download (if available)
rawDownloadItemUncompressed raw download

§DownloadItem

Information for downloading a specific file.

pub struct DownloadItem {
	pub sha1: String,
	pub size: usize,
	pub url: String,
}
FieldTypeDescription
sha1StringSHA1 hash for validation
sizeusizeFile size in bytes
urlStringDownload URL

§FileType

Type of file in the Java installation.

pub enum FileType {
	File,
	Directory,
	Link,
}
VariantDescription
FileRegular file
DirectoryDirectory (no download required)
LinkSymbolic link

§Methods

§JavaManifest::fetch()

Fetches the Java runtime manifest from Mojang’s Piston API.

pub async fn fetch() -> Result<Self>

Returns: Result<JavaManifest> - The parsed Java manifest containing all available runtimes

§JavaRuntime::get_installation_files()

Fetches the list of files required for installing this Java runtime.

pub async fn get_installation_files(&self) -> Result<Vec<JavaInstallationFile>>

Returns: Result<Vec<JavaInstallationFile>> - List of all files in the runtime

§JavaRuntime::install()

Downloads and installs the Java runtime to a specified directory.

pub async fn install(
	&self,
	directory: impl AsRef<Path>,
	parallel: u16,
	sender: Option<tokio::sync::mpsc::Sender<MultiDownloadProgress>>,
) -> Result<()>

Parameters:

  • directory - Directory where the Java runtime will be installed
  • parallel - Number of concurrent downloads
  • sender - Optional channel for receiving download progress updates

Returns: Result<()> - Success or error

§Examples

§Fetch Java Manifest

use piston_mc::java::JavaManifest;

#[tokio::main]
async fn main() {
	// Fetch the Java runtime manifest from Mojang's Piston API
	let manifest = JavaManifest::fetch().await.expect("Failed to fetch Java manifest");

	// Access runtimes for a specific platform
	let windows_runtimes = &manifest.windows_x64;

	// Get the first gamma runtime (Java 17)
	if let Some(runtime) = windows_runtimes.gamma.first() {
		println!("Java version: {}", runtime);
	}
}

§Install Java Runtime Without Progress

use piston_mc::java::JavaManifest;
use std::path::Path;

#[tokio::main]
async fn main() {
	// Fetch the Java runtime manifest
	let manifest = JavaManifest::fetch().await.expect("Failed to fetch Java manifest");

	// Get the gamma runtime (Java 17) for Windows x64
	if let Some(runtime) = manifest.windows_x64.gamma.first() {
		// Define the installation directory
		let install_dir = Path::new("target/java/17");

		// Install with 50 parallel downloads, no progress reporting
		runtime.install(install_dir, 50, None).await.expect("Failed to install Java runtime");

		println!("Java runtime installed successfully");
	}
}

§Install Java Runtime With Progress

use simple_download_utility::MultiDownloadProgress;
use piston_mc::java::JavaManifest;
use std::path::Path;

#[tokio::main]
async fn main() {
	// Fetch the Java runtime manifest
	let manifest = JavaManifest::fetch().await.expect("Failed to fetch Java manifest");

	// Get the gamma runtime (Java 17) for Windows x64
	if let Some(runtime) = manifest.windows_x64.gamma.first() {
		// Define the installation directory
		let install_dir = Path::new("target/java/17");

		// Create a channel for receiving download progress updates
		let (sender, mut receiver) = tokio::sync::mpsc::channel::<MultiDownloadProgress>(16);

		// Start the installation task
		let task = runtime.install(install_dir, 100, Some(sender));

		// Spawn a separate task to handle progress updates
		tokio::spawn(async move {
			while let Some(progress) = receiver.recv().await {
				// Calculate download completion percentage
				let percent = (progress.files_downloaded as f32 / progress.files_total as f32) * 100.0;

				// Convert download speed to MB/s
				let mb_per_sec = progress.bytes_per_second as f32 / 1024.0 / 1024.0;

				println!(
					"Installing Java: {:.2}% ({}/{} files) {:.2} MB/s",
					percent,
					progress.files_downloaded,
					progress.files_total,
					mb_per_sec
				);
			}
		});

		// Await the installation task completion
		task.await.expect("Failed to install Java runtime");

		println!("Java runtime installed successfully");
	}
}

§Get Installation Files

use piston_mc::java::JavaManifest;

#[tokio::main]
async fn main() {
	// Fetch the Java runtime manifest
	let manifest = JavaManifest::fetch().await.expect("Failed to fetch Java manifest");

	// Get the legacy runtime (Java 8) for Linux
	if let Some(runtime) = manifest.linux.legacy.first() {
		// Fetch the list of installation files
		let files = runtime.get_installation_files().await.expect("Failed to get installation files");

		println!("Java {} requires {} files", runtime, files.len());

		// List some files
		for file in files.iter().take(10) {
			println!("  {}", file.name);
		}
	}
}

§Select Runtime for Current Platform

use piston_mc::java::{JavaManifest, Runtimes};

#[tokio::main]
async fn main() {
	let manifest = JavaManifest::fetch().await.expect("Failed to fetch Java manifest");

	// Select runtimes based on current platform
	let runtimes: &Runtimes = if cfg!(target_os = "windows") {
		if cfg!(target_arch = "x86_64") {
			&manifest.windows_x64
		} else if cfg!(target_arch = "aarch64") {
			&manifest.windows_arm64
		} else {
			&manifest.windows_x86
		}
	} else if cfg!(target_os = "macos") {
		if cfg!(target_arch = "aarch64") {
			&manifest.macos_arm64
		} else {
			&manifest.macos
		}
	} else {
		if cfg!(target_arch = "x86") {
			&manifest.linux_i386
		} else {
			&manifest.linux
		}
	};

	// Get the recommended Java 17 runtime (gamma)
	if let Some(runtime) = runtimes.gamma.first() {
		println!("Recommended Java runtime: {}", runtime);
	}
}

§Runtime Types

Mojang provides several Java runtime variants for different Minecraft versions:

RuntimeJava VersionMinecraft Versions
legacyJava 81.0 - 1.16.5
alphaJava 161.17 - 1.17.1
betaJava 17Early 1.18 snapshots
gammaJava 171.18 - 1.20.4
gamma_snapshotJava 17Snapshot versions
deltaJava 211.20.5+
epsilonJava 24+Future versions

§API Source

The Java runtime manifest is fetched from Mojang’s Piston API:

https://piston-meta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json

Structs§

Availability
DownloadItem
Downloads
JavaInstallationFile
JavaManifest
JavaRuntime
Manifest
Runtimes
Version

Enums§

FileType