use crate::errors::{JavaRuntimeError, JavaRuntimeResult};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use tokio::process::{Child, Command};
pub struct JavaRuntime(pub PathBuf);
impl JavaRuntime {
pub fn new(path: PathBuf) -> Self {
Self(path)
}
pub async fn execute(&self, arguments: Vec<String>, game_dir: &Path) -> JavaRuntimeResult<Child> {
if !self.0.exists() {
return Err(JavaRuntimeError::NotFound {
path: self.0.clone(),
});
}
lighty_core::trace_debug!("Spawning Java process: {:?}", &self.0);
let mut command = Command::new(&self.0);
command
.current_dir(game_dir)
.args(arguments)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x08000000;
command.creation_flags(CREATE_NO_WINDOW);
}
let child = command.spawn()?;
lighty_core::trace_info!("Java process spawned successfully");
Ok(child)
}
}