Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- @FXML
- private void handleStartButton() {
- String selectedProcess = selectProcessComboBox.getValue();
- if (selectedProcess != null) {
- try {
- // Construct the path to the selected process JAR file
- String processPath = "/processes/" + processFileName;
- // Get the input stream for the selected process JAR file
- InputStream processInputStream = getClass().getResourceAsStream(processPath);
- if (processInputStream == null) {
- log("Failed to locate process JAR file: " + processFileName);
- return;
- }
- // Create a temporary file to store the process JAR contents
- File tempFile = File.createTempFile("Runner", ".jar");
- tempFile.deleteOnExit(); // Delete the temp file when the VM exits
- // Write the process JAR contents to the temporary file
- try (OutputStream outputStream = new FileOutputStream(tempFile)) {
- byte[] buffer = new byte[1024];
- int bytesRead;
- while ((bytesRead = processInputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- }
- }
- // Start the process
- ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", tempFile.getAbsolutePath());
- Process processProcess = processBuilder.start();
- // Create a separate thread to continuously read from the process's input stream
- new Thread(() -> {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(processProcess .getInputStream()))) {
- String inputLine;
- while ((inputLine = reader.readLine()) != null) {
- final String line = inputLine; // Declare line as final
- // Update the GUI with the output from the JAR file
- Platform.runLater(() -> logTextArea.appendText(line + "\n"));
- }
- } catch (IOException e) {
- log("Error reading output from process: " + e.getMessage());
- }
- }).start();
- log("Started process: " + selectedProcess );
- // Optionally, store the process for later management
- currentProcess = processProcess;
- } catch (IOException e) {
- log("Failed to start process: " + e.getMessage());
- }
- } else {
- log("No process selected.");
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment