Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Windows服务器 > » 正文

Java 获取系统软件安装列表

来源: foohsinglong 分享至:
Java获取系统软件安装列表
package com.kevin.demo;
/**
 * @author  <a href="mailto:foohsinglong@gmail.com">kevin.long</a>
 * @description Java获取系统软件安装列表,代码核心来自网上,主要通过Runtime实现,
 *  用JNI也行,解决乱码问题
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
 
public class SystemSoftware {  
	private JFrame f = new JFrame("本系统已经安装的软件列表");
	private JTextPane textPane = new JTextPane();
	private MyTable myTable=new MyTable();
	public static Charset charset = Charset.forName("GBK");
	public SystemSoftware() {
		f.setLocation(300, 200);
		f.setSize(800,500);
		JScrollPane jScrollPane = new JScrollPane(myTable.getTable());
		f.add(jScrollPane);
		f.setVisible(true);
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

		try {
			check();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void check() throws Exception {
		textPane.setText("您已经安装的软件:");
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		process = runtime
				.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
		BufferedReader in = new BufferedReader(new InputStreamReader(process
				.getInputStream(),"GBK"));
		String string = null;
		while ((string = in.readLine()) != null) {
			process = runtime.exec("cmd /c reg query " + string
					+ " /v DisplayName");
			BufferedReader name = new BufferedReader(new InputStreamReader(
					process.getInputStream(),"GBK"));
			String[] message = queryValue(string);
			if(message!=null) myTable.addRow(message);
			f.repaint();
		}
		in.close();
		process.destroy();

	}

	//具体查询每一个软件的详细信息
	private String[] queryValue(String string) throws IOException {
		String nameString = "";
		String versionString = "";
		
		String publisherString="";
		String uninstallPathString = "";
		
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		BufferedReader br = null;
		
		process = runtime.exec("cmd /c reg query " + string + " /v DisplayName");
		br = new BufferedReader(new InputStreamReader(process
				.getInputStream(),"GBK"));
		br.readLine();br.readLine();//去掉前两行无用信息
		if((nameString=br.readLine())!=null){
			nameString=nameString.replaceAll("DisplayName    REG_SZ    ", "");	//去掉无用信息
		}
		

		process = runtime.exec("cmd /c reg query " + string + " /v DisplayVersion");
		br = new BufferedReader(new InputStreamReader(process
				.getInputStream(),"GBK"));
		br.readLine();br.readLine();//去掉前两行无用信息
		if((versionString=br.readLine())!=null){
			versionString=versionString.replaceAll("DisplayVersion    REG_SZ    ", "");	//去掉无用信息
		}
		
		process = runtime.exec("cmd /c reg query " + string + " /v Publisher");
		br = new BufferedReader(new InputStreamReader(process
				.getInputStream(),"GBK"));
		br.readLine();br.readLine();//去掉前两行无用信息
		if((publisherString=br.readLine())!=null){
			publisherString =publisherString.replaceAll("Publisher    REG_SZ    ", "");	//去掉无用信息
		}
		
		process = runtime.exec("cmd /c reg query " + string + " /v UninstallString");
		br = new BufferedReader(new InputStreamReader(process
				.getInputStream(),"GBK"));
		br.readLine();br.readLine();//去掉前两行无用信息
		if((uninstallPathString=br.readLine())!=null){
			uninstallPathString=uninstallPathString.replaceAll("UninstallString    REG_SZ    ", "");	//去掉无用信息
		}
		
		String[] resultString=new String[4];
		resultString[0]= nameString ;//== null ? null : new String(nameString.getBytes(),"GB-2312");
		resultString[1]= versionString ;//== null ? null : new String(versionString.getBytes(),"GB-2312");
		resultString[2]= publisherString ;//== null ? null : new String(publisherString.getBytes(),"GB-2312");
		resultString[3]= uninstallPathString ;//== null ? null : new String(uninstallPathString.getBytes(),"GB-2312");
		if(resultString[0]==null) resultString=null;	//没有名字的不显示
		return resultString;
	}
	
	//列表
	private class MyTable{
		private JTable jTable;
		private Object[][] data=new Object[100][4];
		private Object[] colNames= { "软件名称","版本号","出版商","卸载路径"};
		private int p=-1;
		
		public MyTable(){
			
		}
		
		public void addRow(Object[] data){
			p++;
			if(p>=100) return ;
			this.data[p]=data;
		}
		
		
		public JTable getTable(){
			jTable=new JTable(data,colNames);
			return jTable;
		}
		
	}
	
	public static void main(String[] args) {
		new SystemSoftware();
	}
}  

Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史