首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视 ┆淘宝手机在线充值 ┆淘宝游戏点卡充值 
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>JAVA专区>开发工具>java将exe文件打包成系统的服务的实例
java将exe文件打包成系统的服务的实例
来源: 发布时间:2008-01-15 发布人: 浏览: 人次   字体: [ ]  
刚做过这样的一个应用,趁着现在还比较熟悉。粘上来供大家学习之用,也便于自己以后时常可以参考。

在这部分的操作中,需要三个外部的应用:exe4j_ejtechnologies.exe,instsrv.exe,srvany.exe。

       首先将自己做好的java项目即jar包通过exe4j_ejtechnologies.exe打包成exe格式的文件。在指定目录里面创建文件夹将打包好的exe文件存放在指定目录中,并且同时在目录里面创建./ToServer文件夹(后面用到)。将instsrv.exe,srvany.exe两个文件存放在./ToServer中.

 public class Buildfile {

 String instsrv = "\\ToServer\\instsrv.exe";
 String srvany = "\\ToServer\\srvany.exe";
 public List getRegContent(String fileName,String serverName){
  File file = new File(fileName);
  String path = "";
  String root = "";
  File temp1 = null;
  File temp2 = null;
  List list = new ArrayList<String>();
  if(file.exists()){
   path = file.getAbsolutePath();
   root = path.substring(0, path.lastIndexOf("\\"));
   if(root.endsWith(File.separator)){
    temp1 = new File(root+instsrv);
    temp2 = new File(root+srvany);
   }else{
    temp1 = new File(root+File.separator+instsrv);
    temp2 = new File(root+File.separator+srvany);
   }
   list.add(0, "@echo off");
   list.add(1, "rem as follows is regedit system server");
   list.add(2, temp1.getAbsolutePath()+" "+serverName+" "+temp2.getAbsolutePath());
   list.add(3, "rem as follows is regedit key assignments");
   list.add(4, "start REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\"+serverName+"\\Parameters /v Application /t REG_SZ  /d "+path);
  }
  return list;
 }
 public String getRemContent(String fileName,String serverName){
  File file = new File(fileName);
  String path = "";
  String root = "";
  File temp1 = null;
  if(file.exists()){
   path = file.getAbsolutePath();
   root = path.substring(0, path.lastIndexOf("\\"));
   if(root.endsWith(File.separator)){
    temp1 = new File(root+instsrv);
   }else{
    temp1 = new File(root+File.separator+instsrv);
   }
  }
  String str = temp1.toString()+" "+serverName+" "+"remove";
  return str;
 }
 public static void writeLog(String filePath,String content){
  FileWriter tofile;
  BufferedWriter bfw;
  try {
   tofile = new FileWriter(filePath, true);
   bfw = new BufferedWriter(tofile);
   bfw.write(content, 0, content.length());
   bfw.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {
       Buildfile test = new Buildfile();
       File file1 = new File("regeditServer.bat");
       File file2 = new File("UninsServer.bat");
       if(file1.exists()&&file2.exists()){
        System.exit(0);
       }else{
        for(int i=0;i<test.getRegContent("ServerConsole.exe","ServerApp").size();i++){
            String str = (String) test.getRegContent("ServerConsole.exe","ServerApp").get(i);
            writeLog("regeditServer.bat", str+"\n");
           }
        writeLog("UninsServer.bat", test.getRemContent("ServerConsole.exe","ServerApp")+"\n");
       }    
 }
}

       将上面的类做成一个项目,并且打包。在上面的类中特别注意的是在main()方法里面的参数,例如ServerConsole.exe为自己做的exe文件的名字,ServerApp为需要打包的服务名字。

       将打包好的项目存放在自己exe文件的当前目录,些一个批处理来执行jar包,生成两个新文件分别为:regeditServer.bat和UninsServer.bat。通过点击regeditServer.bat就可以将exe注册成服务,点击UninsServer.bat便可以移除此系统服务的。


相 关 文 章   发布商链接
·eclipse开发Web项目时虚拟目录的妙用
·用Editplus配置JAVA编辑环境三步走
·使用Eclipse快速浏览源代码小技巧
·eclipse常用快捷键使用大全
·如何设置Eclipse工作空间的缺省字符...
·Eclipse的Ant Viewer中开发EJB的环境...
·图解两工具轻松去除文章中的多余空行
·举例myeclipse设置自定义快捷方式的...
·解读Eclipse3.3下VSS的安装和使用步...
·详解MyEclipse6开发JDK6和Struts2冲...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·图解两工具轻松去除文章中的...
·Eclipse的Ant Viewer中开发EJ...
·如何设置Eclipse工作空间的缺
·eclipse常用快捷键使用大全
·使用Eclipse快速浏览源代码小
·用Editplus配置JAVA编辑环境...
·eclipse开发Web项目时虚拟目...
·在Eclipse 3.0 上配置JSP开发
热 门 文 章
·如何设置Eclipse工作空间的缺...
·解读Eclipse3.3下VSS的安装和...
·eclipse常用快捷键使用大全
·详解MyEclipse6开发JDK6和Str...
·Eclipse 十大常用快捷键
·在Eclipse 3.0 上配置JSP开发...
·举例myeclipse设置自定义快捷...
·使用Eclipse快速浏览源代码小...
·eclipse开发Web项目时虚拟目...
·用Editplus配置JAVA编辑环境...
·图解两工具轻松去除文章中的...
·Eclipse的Ant Viewer中开发EJ...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .