---
title: "在Java中执行命令行程序"
author: "samjavaeye"
date: "2009-04-06T14:37:48Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-01-18T13:31:10Z"
id: 362226
url: "https://samjavaeye.iteye.com/blog/362226"
---

```
String commandLine = "explorer.exe C:\\";
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec(commandLine);
int code = process.waitFor();
```

第1行表示要打开资源管理器，并定位到C盘根目录。

第4行，process.waitFor()表示当前线程等待process这个线程执行完毕，再开始往下执行。该方法返回0时表示正常终止。