java实现windows下客户端软件的下载安装和检测
2013-10-15 11:25
459 查看
最近我们有个客户端,需要在不同os系统上安装客户端程序并实时检测其进程是否OK,整体的设计思路,由jenkins的分布式调度策略来分发在不同机器上执行下载安装和检测程序,如图:
下面是java实现windows exe文件的下载、安装、进程检测。贴一段代码如下:
下面是java实现windows exe文件的下载、安装、进程检测。贴一段代码如下:
private final static String OS_NAME="os.name"; private final static String WINDOWS_OS="windows"; private final static String LINUX_OS="linux"; private final static String windows_download_url="xxx"; private final static String linux_download_url="xxx";
private final static String window_check_pro_Ali_update="xxe.exe";
private final static String window_check_pro_AliYunDun="xxx.exe"; /**1、先判断是windows还是linux。 * 2、下载和安装。如果是windows。放入C盘 temp安装下,并执行安装程序,如果是linux,放入当前用户新建一个temp安装目录,并执行安装程序 * 3、检查进程是否起来,windows下采用的是tasklist(xxx.exe,等待一分钟,xxx.exe进程)。linux下采用的是top,断言进程是否存在(xxx 等待一分钟,xxxi) * * * @param args */ /** * os.name 操作系统的名称 os.arch 作系统的架构 os.version 操作系统的版本 user.name 用户的账户名称 */ String judge_os_type(){ String osName=null; try{ osName=System.getProperty(OS_NAME); if(null!=osName&&osName.indexOf(WINDOWS_OS)<0){ osName=WINDOWS_OS; }else{ osName=LINUX_OS; } }catch (Exception e) { System.out.println("error============"+e); } //取操作系统的相关信息放入数据库,错误信息详细定位 try { InetAddress addr = InetAddress.getLocalHost(); String ip=addr.getHostAddress().toString();//获得本机IP String address=addr.getHostName().toString();//获得本机名称 // System.out.println( "IP:"+ ip+",address:"+address); }catch(Exception e) { // System.out.println("Bad IP Address!"+e); } return osName; } /** * 下载并安装控件 * @param osType * @throws IOException */ void download_install(String osType,String filedirPath) { //下载安装程序 String exeNameAndpath=filedirPath+"\\xxx.exe";//下载文件存放路径 File dirFire=new File(filedirPath); File exeFile=new File(exeNameAndpath); try{ //判断文件夹是否存在和文件是否存在; //如果文件存在,则删除 if(!dirFire.exists()){ dirFire.mkdir(); } if(exeFile.exists()){ exeFile.delete(); } URL url = new URL(windows_download_url); URLConnection connect=url.openConnection(); InputStream in=connect.getInputStream(); FileOutputStream fos=new FileOutputStream(exeNameAndpath); byte[] buffer=new byte[4*1024]; int read; while ((read = in.read(buffer)) > 0) { fos.write(buffer, 0, read); } fos.close(); in.close(); }catch(IOException e){ e.printStackTrace(); } //安装程序 Runtime run=Runtime.getRuntime(); try{ Process pro=run.exec(exeNameAndpath); System.out.println("安装完毕"); }catch (Exception e){ e.printStackTrace(); } //pro.destroy();销毁开启的进程 //check一下 需要在前面sleep try { Thread.sleep(1000*10); boolean ali_update_exit =check_pro_for_windows(window_check_pro_Ali_update); if(ali_update_exit==true){ System.out.println(window_check_pro_Ali_update+" 该进程存在 "); }else{ System.out.println(window_check_pro_Ali_update+" 该进程不存在 "); } Thread.sleep(1000*50); boolean aliYunDun_exit=check_pro_for_windows(window_check_pro_AliYunDun); if(aliYunDun_exit==true){ System.out.println(window_check_pro_AliYunDun+" 该进程存在 "); }else{ System.out.println(window_check_pro_AliYunDun+" 该进程不存在 "); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 根据进程名检查进程是否存在 * @return */ boolean check_pro_for_windows(String checkProcessName){ boolean exit=false; Process process=null; try{ //利用tasklist查看启用的进程 process=Runtime.getRuntime().exec("cmd.exe /c tasklist"); BufferedReader input=new BufferedReader(new InputStreamReader(process.getInputStream())); String line=""; while((line=input.readLine())!=null){ if(true==line.contains(checkProcessName)){ exit=true; break; } System.out.println(line); } input.close(); }catch (Exception e) { exit=false; e.printStackTrace(); } return exit; }
相关文章推荐
- 为了在本机上测试,必须先安装memcache的服务器端,安装文件见附件,只能在Administrator用户下才能启动memcached服务。 首先要下载memcache的客户端java实现的jar
- Android客户端apk自动检测更新自动下载自动安装的实现方法
- JDK下载,安装,写第一个简单的Java程序(windows)
- windows 下调用command line(CMD)检查是否安装java(c#实现)
- windows下git客户端的下载安装及使用教程
- 轻量级、全功能单文件火狐无缝翻越(自解压版不用loader实现便捷安装,并可设为默认浏 - 『 软件下载交流区 』 - 赢政天下 - YingZheng.com - Powered by Discuz!
- 下载最新版本Maven 3.3.9 ,检测安装是否成功时发现Java版本JDK却低于1.7时报错
- Windows 10 下载、安装、破解 SolidWorks 2012 Sp5.0 软件 图文教程
- 在mobile safari中巧妙实现检测应用安装就打开,否则进App Store下载
- Java语言实现简单FTP软件 FTP上传下载管理模块实现(11)
- 利用Java 代码创建ftp客户端并实现上传下载等功能
- zabbix 客户端软件安装配置(Windows操作系统)
- java实现的FTP上传下载客户端
- Windows下Memcached安装及Java客户端调用
- Java_Java exe可执行文件如何制作成能在Windows下安装的软件?
- Java NIO 服务器与客户端实现文件下载
- 安装WinSSHD实现Windows下的SSH协议并配置信任Linux客户端,scp使用
- PyCharm 软件(professional版 专业版)在Windows系统上的下载、安装、破解图文教程
- BlackBerry手机上,Java编程实现程序中下载并安装多个cod,升级自己
- 利用JS判断客户端是否已安装Adobe Reader软件并提示下载安装