pub struct JavaRunner { /* private fields */ }Expand description
A builder for configuring and executing a Java program (JAR or main class).
This struct allows you to set the Java runtime, JAR file or main class, memory limits, program arguments, and I/O redirection before spawning the process.
§Examples
use java_manager::{JavaRunner, JavaRedirect};
JavaRunner::new()
.java(java)
.jar("myapp.jar")
.min_memory(256 * 1024 * 1024) // 256 MB
.max_memory(1024 * 1024 * 1024) // 1 GB
.arg("--server")
.redirect(JavaRedirect::new().output("out.log").error("err.log"))
.execute()?;Implementations§
Source§impl JavaRunner
impl JavaRunner
Sourcepub fn java(self, java: JavaInfo) -> Self
pub fn java(self, java: JavaInfo) -> Self
Sets the Java installation to use.
This is mandatory before calling execute.
Sourcepub fn jar(self, jar: impl AsRef<Path>) -> Self
pub fn jar(self, jar: impl AsRef<Path>) -> Self
Sets the JAR file to execute (implies the -jar flag).
Either jar or main_class must be set.
Sourcepub fn min_memory(self, bytes: usize) -> Self
pub fn min_memory(self, bytes: usize) -> Self
Sets the initial heap size (-Xms).
The value is given in bytes and will be formatted as a memory string
(e.g., 256m, 1g). If the size is not a multiple of a megabyte or gigabyte,
it will be rounded to the nearest megabyte.
Sourcepub fn max_memory(self, bytes: usize) -> Self
pub fn max_memory(self, bytes: usize) -> Self
Sets the maximum heap size (-Xmx).
See min_memory for formatting details.
Sourcepub fn main_class(self, class: impl Into<String>) -> Self
pub fn main_class(self, class: impl Into<String>) -> Self
Sets the main class to execute (instead of a JAR file).
Either jar or main_class must be set.
Sourcepub fn arg(self, arg: impl Into<String>) -> Self
pub fn arg(self, arg: impl Into<String>) -> Self
Adds a single argument to be passed to the Java program.
Arguments are appended in the order they are added.
Sourcepub fn redirect(self, redirect: JavaRedirect) -> Self
pub fn redirect(self, redirect: JavaRedirect) -> Self
Sets I/O redirection options.
Sourcepub fn execute(self) -> Result<(), JavaError>
pub fn execute(self) -> Result<(), JavaError>
Executes the configured Java program.
§Errors
Returns JavaError::Other if no Java installation has been set, or if
neither a JAR file nor a main class has been specified.
Returns JavaError::NotFound if the Java executable does not exist.
Returns JavaError::IoError if file operations or process spawning fail.
Returns JavaError::ExecutionFailed if the Java process exits with a non‑zero status.