Skip to main content

lighty_java/
runtime.rs

1// Copyright (c) 2025 Hamadi
2// Licensed under the MIT License
3
4//! Java process execution wrapper.
5
6use crate::errors::{JavaRuntimeError, JavaRuntimeResult};
7use std::path::{Path, PathBuf};
8use std::process::Stdio;
9use tokio::process::{Child, Command};
10
11/// Wrapper around a Java binary path for process execution
12pub struct JavaRuntime(pub PathBuf);
13
14impl JavaRuntime {
15    /// Creates a new JavaRuntime from a binary path
16    pub fn new(path: PathBuf) -> Self {
17        Self(path)
18    }
19
20    /// Spawns a Java process with the given arguments in `game_dir`.
21    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        // On Windows, hide the console window
39        #[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}