简体   繁体   English

Java中Process类的目的是什么?

[英]What is the purpose of Process class in Java?

Runtime objRuntime = Runtime.getRuntime();
String strBackupString = "mysqldump -u " + userName + " -p" + password + " " + dbName;
Process objProcess = objRuntime.exec(strBackupString);

This is used for backup of database. 这用于备份数据库。 But what exactly happens? 但究竟发生了什么? Can anybody make me explain, what is the purpose of Runtime and Process class? 任何人都可以让我解释一下, RuntimeProcess类的目的是什么?

Is this class used to act as if we are typing command from command prompt? 这个类用于表现我们是否从命令提示符输入命令? Then what should i pass to objRuntime.exec() if i want to open notepad? 那么如果我想打开记事本,我应该传递给objRuntime.exec() And is the command executed as soon as we call exec method? 我们调用exec方法时是否执行命令? If yes, then what purpose does Process serve here? 如果是,那么Process在这里服务的目的是什么? I really can't understand these two classes. 我真的无法理解这两个类。 Please make me understand. 请让我明白。 Thanks in advance :) 提前致谢 :)

Whenever in doubt, always consult the API: 如有疑问,请随时咨询API:

java.lang.Process

The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. ProcessBuilder.start()Runtime.exec方法创建本机进程并返回Process子类的实例,该实例可用于控制进程并获取有关它的信息。 The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process. Process类提供了从进程执行输入,执行输出到进程,等待进程完成,检查进程的退出状态以及销毁(杀死)进程的方法。

Runtime.exec(String command)

Executes the specified system command in a separate process. 在单独的进程中执行指定的系统命令。

So yes, Runtime.exec can execute a command that you'd usually type in the system command prompt. 所以,是的, Runtime.exec可以执行您通常在系统命令提示符中键入的命令。 This is hardly a platform-independent solution, but sometimes it's needed. 这不是一个独立于平台的解决方案,但有时需要它。 The returned Process object lets you control it, kill it, and importantly sometimes, redirect its standard input/output/error streams. 返回的Process对象允许您控制它,将其终止,重要的是,有时会重定向其标准输入/输出/错误流。

Related questions 相关问题

API links API链接


notepad.exe example notepad.exe示例

As mentioned before, this is platform dependent, but this snippet works on my Windows machine; 如前所述,这是依赖于平台的,但这个代码片段适用于我的Windows机器; it launches notepad.exe , and attempts to open test.txt from the current working directory. 它启动notepad.exe ,并尝试从当前工作目录中打开test.txt The program then waits for the process to terminate, and prints its exit code . 然后程序等待进程终止,并打印其退出代码

public class ExecExample {
    public static void main(String[] args) throws Exception {
        Process p = Runtime.getRuntime().exec("notepad.exe test.txt");      
        System.out.println("Waiting for notepad to exit...");
        System.out.println("Exited with code " + p.waitFor());
    }
}

It's an object-based representation of a process. 它是一个基于对象的过程表示。 Similar to the Thread class, which represents a thread. 类似于Thread类,它代表一个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM