二、API预览destroy()
杀掉子进程。
exitValue()
返回子进程的出口值。
InputStream getErrorStream()
获得子进程的错误流。
InputStream getInputStream()
获得子进程的输入流。
OutputStream getOutputStream()
获得子进程的输出流。
waitFor()
导致当前线程等待,如果必要,一直要等到由该 Process 对象表示的进程已经终止。
三、如何创建Process对象,一般有一下两种方法:
1、每个 ProcessBuilder 实例管理一个进程属性集。start() 方法利用这些属性创建一个新的 Process 实例。start() 方法可以从同一实例重复调用,以利用相同的或相关的属性创建新的子进程。(ProcessBuilder这个是JDK5中新添加的final类,详细请参看“深入研究java.lang.ProcessBuilder类”一文。
2、Runtime.exec() 方法创建一个本机进程,并返回 Process 子类的一个实例。详细请参看“深入研究java.lang.Runtime类”一文。
四、实例
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 控制台处理工具箱
*
* @author leizhimin 2009-6-25 14:12:14
*/
public final class CmdToolkit {
private static Log log = LogFactory.getLog(CmdToolkit.class);
private CmdToolkit() {
}
/**
* 读取控制命令的输出结果
*
* @param cmd命令
* @param isPrettify 返回的结果是否进行美化(换行),美化意味着换行,默认不进行美化,当此参数为null时也不美化,
* @return 控制命令的输出结果
* @throws IOException
*/
public static String readConsole(String cmd, Boolean isPrettify) throws IOException {
StringBuffer cmdout = new StringBuffer();
log.info("执行命令:" + cmd);
Process process = Runtime.getRuntime().exec(cmd); //执行一个系统命令
InputStream fis = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
if (isPrettify == null || isPrettify) {
while ((line = br.readLine()) != null) {
cmdout.append(line);
}
} else {
while ((line = br.readLine()) != null) {
cmdout.append(line).append(System.getProperty("line.separator"));
}
}
log.info("执行系统命令后的结果为:\n" + cmdout.toString());
return cmdout.toString().trim();
}
}
相关推荐
深入研究java.lang.Process类
NULL 博文链接:https://jameskaron.iteye.com/blog/2291495
一、问题描述 Android studio导入一个项目报一堆错误: ... Java.lang.ExceptionInInitializerError at com.werb.gankwithzhihu.ui.fragment.ZhihuFragment.createPresenter(ZhihuFragment.java:33)
AndroidRuntime: java.lang.UnsatisfiedLinkError: Couldn’t load airtalkee from loader dalvik.system.PathClassLoader[DexPathList[[zip file “/system/app/POCM3A.apk”, zip file “/data/data/com.cmccpo
2352): java.lang.RuntimeException: Unable to create service com.android.systemui.keyguard.KeyguardService: java.lang.SecurityException: read wallpaper: Neither user 10044 nor current process has ...
jmeter针对websocket压测的时候,会出现错误,该错误为插件的bug [Problems] - Unexpected error: null java.util.LinkedList$ListItr.checkForComodification(LinkedList....java.lang.Thread.run(Thread.java:744)
java.lang.SecurityException: class "org.apache.commons.collections.SequencedHashMap"'s signer information does not match signer information of other classes in the same package at java.lang....
nested exception is java.lang.OutOfMemoryError: Java heap space org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Java heap...
at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186) at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323) at org.apache.axis.strategies....
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException 解决这个问题
二 Exception in thread "main" java.lang.RuntimeException: Your license has expired. Please registe ...,Your key is @2AC*1A97~C@6DCF6B8FEFE03@6B4C&CCAFF92 需要key的 可以去这弄...
所有的checked exception是从java.lang.Exception类衍生出来的,而runtime exception则是从java.lang.RuntimeException或java.lang.Error类衍生出来的。 它们的不同之处表现在两方面:机制上和逻辑上。 一...
12.4 java.lang.Math类 172 12.5 java.Math.BigInteger类 174 12.6 java.Math.BigDecimal类 176 12.7 巩固练习 177 第13章 Java输入与输出(I/O) 179 13.1 File类 179 13.2 流(stream) 182 13.2.1 ...
OutOfMemoryError的8种经典案例,Java heap space、GC overhead limit exceeded、Permgen space、Metaspace、Unable to create new native thread、Out of swap space?、Requested array size exceeds VM limit、...
它通过java.lang.Process与 ffmpeg 集成。 灵感来自 在以下人员的帮助下进行了测试 操作系统:Ubuntu、MacOS、Windows JDK : 8, 11, 14 用法 < dependency > < groupId >com.github.kokorin.jaffree</ ...
java.lang.Runtime rt = Runtime.getRuntime(); //命令 String command = fileUrl+"ffmpeg -i " + localPath + " " + targetFilePath; //执行amr转MP3命令 Process proc = rt.exec(command); InputStream ...
通过使用单个类ProcessExecutor ,用户可以从java.lang.ProcessBuilder和获得功能。 安装 项目工件可在。 要将其包含在您的Maven项目中,则必须指定依赖项。 ... < groupId>org.zeroturnaround < artifactId>...
SampleCreateNewProcess.cs: Create a new process. (Example for: java.lang.Runtime) SampleSimpleHttpServer.cs: Create a simple multi threaded server. (Example for: Thread, Runnable, Socket, ServerSocket...
2011-1-8 20:34:20 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet default threw exception java.lang.IllegalStateException at org.apache.catalina.connector....
import java.lang.*; public class util extends Object { public static int RunThis(String args) { Runtime rt = Runtime.getRuntime(); int RC = -1; try { Process p = rt.exec(args); int bufSize = 4096; ...