Guest User

Untitled

a guest
Jun 7th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. ```
  2. @FXML
  3.     private void handleStartButton() {
  4.         String selectedProcess = selectProcessComboBox.getValue();
  5.         if (selectedProcess != null) {
  6.             try {
  7.                 // Construct the path to the selected process  JAR file
  8.                 String processPath = "/processes/" + processFileName;
  9.  
  10.                 // Get the input stream for the selected process JAR file
  11.                 InputStream processInputStream = getClass().getResourceAsStream(processPath);
  12.                 if (processInputStream == null) {
  13.                     log("Failed to locate process JAR file: " + processFileName);
  14.                     return;
  15.                 }
  16.  
  17.                 // Create a temporary file to store the process JAR contents
  18.                 File tempFile = File.createTempFile("Runner", ".jar");
  19.                 tempFile.deleteOnExit(); // Delete the temp file when the VM exits
  20.  
  21.                 // Write the process JAR contents to the temporary file
  22.                 try (OutputStream outputStream = new FileOutputStream(tempFile)) {
  23.                     byte[] buffer = new byte[1024];
  24.                     int bytesRead;
  25.                     while ((bytesRead = processInputStream.read(buffer)) != -1) {
  26.                         outputStream.write(buffer, 0, bytesRead);
  27.                     }
  28.                 }
  29.  
  30.                 // Start the process
  31.                 ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", tempFile.getAbsolutePath());
  32.                 Process processProcess = processBuilder.start();
  33.  
  34.                 // Create a separate thread to continuously read from the process's input stream
  35.                 new Thread(() -> {
  36.                     try (BufferedReader reader = new BufferedReader(new InputStreamReader(processProcess .getInputStream()))) {
  37.                         String inputLine;
  38.                         while ((inputLine = reader.readLine()) != null) {
  39.                             final String line = inputLine; // Declare line as final
  40.                             // Update the GUI with the output from the JAR file
  41.                             Platform.runLater(() -> logTextArea.appendText(line + "\n"));
  42.                         }
  43.                     } catch (IOException e) {
  44.                         log("Error reading output from process: " + e.getMessage());
  45.                     }
  46.                 }).start();
  47.  
  48.  
  49.                 log("Started process: " + selectedProcess );
  50.  
  51.                 // Optionally, store the process for later management
  52.                 currentProcess = processProcess;
  53.             } catch (IOException e) {
  54.                 log("Failed to start process: " + e.getMessage());
  55.             }
  56.         } else {
  57.             log("No process selected.");
  58.         }
  59.     }
  60. ```
Advertisement
Add Comment
Please, Sign In to add comment