class="MsoListParagraphCxSpMiddle">2,如果调用外部java服务,如何在销毁进程的同时关闭它开启的外部服务?
3,如何获得执行进程的PID?
?
?
1,如何执行,如何用ProcessBuilder来简化操作
在ProcessBuilder(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html)有个简单的例子。
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2"); Map<String, String> env = pb.environment(); env.put("VAR1", "myValue"); env.remove("OTHERVAR"); env.put("VAR2", env.get("VAR1") + "suffix"); pb.directory("myDir"); Process p = pb.start();
这个例子描述了如何执行一个命令,但是如果你想从命令行读取输入输出呢?
假设我们想执行如下命令
Ipconfig –all
我们可以做如下改进
写道 import java.io.BufferedReader;?
现在我们可以异步读取输出了,但其方式比较单一,我们可不可以直接输出到一个指定文件呢?
自jdk1.7起,ProcessBuilder增加Redirect来方便我们处理这个问题
1,输出有output, error两个流,输入有input流,相应的在这我们要创建3个文件
写道 File outputs = new File("C:/output.log");?
2创建input.txt文件,输入一下命令
Ipconfig –all
确定all后面有回车符
3 创建一个ProcessBuilder实例,指定“cmd”作为参数,表示我们想执行一个window命令
?
写道?
ProcessBuilder pb = newProcessBuilder("cmd");?
4 Redirect
输入输出流到不同的文件
?
?
?
5 最后,执行命令
?
写道?
pb.start();?
?
需要注意的是start必须在redirct设定后才调用
?
ProcessBuilder还有一些使用的方法
比如ProcessBuilder.Redirect.appendTo
这样每次输出到指定文件都追加到后面而不是覆盖
?
写道?
pb.redirectInput(ProcessBuilder.Redirect.appendTo( outputs ));?
?
比较有用的还有inheritIO(), 调用这个方法可以redirect被执行命令的输入输出流到当前java进程的输入输出流,也就是说,如果你用eclipse执行,你在console里就可以看到输入输出。
?
2,如果调用外部java服务,如何在销毁进程的同时关闭它开启的外部服务?有时候或许我们会执行一个外部的java应用
如:java –jar D:\\test.jar
假设test.jar 一旦启动会一直运行,那么问题来了,当process被销毁时,如何关闭这个外部进程呢?
有个bug专门描述了这个问题
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4770092
可以看出没有什么好的解决办法
但有时我们就想用这个功能,怎么办?
或许我们可以使用jdk自带的tools.jar
?
这个程序遍历虚拟机上所有的java程序,提取他们main class名,如果和指定的main class名匹配,则返回pid值
写道 import sun.jvmstat.monitor.HostIdentifier;?
?3, 如何获得执行进程的PID?
如果process因为未知的原因阻塞了,我们想直接kill它,如何获得该进程的pid呢?
在Unix,我们可以用反射,因为UnixProcess类有pid属性
写道 if(process.getClass().getName().equals("java.lang.UNIXProcess")) {在这里我们可以利用它的handle属性
首先下载jna.jar
写道 if (process.getClass().getName().equals("java.lang.Win32Process") ||?
写道 import com.sun.jna.Native;?*
?* This library is free software; you can redistribute it and/or
?* modify it under the terms of the GNU Lesser General Public
?* License as published by the Free Software Foundation; either
?* version 2.1 of the License, or (at your option) any later version.
?*
?* This library is distributed in the hope that it will be useful,
?* but WITHOUT ANY WARRANTY; without even the implied warranty of
?* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.? See the GNU
?* Lesser General Public License for more details.?
?*/
?
import java.util.HashMap;
import java.util.Map;
?
import com.sun.jna.FromNativeContext;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIFunctionMapper;
import com.sun.jna.win32.W32APITypeMapper;
?
/** Base type for most W32 API libraries.? Provides standard options
?* for unicode/ASCII mappings.? Set the system property w32.ascii
?* to true to default to the ASCII mappings.
?*/
public interface W32API extends StdCallLibrary, W32Errors {
???
??? /** Standard options to use the unicode version of a w32 API. */
??? Map UNICODE_OPTIONS = new HashMap() {
??????? {
??????????? put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
??????????? put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
??????? }
??? };
???
??? /** Standard options to use the ASCII/MBCS version of a w32 API. */
??? Map ASCII_OPTIONS = new HashMap() {
??????? {
??????????? put(OPTION_TYPE_MAPPER, W32APITypeMapper.ASCII);
??????????? put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.ASCII);
??????? }
??? };
?
??? Map DEFAULT_OPTIONS = Boolean.getBoolean("w32.ascii") ? ASCII_OPTIONS : UNICODE_OPTIONS;
???
??? public class HANDLE extends PointerType {
??? ???? @Override
??????? public Object fromNative(Object nativeValue, FromNativeContext context) {
??????????? Object o = super.fromNative(nativeValue, context);
??????????? if (INVALID_HANDLE_VALUE.equals(o))
??????????????? return INVALID_HANDLE_VALUE;
??????????? return o;
??????? }
??? }
?
??? /** Constant value representing an invalid HANDLE. */
??? HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
??????? { super.setPointer(Pointer.createConstant(-1)); }
??????? @Override
??????? public void setPointer(Pointer p) {
??????????? throw new UnsupportedOperationException("Immutable reference");
??????? }
??? };
}
/* Copyright (c) 2007 Timothy Wall, All Rights Reserved?*
?* This library is free software; you can redistribute it and/or
?* modify it under the terms of the GNU Lesser General Public
?* License as published by the Free Software Foundation; either
?* version 2.1 of the License, or (at your option) any later version.
?*
?
?* This library is distributed in the hope that it will be useful,
?* but WITHOUT ANY WARRANTY; without even the implied warranty of
?* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.? See the GNU
?* Lesser General Public License for more details.?
?*/
?
public interface W32Errors {
????????
??? int NO_ERROR?????????????? = 0;
??? int ERROR_INVALID_FUNCTION = 1;
??? int ERROR_FILE_NOT_FOUND?? = 2;
??? int ERROR_PATH_NOT_FOUND?? = 3;
?
}
? 还有一种方式,通过RuntimeMXBean 写道 RuntimeMXBean rtb = ManagementFactory.getRuntimeMXBean();相关信息还可以参照http://bugs.sun.com/view_bug.do?bug_id=4244896
?