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, arbitrary tool arguments, memory limits, program arguments, and I/O redirection before spawning the process.

§Examples

Run a JAR:

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()?;

Run a tool command (no JAR or main class needed):

JavaRunner::new()
    .java(java)
    .cmd(&["-version"])
    .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 cmd(self, args: &[&str]) -> Self

Sets arbitrary tool arguments for the Java command (no JAR or main class).

Use this to run commands like java -version, java --list-modules, or java -XshowSettings. When cmd_args is set, the jar and main_class requirements are bypassed.

§Examples
JavaRunner::new()
    .java(java)
    .cmd(&["-version"])
    .execute()?;
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 classpath(self, paths: &[impl AsRef<Path>]) -> Self

Sets the classpath (-cp / -classpath).

Paths are joined with the platform-specific separator (; on Windows, : otherwise).

Source

pub fn module_path(self, path: impl AsRef<Path>) -> Self

Sets the module path (--module-path).

Source

pub fn add_opens( self, module: impl Into<String>, package: impl Into<String>, target: impl Into<String>, ) -> Self

Adds a --add-opens flag (e.g., java.base/java.lang=ALL-UNNAMED).

Source

pub fn add_exports( self, module: impl Into<String>, package: impl Into<String>, target: impl Into<String>, ) -> Self

Adds a --add-exports flag (e.g., java.base/com.sun.internal=ALL-UNNAMED).

Source

pub fn system_property( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Sets a system property (-Dkey=value).

Source

pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self

Sets an environment variable for the child Java process.

Source

pub fn working_dir(self, path: impl AsRef<Path>) -> Self

Sets the working directory for the child Java process.

Source

pub fn timeout(self, duration: Duration) -> Self

Sets a timeout for the Java process.

If the process runs longer than the specified duration, it will be killed. A Duration::ZERO or None means no timeout.

Source

pub fn execute(&self) -> Result<(), JavaError>

Executes the configured Java program.

When cmd_args is set, the JAR / main-class requirement is bypassed and the provided arguments are passed directly to java.

§Errors

Returns JavaError::Other if no Java installation has been set, or if neither a JAR file nor a main class (nor cmd arguments) 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.