java怎么获取当前windows进程pid呢

小昭归来 2013-10-28 09:18:11
亲 我用这个 Processexec=Runtime.getRuntime().exec("cmd /c " + filePath); 打开了一个excel文件,然后再通过pid关闭进程,怎么怎么获取这个文件的PID呢,有木有现成的函数可用?
亲亲 求段代码
...全文
1004 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
小昭归来 2013-10-28
  • 打赏
  • 举报
回复
引用 8 楼 huxiweng 的回复:
[quote=引用 7 楼 u010186028 的回复:] [quote=引用 3 楼 huxiweng 的回复:]

public class PidTest {

	public static void main(String[] args) throws IOException,
			InterruptedException {

		Runtime rt = Runtime.getRuntime();
		rt.exec("cmd  /c C:/Users/eacfgjl/Desktop/MABEZ/EP2/regression_ST_test_spec.xls");
		Thread.sleep(3000);

		String processName = null;
		String cmd = "tasklist /nh /FI \"IMAGENAME eq EXCEL.EXE\"";
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec(cmd);
		BufferedReader br = new BufferedReader(new InputStreamReader(
				process.getInputStream()));
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
			if (line.indexOf("EXCEL.EXE") != -1) {
				String[] lineArray = line.split(" ");
				processName = lineArray[0].trim();
				System.out.println(processName);
				Runtime.getRuntime().exec("taskkill /IM " + processName);
			}
		}
	}

}

亲 我刚才没有说明白遇到的问题,我的电脑上打开了两张excel表,然后想通过命令关闭其中一个。我原以为一个excel会有一个pid,然后通过指定的PID关闭某一个,但是后来发现只有一个pid,这种情况该怎么实现呢?用您的代码一下就把所以的打开着的都关闭了[/quote] 不能先打开一张表,数据取出来保存,用完关掉,再开第二个?[/quote] 那我这也行不呢?在excel里面写一段VBA,在excel打开之后,然后通过里面的VBA操作来关闭excel
小昭归来 2013-10-28
  • 打赏
  • 举报
回复
引用 8 楼 huxiweng 的回复:
[quote=引用 7 楼 u010186028 的回复:] [quote=引用 3 楼 huxiweng 的回复:]

public class PidTest {

	public static void main(String[] args) throws IOException,
			InterruptedException {

		Runtime rt = Runtime.getRuntime();
		rt.exec("cmd  /c C:/Users/eacfgjl/Desktop/MABEZ/EP2/regression_ST_test_spec.xls");
		Thread.sleep(3000);

		String processName = null;
		String cmd = "tasklist /nh /FI \"IMAGENAME eq EXCEL.EXE\"";
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec(cmd);
		BufferedReader br = new BufferedReader(new InputStreamReader(
				process.getInputStream()));
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
			if (line.indexOf("EXCEL.EXE") != -1) {
				String[] lineArray = line.split(" ");
				processName = lineArray[0].trim();
				System.out.println(processName);
				Runtime.getRuntime().exec("taskkill /IM " + processName);
			}
		}
	}

}

亲 我刚才没有说明白遇到的问题,我的电脑上打开了两张excel表,然后想通过命令关闭其中一个。我原以为一个excel会有一个pid,然后通过指定的PID关闭某一个,但是后来发现只有一个pid,这种情况该怎么实现呢?用您的代码一下就把所以的打开着的都关闭了[/quote] 不能先打开一张表,数据取出来保存,用完关掉,再开第二个?[/quote] 因为有多个人访问不同的表,所以不能这样吧,除非控制他们,让其在访问的时候进行排队,那样并发量只能为1了,如果打开关闭操作速度快可以,否则就不得行了。我还是想针对某个特定的表进行关闭
teemai 2013-10-28
  • 打赏
  • 举报
回复
引用 7 楼 u010186028 的回复:
[quote=引用 3 楼 huxiweng 的回复:]

public class PidTest {

	public static void main(String[] args) throws IOException,
			InterruptedException {

		Runtime rt = Runtime.getRuntime();
		rt.exec("cmd  /c C:/Users/eacfgjl/Desktop/MABEZ/EP2/regression_ST_test_spec.xls");
		Thread.sleep(3000);

		String processName = null;
		String cmd = "tasklist /nh /FI \"IMAGENAME eq EXCEL.EXE\"";
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec(cmd);
		BufferedReader br = new BufferedReader(new InputStreamReader(
				process.getInputStream()));
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
			if (line.indexOf("EXCEL.EXE") != -1) {
				String[] lineArray = line.split(" ");
				processName = lineArray[0].trim();
				System.out.println(processName);
				Runtime.getRuntime().exec("taskkill /IM " + processName);
			}
		}
	}

}

亲 我刚才没有说明白遇到的问题,我的电脑上打开了两张excel表,然后想通过命令关闭其中一个。我原以为一个excel会有一个pid,然后通过指定的PID关闭某一个,但是后来发现只有一个pid,这种情况该怎么实现呢?用您的代码一下就把所以的打开着的都关闭了[/quote] 不能先打开一张表,数据取出来保存,用完关掉,再开第二个?
小昭归来 2013-10-28
  • 打赏
  • 举报
回复
引用 3 楼 huxiweng 的回复:

public class PidTest {

	public static void main(String[] args) throws IOException,
			InterruptedException {

		Runtime rt = Runtime.getRuntime();
		rt.exec("cmd  /c C:/Users/eacfgjl/Desktop/MABEZ/EP2/regression_ST_test_spec.xls");
		Thread.sleep(3000);

		String processName = null;
		String cmd = "tasklist /nh /FI \"IMAGENAME eq EXCEL.EXE\"";
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec(cmd);
		BufferedReader br = new BufferedReader(new InputStreamReader(
				process.getInputStream()));
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
			if (line.indexOf("EXCEL.EXE") != -1) {
				String[] lineArray = line.split(" ");
				processName = lineArray[0].trim();
				System.out.println(processName);
				Runtime.getRuntime().exec("taskkill /IM " + processName);
			}
		}
	}

}

亲 我刚才没有说明白遇到的问题,我的电脑上打开了两张excel表,然后想通过命令关闭其中一个。我原以为一个excel会有一个pid,然后通过指定的PID关闭某一个,但是后来发现只有一个pid,这种情况该怎么实现呢?用您的代码一下就把所以的打开着的都关闭了
小昭归来 2013-10-28
  • 打赏
  • 举报
回复
引用 4 楼 u010186028 的回复:
[quote=引用 2 楼 suciver 的回复:] 用dos的tasklis命令用-fi筛选出进程名为excel.exe

Process p=Runtime.getRuntime().exec("cmd /c tasklist -fi \"imagename eq excel.exe\"");
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
String line=null;
while((line=reader.readLine())!=null){
	System.out.println(line);
}
reader.close();
亲 我照着你的代码写进去了,excel打开了,但是控制台没有输出任何信息,当我把excel关闭后会输出“no”;junittest在excel关闭之前一直是在运行中的,当关闭excel后才会显示运行成功,该怎么修改呢。我的目的就是打开指定的excel后然后通过pid再关闭掉。[/quote] 不好意思弄错了
suciver 2013-10-28
  • 打赏
  • 举报
回复
引用 4 楼 u010186028 的回复:
[quote=引用 2 楼 suciver 的回复:] 用dos的tasklis命令用-fi筛选出进程名为excel.exe

Process p=Runtime.getRuntime().exec("cmd /c tasklist -fi \"imagename eq excel.exe\"");
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
String line=null;
while((line=reader.readLine())!=null){
	System.out.println(line);
}
reader.close();
亲 我照着你的代码写进去了,excel打开了,但是控制台没有输出任何信息,当我把excel关闭后会输出“no”;junittest在excel关闭之前一直是在运行中的,当关闭excel后才会显示运行成功,该怎么修改呢。我的目的就是打开指定的excel后然后通过pid再关闭掉。[/quote] 我这个tasklist命令是在excel已经打开的时候也就是获取进程的时候才显示的。打开一个excel文件是不会返回什么信息的。

Process p=Runtime.getRuntime().exec("cmd /c "+filePath);
Thread.sleep(3000);
p=Runtime.getRuntime().exec("cmd /c tasklist -fi \"imagename eq excel.exe\"");
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
String line=null;
while((line=reader.readLine())!=null){
   System.out.println(line);
}
reader.close();
小昭归来 2013-10-28
  • 打赏
  • 举报
回复
引用 2 楼 suciver 的回复:
用dos的tasklis命令用-fi筛选出进程名为excel.exe

Process p=Runtime.getRuntime().exec("cmd /c tasklist -fi \"imagename eq excel.exe\"");
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
String line=null;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();


亲 我照着你的代码写进去了,excel打开了,但是控制台没有输出任何信息,当我把excel关闭后会输出“no”;junittest在excel关闭之前一直是在运行中的,当关闭excel后才会显示运行成功,该怎么修改呢。我的目的就是打开指定的excel后然后通过pid再关闭掉。
teemai 2013-10-28
  • 打赏
  • 举报
回复

public class PidTest {

	public static void main(String[] args) throws IOException,
			InterruptedException {

		Runtime rt = Runtime.getRuntime();
		rt.exec("cmd  /c C:/Users/eacfgjl/Desktop/MABEZ/EP2/regression_ST_test_spec.xls");
		Thread.sleep(3000);

		String processName = null;
		String cmd = "tasklist /nh /FI \"IMAGENAME eq EXCEL.EXE\"";
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec(cmd);
		BufferedReader br = new BufferedReader(new InputStreamReader(
				process.getInputStream()));
		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
			if (line.indexOf("EXCEL.EXE") != -1) {
				String[] lineArray = line.split(" ");
				processName = lineArray[0].trim();
				System.out.println(processName);
				Runtime.getRuntime().exec("taskkill /IM " + processName);
			}
		}
	}

}

suciver 2013-10-28
  • 打赏
  • 举报
回复
用dos的tasklis命令用-fi筛选出进程名为excel.exe

Process p=Runtime.getRuntime().exec("cmd /c tasklist -fi \"imagename eq excel.exe\"");
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
String line=null;
while((line=reader.readLine())!=null){
	System.out.println(line);
}
reader.close();
失落夏天 2013-10-28
  • 打赏
  • 举报
回复
一样吧。 使用的是 Processexec=Runtime.getRuntime().exec方法 执行的命令式 ps -ef

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧