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 classpath(self, paths: &[impl AsRef<Path>]) -> Self
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).
Sourcepub fn module_path(self, path: impl AsRef<Path>) -> Self
pub fn module_path(self, path: impl AsRef<Path>) -> Self
Sets the module path (--module-path).
Sourcepub fn add_opens(
self,
module: impl Into<String>,
package: impl Into<String>,
target: impl Into<String>,
) -> Self
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).
Sourcepub fn add_exports(
self,
module: impl Into<String>,
package: impl Into<String>,
target: impl Into<String>,
) -> Self
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).
Sourcepub fn system_property(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn system_property( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Sets a system property (-Dkey=value).
Sourcepub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
Sets an environment variable for the child Java process.
Sourcepub fn working_dir(self, path: impl AsRef<Path>) -> Self
pub fn working_dir(self, path: impl AsRef<Path>) -> Self
Sets the working directory for the child Java process.
Sourcepub fn timeout(self, duration: Duration) -> Self
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.
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.