1use crate::errors::{JavaRuntimeError, JavaRuntimeResult};
7use std::path::{Path, PathBuf};
8use std::process::Stdio;
9use tokio::process::{Child, Command};
10
11pub struct JavaRuntime(pub PathBuf);
13
14impl JavaRuntime {
15 pub fn new(path: PathBuf) -> Self {
17 Self(path)
18 }
19
20 pub async fn execute(&self, arguments: Vec<String>, game_dir: &Path) -> JavaRuntimeResult<Child> {
22 if !self.0.exists() {
23 return Err(JavaRuntimeError::NotFound {
24 path: self.0.clone(),
25 });
26 }
27
28 lighty_core::trace_debug!("Spawning Java process: {:?}", &self.0);
29
30 let mut command = Command::new(&self.0);
31 command
32 .current_dir(game_dir)
33 .args(arguments)
34 .stdin(Stdio::null())
35 .stdout(Stdio::piped())
36 .stderr(Stdio::piped());
37
38 #[cfg(windows)]
40 {
41 use std::os::windows::process::CommandExt;
42 const CREATE_NO_WINDOW: u32 = 0x08000000;
43 command.creation_flags(CREATE_NO_WINDOW);
44 }
45
46 let child = command.spawn()?;
47
48 lighty_core::trace_info!("Java process spawned successfully");
49 Ok(child)
50 }
51}