java Java中Process类的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2798425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:53:40  来源:igfitidea点击:

What is the purpose of Process class in Java?

javaruntime.exec

提问by TCM

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 Runtimeand Processclass?

这用于备份数据库。但究竟发生了什么?任何人可以让我解释一下,什么是目的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? And is the command executed as soon as we call exec method? If yes, then what purpose does Processserve here? I really can't understand these two classes. Please make me understand. Thanks in advance :)

此类是否用于充当我们从命令提示符键入命令的行为?那么objRuntime.exec()如果我想打开记事本,我应该传递给什么?并且是一调用exec方法就执行命令吗?如果是,那么Process这里的目的是什么?这两个类我真的看不懂。请让我明白。提前致谢 :)

回答by polygenelubricants

Whenever in doubt, always consult the API:

如有疑问,请务必查阅 API:

java.lang.Process

The ProcessBuilder.start()and Runtime.execmethods create a native process and return an instance of a subclass of Processthat can be used to control the process and obtain information about it. The class Processprovides 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.

Runtime.exec(String command)

Executes the specified system command in a separate process.

java.lang.Process

ProcessBuilder.start()Runtime.exec方法创建一个本机进程,并返回的子类的实例Process可以用来控制过程,并获得相关信息。该类Process提供了执行进程输入、执行输出到进程、等待进程完成、检查进程退出状态以及销毁(杀死)进程的方法。

Runtime.exec(String command)

在单独的进程中执行指定的系统命令。

So yes, Runtime.execcan execute a command that you'd usually type in the system command prompt. This is hardly a platform-independent solution, but sometimes it's needed. The returned Processobject lets you control it, kill it, and importantly sometimes, redirect its standard input/output/error streams.

所以是的,Runtime.exec可以执行您通常在系统命令提示符中键入的命令。这几乎不是一个独立于平台的解决方案,但有时是需要的。返回的Process对象可以让你控制它,杀死它,重要的是有时,重定向它的标准输入/输出/错误流。

Related questions

相关问题

API links

接口链接



notepad.exe example

记事本.exe示例

As mentioned before, this is platform dependent, but this snippet works on my Windows machine; it launches notepad.exe, and attempts to open test.txtfrom the current working directory. The program then waits for the process to terminate, and prints its exit code.

如前所述,这与平台有关,但此代码段适用于我的 Windows 机器;它启动notepad.exe,并尝试test.txt从当前工作目录打开。然后程序等待进程终止,并打印其退出代码

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());
    }
}

回答by someguy

It's an object-based representation of a process. Similar to the Thread class, which represents a thread.

它是过程的基于对象的表示。类似于Thread类,代表一个线程。