Skip to main content

JavaRunner

Struct JavaRunner 

Source
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

Source

pub fn new() -> Self

Creates a new builder with default settings.

Source

pub fn java(self, java: JavaInfo) -> Self

Sets the Java installation to use.

This is mandatory before calling execute.

Source

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.

Source

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.

Source

pub fn max_memory(self, bytes: usize) -> Self

Sets the maximum heap size (-Xmx).

See min_memory for formatting details.

Source

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.

Source

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.

Source

pub fn redirect(self, redirect: JavaRedirect) -> Self

Sets I/O redirection options.

Source

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.

Trait Implementations§

Source§

impl Debug for JavaRunner

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for JavaRunner

Source§

fn default() -> JavaRunner

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.