在Java GUI 執行外部JAR
問題:
問題:答案:
<code class="java">// Import necessary libraries import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; import java.lang.Process; import java.lang.Runtime; // Method to execute a JAR file public void executeJar(String jarPath) { try { // Execute the JAR in a separate process Process proc = Runtime.getRuntime().exec("java -jar " + jarPath); // Get the process input and error streams InputStream in = proc.getInputStream(); InputStream err = proc.getErrorStream(); // Use buffered readers to read the output efficiently BufferedReader inReader = new BufferedReader(new InputStreamReader(in)); BufferedReader errReader = new BufferedReader(new InputStreamReader(err)); // Display the output in the GUI while (inReader.ready()) { String line = inReader.readLine(); // Display the line in the GUI using appropriate method } while (errReader.ready()) { String line = errReader.readLine(); // Display the line in the GUI using appropriate method } // Close the streams inReader.close(); errReader.close(); } catch (IOException e) { // Handle the IO exception and display an error message in the GUI } }</code>
要在Java GUI 應用程式的單獨進程中執行外部JAR 文件,請按照以下步驟操作:
點選按鈕時呼叫executeJar() 方法,傳遞對應的JAR 檔案路徑。這將執行 JAR 檔案並在 GUI 中顯示其輸出。以上是如何從 Java GUI 執行外部 JAR 檔案並顯示進度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!