首頁  >  文章  >  Java  >  Java程式開啟命令提示字元並插入命令

Java程式開啟命令提示字元並插入命令

WBOY
WBOY轉載
2023-08-19 12:29:051091瀏覽

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>
Java程式開啟命令提示字元並插入命令

方法一:透過使用更改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
Java程式開啟命令提示字元並插入命令

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
Java程式開啟命令提示字元並插入命令

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
Java程式開啟命令提示字元並插入命令

範例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 ...
Java程式開啟命令提示字元並插入命令

範例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 ...
Java程式開啟命令提示字元並插入命令

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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除