首页 > Java > java教程 > 正文

Java程序打开命令提示符并插入命令

WBOY
发布: 2023-08-19 12:29:05
转载
1214人浏览过

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窗口中执行所选命令。

  • 第四步 − 查看结果。

    立即学习Java免费学习笔记(深入)”;

多种方法

对于这些程序,命令的选择是通过两种不同的方法来完成的。

  • 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.

MATLAB 函数帮助文档 中文WORD版
MATLAB 函数帮助文档 中文WORD版

函数是一组语句一起执行任务。在MATLAB中,函数定义在单独的文件。文件函数的文件名应该是相同的。 函数操作在自己的工作空间,它也被称为本地工作区,独立的工作区,在 MATLAB 命令提示符访问,这就是所谓的基础工作区的变量。函数可以接受多个输入参数和可能返回多个输出参数 。 MATLAB是MathWorks公司开发的一种编程语言。它最初是一个矩阵的编程语言,使线性代数编程很简单。它可以运行在交互式会话和作为批处理作业。有需要的朋友可以下载看看

MATLAB 函数帮助文档 中文WORD版 1
查看详情 MATLAB 函数帮助文档 中文WORD版

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 first set of commands changes the properties of the command window 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中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号