Java程式開啟命令提示字元並插入命令
This article uses various approaches for selecting the commands inserted in the opened command window through the Java code. The command window is opened by using ‘cmd’. Here, the methods of doing the same are specified using Java code. The Command window is first opened using the Java program. It is opened as a child process. If the java program is run in an existing cmd window, another one is opened as a new process. Further, different types of commands are inserted and executed in that opened command window 透過Java程式碼。
這些程式可能在線上程式設計環境中無法運作。如何使用javac和java命令運行這些程式的詳細資訊在本文中的輸出部分中詳細說明。
演算法
步驟 1 − 使用 Java 程式碼開啟 CMD 視窗。
Step 2 − Select the command to be executed. The selected command is used as a text String.
#第三步驟 - 透過Java程式在開啟的CMD視窗中執行所選指令。
第四步 − 查看結果。
多種方法
對於這些程序,命令的選擇是透過兩種不同的方法來完成的。
By Using Commands that change the property of the cmd window.
By Using Executable Commands
#Let’s see the program along with its output one by one.
First, the java code is given to start the new CMD window.
Java Code (to start the new CMD window)
public class cmdprog1 { public static void main(String[] args) { System.out.println("Opening cmd window"); try{ // cmd is a command that opens the command window //CMD /C is used to run commands and then terminate the existing window while CMD /K will run the command and then it returns you to the given prompt. Runtime.getRuntime().exec(new String[] {"cmd", "/K", "Start"}); // the following line can also be used..... //Runtime.getRuntime().exec(new String[] {"cmd", "/C", "Start"}); } catch (Exception e){ System.out.println("Error: " + e); } } }
輸出
C:\java\javaprgstu>javac cmdprog1.java C:\java\javaprgstu>java cmdprog1 Opening cmd window C:\java\javaprgstu>

方法一:透過使用更改cmd視窗屬性的命令。
在這種方法中,使用了兩個不同的範例。
範例1:在開啟CMD視窗時更改標題。
範例2:在開啟CMD視窗時變更背景和前景顏色。
範例1:在開啟CMD視窗時更改標題。
Program
public class cmdprog22 { public static void main(String[] args) { String command_to_playwith =" title 'The New Title of the New Command Window' "; System.out.println("Opening cmd window"); try { String command = "cmd /c" + " start" + command_to_playwith; //Starting the new child process Process childprocess11 = Runtime.getRuntime().exec(command); System.out.println("The child process is Alive: " + childprocess11.isAlive()); System.out.println(); } catch (Exception e){ System.out.println("Error: " + e); } } }
輸出
C:\java\javaprgstu>javac cmdprog22.java C:\java\javaprgstu>java cmdprog22 Opening cmd window The child process is Alive: true

Example 2: Changes the background and foreground color of the CMD window while opening it.
public class cmdprog55 { public static void main(String[] args) { //the following command will change the color of the cmd window. First the number for bg color and then the number for fg color is added. // 4 means red color and 0 means black color String command_to_playwith =" COLOR 40"; System.out.println("Opening cmd window"); try { String command = "cmd /c" + " start" + command_to_playwith; // starting the child process .... Process childprocess11 = Runtime.getRuntime().exec(command); System.out.println("The child process is Alive: " + childprocess11.isAlive()); System.out.println(); } catch (Exception e){ System.out.println("Error: " + e); } } }
Output (Approach 1: Example 2)
C:\java\javaprgstu>javac cmdprog55.java C:\java\javaprgstu>java cmdprog55 Opening cmd window The child process is Alive: true

Approach-2: By Using the executable commands
新的cmd視窗作為子進程開啟。插入的命令結果只能在新的cmd視窗中看到。在這種方法中,使用了三個不同的範例。
範例1:在開啟的CMD視窗中顯示訊息。
Example 2 Show the contents of a txt file.
範例3:以寬格式顯示資料夾內容。
範例1:在開啟的CMD視窗中顯示訊息。
public class cmdprog44 { public static void main(String[] args) { // The following command will display the message specified. String command_to_playwith =" ECHO 'Hi! Lets check the cmd commands ....'"; System.out.println("Opening cmd window"); try { String command = "cmd /c" + " start" + command_to_playwith; // starting the child process.... Process childprocess11 = Runtime.getRuntime().exec(command); System.out.println("The child process is Alive: " + childprocess11.isAlive()); System.out.println(); } catch (Exception e){ System.out.println("Error: " + e); } } } System.out.println("Opening cmd window"); try { String command = "cmd /c" + " start" + command_to_playwith; // starting the child process .... Process childprocess11 = Runtime.getRuntime().exec(command); System.out.println("The child process is Alive: " + childprocess11.isAlive()); System.out.println(); } catch (Exception e){ System.out.println("Error: " + e); }
Output (Approach 2: Example 1)
C:\java\javaprgstu>javac cmdprog44.java C:\java\javaprgstu>java cmdprog44 Opening cmd window The child process is Alive: true

範例2:顯示txt檔案的內容。
public class cmdprog33 { public static void main(String[] args) { //The following command is the command that is needed to see the contents of the given text file String command_to_playwith =" TYPE testfile.txt"; System.out.println("Opening cmd window"); try { String command = "cmd /c" + " start" + command_to_playwith; //Starting the new child process Process childprocess11 = Runtime.getRuntime().exec(command); System.out.println("The child process is Alive: " + childprocess11.isAlive()); System.out.println(" Now showing the content of testfile.txt ....\n"); } catch (Exception e){ System.out.println("Error: " + e); } } }
輸出
C:\java\javaprgstu>javac cmdprog33.java C:\java\javaprgstu>java cmdprog33 Opening cmd window The child process is Alive: true Now showing the content of testfile.txt ...

範例3:以寬格式顯示資料夾內容。
public class cmdprog66 { public static void main(String[] args) { // The following command will display the specified directory in wide format String command_to_playwith =" dir .\applettest /W"; System.out.println("Opening cmd window"); try { String command = "cmd /c" + " start" + command_to_playwith; //Starting the new child process Process childprocess11 = Runtime.getRuntime().exec(command); System.out.println("The child process is Alive: " + childprocess11.isAlive()); System.out.println(" Now showing the directory in wide format ....\n"); } catch (Exception e){ System.out.println("Error: " + e); } } }
輸出
C:\java\javaprgstu>javac cmdprog66.java C:\java\javaprgstu>java cmdprog66 Opening cmd window The child process is Alive: true Now showing the directory in wide format ...

Conclusion
In this article, we explored different commands to be inserted into the cmd window after opening it through the java program. The selection of commands is done based on the different categories The selection of commands is done setd on the different categories. The first of mandscom the sations the dowcom while opening it and the second set of commands is used to show results in the opened command window after it is displayed. In both cases, the new cmd window is opened as a child process.
以上是Java程式開啟命令提示字元並插入命令的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Windows11能夠運行大量文件類型,無論是否有外部第三方應用程式。它不僅允許您在舒適的PC上執行大量任務,而且還確保您可以利用PC的原始功能。今天,我們將了解一種複雜的文件類型—jar—並告訴您如何在Windows11或Windows10PC上開啟它。什麼是jar檔? jar是一種歸檔包格式,可能包含或不包含可執行的Java程式。這些檔案可以包含java應用程式或原始程式碼,然後可用於編譯和運行應用程序,而無需為每個應用程式編寫單獨的程式碼。您可以透過各種方式

什麼是CAB檔案? CAB檔案的副檔名為.cab,是WindowsCabinet檔案的縮寫。這是一種壓縮文件,通常用於壓縮設備驅動程式或更新文件等軟體安裝包。 CAB文件支援無損壓縮,這使得該格式非常適合壓縮文件,其中文件可以準確提取至關重要,例如驅動程式和其他更新。如何使用命令提示字元在Windows11上安裝CAB檔案在Windows11上安裝CAB檔案有多種方法。其中一種方法是使用命令提示字元來提取和安裝檔案。您也可以使用較新的WindowsPowerShell

線上FPS遊戲中作弊一直是個大問題,即使Valorant不存在。它會破壞遊戲體驗,降低玩家對遊戲的興趣。 Valorant從其初期階段開始就試圖透過自己的RiotVanguard保護系統來克服這一不足。安裝遊戲一次就需要重新啟動系統,完全正常,Vanguard系統會自動啟動。但是,如果您重新啟動系統後仍然看到“您的遊戲需要重新啟動系統才能玩。請重新啟動您的電腦。”在首頁留言?這個問題很多用戶都遇過,不用擔心。請遵循這些修復程序以獲得快速解決方案。修復1–不要退出先鋒重新啟動電腦後

一些用戶在嘗試在其係統上運行應用程式時遇到錯誤“應用程式錯誤0xc0000906”,他們將無法繼續。系統上的單一應用程式或多個應用程式可能會遇到此錯誤。這可能是由於檔案損壞、快取問題、使用可能會阻止軟體應用程式的第三方防毒軟體等。在本文中,我們有一些解決方案可以幫助使用者消除錯誤。嘗試執行命令來掃描系統文件,並停用防毒軟體,如下所述。所以讓我們開始吧!方法1:執行SFC和DISM掃描步驟1–以管理者身分開啟命令提示字元。為此,請在視窗搜尋列中鍵入cmd,然後同時按住ctrl+shift鍵並按回車

Windows透過為它們分配優先級,在將系統資源分配給最需要它的進程和程序方面做得非常好。您安裝的大多數應用程式都可以在預設的「正常」優先權等級下完美運作。 然而,有時,您可能需要以高於預設正常水平的水平運行程序,例如游戲,以提高它的性能。但這樣做是有代價的,而且是一筆值得深思的交易。 當您將應用程式設定為高優先級時會發生什麼? Windows執行不同的進程和程式時總共有六個優先權——低、低於正常、正常、高於正常、高和即時。 Windows將根據它們的優先順序對應用程式進行排名和排隊。優先權越高,應用

System32資料夾是什麼? System32是Windows使用的主要資料夾之一。在Windows安裝期間,對Windows正常運作至關重要的所有必要檔案和資料夾都會複製到此資料夾中。其中包括重要的系統檔案、Windows實用程式使用的相關執行檔、動態連結庫(DLL),甚至一些軟體檔案都複製到此資料夾中。不過,不要被System32這個名字迷惑。這對於32位元和64位元電腦都是如此。在64位元電腦中,System32資料夾託管64位元文件,而32位元文件位於

我們的許多讀者在嘗試將電腦連接到組織的伺服器時0xC004C020報告了錯誤。此錯誤會阻止啟動其Windows作業系統。雖然錯誤可能令人沮喪,但我們將在嘗試在組織連結的電腦上啟動Windows時指導您解決錯誤0xC004C020。導致錯誤0xC004C020的原因是什麼?如果您嘗試在與組織連結的電腦上啟動Windows並遇到錯誤0xC004C020,則可能的原因可能如下所示:非MAK金鑰–如果在組織連結的電腦上使用非MAK金鑰,則組織的策略將不允許激活它。格式化後密鑰存取丟

對於Windows用戶來說,沒有什麼比必須面對藍色畫面錯誤更煩人的了,尤其是伴隨著系統崩潰的錯誤。 srttrail.txt錯誤就是其中之一。雖然從技術上講不是BSOD,但自動修復環境中的錯誤仍然是導致Windows脫軌的更深層問題的症狀,需要介入。什麼是srttrail.txt錯誤?訊息中提到的srttrail.txt文字檔案只是Windows在無法正常啟動時維護的所有實例的日誌,如果Windows在啟動時卡住,它將繼續出現。此錯誤訊息主要發生在系統啟動時,但也可能在Windo
