C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装
原文:C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装
要求:
- JDK、Mysql、Tomcat三者制作成一个安装包,
- 不能单独安装,安装过程不显示三者的界面,
- 安装完成要配置好JDK环境、Mysql服务、Tomcat 服务
目的:
- 解决客户在安装软件的复杂配置和繁琐
- 便于管理软件版本
- 便于系统集成
分析:
由于不能使用软件的原始安装版本,故只能将JDK的安装目录拷贝出来,放在D盘的SoftSource文件夹,由于要管理三者,将这三个放进一个文件夹里面
Mysql、Tomcat只能用解压版,要让软件运行起来,要做的事情如下:
- 配置JDK环境变量
这一步很重要,否则后面的Tomcat将不能正确运行,
2、安装Mysql服务,我用的是MySQL Server 5.5社区版、解压目录下面有my.ini文件,或者先将mysql安装,然后拷贝安装目录文件,目录结构不能变,安装方法是 用命令行将目录转到mysql的bin目录下,mysqld --install MySQL5 --defaults-file="C:\Program Files\MySQL\MySQL Server 5.5\my.ini" 注意=后面就是你的mysql安装目录下的ini文件路径(可先行在cmd下测试安装,若可以,删除服务的只要将前面的安装语句里面的install改成uninstall)
难点在my.ini文件里面的datadir="D:/ProgramData/MySQL/MySQL Server 5.5/Data/"(数据安装目录)
basedir="D:/Program Files/MySQL/MySQL Server 5.5/" (软件安装目录)
需要与你的实际目录对应,否则会失败,另外要根据选择不同路径,该路径要可以跟着改变
3、安装Tomcat服务是要执行bin目录下面的service.bat文件用法命令行将目录的转到你的Tomcat 下的bin目录安装语句是
service.bat install 卸载 为service.bat uninstall 主要事项(bat文件必须要在当前目录执行,就是命令行的路径必须要转到bat文件的目录,另外需要JAVA_HOME环境变量,还有CATALINA_HOME环境变量(就是要将Tomcat安装目录加到环境变量))
思路清晰了,接着就是代码实现了,(先实现JDK和Mysql )
vs2008 新建 C#类库项目,添加组件Installer1.cs(安装组件)命名为MyInstallerClassDll
重写安装方法(安装前、安装、安装后)附上代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Diagnostics; namespace CustomAction
{
[RunInstaller(true)]
public partial class MyInstallerClassDll : Installer
{ public MyInstallerClassDll()
{
InitializeComponent();
}
protected override void OnBeforeInstall(IDictionary savedState)
{ string server = this.Context.Parameters["server"];
string user = this.Context.Parameters["user"];
base.OnBeforeInstall(savedState);
}
public override void Install(IDictionary stateSaver)
{
string installPath = this.Context.Parameters["targetdir"];
string server = this.Context.Parameters["server"];
string user = this.Context.Parameters["user"];
//Mysql的配置文件
IniFile ini = new IniFile(installPath + @"MySQL\MySQL Server 5.5\my.ini");
//mysql安装路径
ini.Write("mysqld", "basedir", installPath + @"MySQL\MySQL Server 5.5\");
//Mysql数据文件夹
ini.Write("mysqld", "datadir", installPath + @"MySQL\MySQL Server 5.5\Data\"); base.Install(stateSaver);
} protected override void OnAfterInstall(IDictionary savedState)
{
string installPath = this.Context.Parameters["targetdir"];
string mysqlpath = installPath + @"MySQL\MySQL Server 5.5\bin";
string jrePath = installPath + @"Java\jre6\bin";
string iniPath = installPath + @"MySQL\MySQL Server 5.5\my.ini";
string tomcatPath = installPath + @"Tomcat\Tomcat6\bin"; InstallMysql(mysqlpath, iniPath,true); //设置Mysql环境变量
SysEnvironment.SetPath(mysqlpath); //设置JRE环境变量
SysEnvironment.SetPath(jrePath); //设置Tomcat环境变量
SysEnvironment.SetPath(tomcatPath); base.OnAfterInstall(savedState);
} /// <summary>
/// 安装与卸载Mysql服务
/// </summary>
/// <param name="mysqlpath"></param>
/// <param name="iniPath"></param>
/// <param name="isInstall">为true时安装,否则为卸载</param>
private static void InstallMysql(string mysqlpath, string iniPath, bool isInstall)
{
//安装Mysql服务
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true; process.Start();
process.StandardInput.WriteLine("");
process.StandardInput.WriteLine("cd " + mysqlpath);
process.StandardInput.WriteLine(mysqlpath.Substring(2) + " cd");
//mysqld --install MySQLXY --defaults-file="D:\Program Files\MySQL\MySQL Server 5.5\my.ini"
if (isInstall)
{
process.StandardInput.WriteLine("mysqld --install MySQL --defaults-file=" + '"' + iniPath + '"');
process.StandardInput.WriteLine("net start mysql");
} else
{
process.StandardInput.WriteLine("net stop mysql");
//mysqld --install MySQLXY --defaults-file="D:\Program Files\MySQL\MySQL Server 5.5\my.ini"
process.StandardInput.WriteLine("mysqld --remove MySQL --defaults-file=" + '"' + iniPath + '"'); }
process.StandardInput.WriteLine("");
process.StandardInput.WriteLine("");
process.StandardInput.WriteLine("exit");
//Writefile(installPath,process.StandardOutput.ReadToEnd().ToString());
process.Close();
} public override void Uninstall(IDictionary savedState)
{
string installPath = this.Context.Parameters["targetdir"];
string mysqlpath = installPath + @"MySQL\MySQL Server 5.5\bin"; string iniPath = installPath + @"MySQL\MySQL Server 5.5\my.ini"; InstallMysql(mysqlpath, iniPath, true); base.Uninstall(savedState);
}
/// <summary>
/// 写日志
/// </summary>
/// <param name="path"></param>
/// <param name="msg"></param>
public void Writefile(string path, string msg)
{
string file = path + @"日志.txt";
if (File.Exists(file))
File.Delete(file);
using (StreamWriter sw = new StreamWriter(file, true))
{ sw.WriteLine(msg); }
} }
}
下面是SysEnvironment 类的代码,读取设置注册表的信息 代码已经封装好了,就不介绍了
(环境变量的注册表位置为HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001 / Session Manager/Environment )
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32; namespace CustomAction
{
class SysEnvironment
{
/// <summary>
/// 获取系统环境变量
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GetSysEnvironmentByName(string name)
{
string result = string.Empty;
try
{
result = OpenSysEnvironment().GetValue(name).ToString();//读取
}
catch (Exception)
{ return string.Empty;
}
return result; } /// <summary>
/// 打开系统环境变量注册表
/// </summary>
/// <returns>RegistryKey</returns>
private static RegistryKey OpenSysEnvironment()
{
RegistryKey regLocalMachine = Registry.LocalMachine;
RegistryKey regSYSTEM = regLocalMachine.OpenSubKey("SYSTEM", true);//打开HKEY_LOCAL_MACHINE下的SYSTEM
RegistryKey regControlSet001 = regSYSTEM.OpenSubKey("ControlSet001", true);//打开ControlSet001
RegistryKey regControl = regControlSet001.OpenSubKey("Control", true);//打开Control
RegistryKey regManager = regControl.OpenSubKey("Session Manager", true);//打开Control RegistryKey regEnvironment = regManager.OpenSubKey("Environment", true);
return regEnvironment;
} /// <summary>
/// 设置系统环境变量
/// </summary>
/// <param name="name">变量名</param>
/// <param name="strValue">值</param>
public static void SetSysEnvironment(string name, string strValue)
{
OpenSysEnvironment().SetValue(name, strValue); } /// <summary>
/// 检测系统环境变量是否存在
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool CheckSysEnvironmentExist(string name)
{
if (!string.IsNullOrEmpty(GetSysEnvironmentByName(name)))
return true;
else
return false;
} /// <summary>
/// 添加到PATH环境变量(会检测路径是否存在,存在就不重复)
/// </summary>
/// <param name="strPath"></param>
public static void SetPath(string strHome)
{
string pathlist = GetSysEnvironmentByName("PATH");
string[] list = pathlist.Split(';');
bool isPathExist = false; foreach (string item in list)
{
if (item == strHome)
isPathExist = true;
}
if (!isPathExist)
{
SetSysEnvironment("PATH", pathlist +strHome+ ";");
} }
}
}
好了,接下来创建Ini文件操作类,调用了系统api,已经封装好了,可以直接用了
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; namespace CustomAction
{
public class IniFile
{
private string m_iniFileFullPath; /// <summary>
/// ini文件路径
/// </summary>
/// <param name="iniFilePath"></param>
public IniFile(string iniFilePath)
{
m_iniFileFullPath = iniFilePath;
} /// <summary>
/// 写入信息
/// </summary>
/// <param name="iniSection"></param>
/// <param name="iniKey"></param>
/// <param name="iniValue"></param>
public void Write(string iniSection, string iniKey, string iniValue)
{
WritePrivateProfileString(iniSection, iniKey, iniValue, this.m_iniFileFullPath);
} /// <summary>
/// 读取信息
/// </summary>
/// <param name="iniSection"></param>
/// <param name="iniKey"></param>
/// <returns></returns>
public string Read(string iniSection, string iniKey)
{
StringBuilder resultValue = new StringBuilder(255);
int i = GetPrivateProfileString(iniSection, iniKey, "", resultValue,
255, this.m_iniFileFullPath);
return resultValue.ToString();
} /// <summary>
/// 写入信息
/// </summary>
/// <param name="iniSection"></param>
/// <param name="iniKey"></param>
/// <param name="iniValue"></param>
/// <param name="iniPath"></param>
public void Write(string iniSection, string iniKey, string iniValue, string iniPath)
{
WritePrivateProfileString(iniSection, iniKey, iniValue, iniPath);
} /// <summary>
/// 读取信息
/// </summary>
/// <param name="iniSection"></param>
/// <param name="iniKey"></param>
/// <param name="iniPath"></param>
/// <returns></returns>
public static string Read(string iniSection, string iniKey, string iniPath)
{
StringBuilder resultValue = new StringBuilder(255);
int i = GetPrivateProfileString(iniSection, iniKey, "", resultValue,
255, iniPath);
return resultValue.ToString();
} [DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath); [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);
}
}
现在基本代码已经写好了,右键解决方案,添加安装部署项目,右键项目文件视图,新建文件夹Mysql、Java、Tomcat,将你的拷贝的Mysql贴进Mysql项目里面
接下来,右键应用程序文件夹添加主输出
然后右键项目,自定义操作视图,添加安装和卸载的操作(选中主输出)
好了,现在可以测试下了Mysql,未完待续。。。。。。。
下次接着写Tomcat
C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装的更多相关文章
- 阿里云上安装mysql步骤/ 阿里云ECS搭建Java+mysql+tomcat环境
使用阿里云ECS挺长一段时间了.这两天碰巧朋友小白让我一步一步教他在ECS上搭建Java+mysql+tomcat环境,所以把在这里把步骤在这简单整理了一下,以便需要的人查阅. 我购买的阿里云服务器系 ...
- linux安装java mysql tomcat 环境
安装jdk: 1. 查看系统版本 uname -a 2.查看操作系统 cat /proc/version 3.上传安装文件到指定目录 tar -zxvf jdkxxx. 4.修改配置文件 vi ...
- Linux学习3-yum安装java和Tomcat环境
前言 linux上安装软件,可以用yum非常方便,不需要下载解压,一个指令就能用yum安装java和tomcat环境. 前面一篇已经实现在阿里云服务器上搭建一个禅道系统的网站,算是小有成就,但并不是每 ...
- azure 云上MySQL最新版本 MySQL5.7.11 批量自动化一键式安装 (转)
--背景云端 以前都喜欢了源码安装mysql,总觉得源码是高大上的事情,不过源码也需要时间,特别是make的时候,如果磁盘和cpu差的话,时间很长很长,在虚拟机上安装mysql尤其甚慢了. 现在业务发 ...
- MySQL最新版本 MySQL5.7.11 批量自动化一键式安装(转)
--背景云端 以前都喜欢了源码安装MySQL,总觉得源码是高大上的事情,不过源码也需要时间,特别是make的时候,如果磁盘和cpu差的话,时间很长很长,在虚拟机上安装mysql尤其甚慢了. 现在业务发 ...
- vmware tools安装程序无法继续,Microsoft Runtime DLL安装程序未能完成安装。的解决方法
vmware tools安装程序无法继续,Microsoft Runtime DLL安装程序未能完成安装.的解决方法_华英雄_新浪博客 http://blog.sina.com.cn/s/blog_5 ...
- 修改WAMPServer(Apache+PHP+MySQL一键式安装)中mysql默认空密码
Note:在EclipsePHP中配置WorkSpace时,将工作目录指到执行PHP代码的www目录下 ,便于在Eclipse下编写PHP项目 eg:D:\KelvinSoftwar ...
- linux CentOS 安装 nginx+tomcat+java+mysql运行环境
本文介绍了CentOS7 64 Java,Tomcat,MySQL,Maven热部署等服务器环境的搭建过程. 服务器: 已经将所需要的工具(Xshell,Xftp.FileZilla等sftp上传工具 ...
- centos7最小版本安装nginx+tomcat+java+mysql运行环境
最近项目从windows搬到linux,由于项目组成员有限并且有其它紧急的任务需要处理,因而这个任务就落到我的头上了.下面记录下centos最小版本安装nginx+tomcat+mysql+java的 ...
随机推荐
- Android开发者指南-用户界面-拖放-Drag and Drop[原创译文]
英文原文:http://developer.android.com/guide/topics/ui/drag-drop.html 版本:Android 4.0 r1 译者注:黄色底色为未决译文 快 ...
- Java反射库中的安全漏洞在30个月后终于修复了(转)
2013年7月,安全组织Security Explorations发现了Java 7u25中的一个安全漏洞,通过这个漏洞攻击者可以完全摆脱Java沙箱.Oracle在更新的7u40中包含了一个补丁,但 ...
- 特么的. 最终把 amobbs 的站长阿莫(莫进明)给回骂了一顿.
起因: 假设你居住的地方,要上马PX等高污染的项目,你会怎么做. 鼓动别人上街暴力示威与军警对抗. 自己待在家里支持怂恿. 这样的人真心猥琐! 鉴于他常常私自改动帖子, 在此截图留存. 真特么没劲. ...
- JOHN W. TUKEY: HIS LIFE AND PROFESSIONAL CONTRIBUTIONS
DAVID R. BRILLINGER 写的关于John的一片纪念文章 JOHN W. TUKEY: HIS LIFE AND PROFESSIONAL CONTRIBUTIONS (The Ann ...
- (萌O(∩_∩)O)哈希知识点小结
噶呜~先来了解一下什么是哈希吧? 当我们要在一堆东西中找到想要的那一个东西,我们常常通过比较来找,理想的情况是不经过任何比较,一次就能找到,怎么才能做到这样呢?那就在记录的储存位置和他的关键字之间建立 ...
- pwd的实现
#include <string.h> #include <stdlib.h> #include <dirent.h> #include <sys/types ...
- Delphi中编写无输出函数名的DLL文件(有点意思)(400多篇博客)
用 Delphi 用长了,总是发现,有些和 MS 不同的地方.例如,MS 的公开库中,常常隐藏了许多重要函数,这些函数在系统中常常有起着非常巨大的作用.一旦知道如何调用,可以给自己的应用程序提供很强的 ...
- [Android学习笔记]LayoutInflater的使用
LayoutInflater用于动态载入布局,然后获取到布局中定义完成的控件引用 常在动态加载布局,和Adapter中用到 使用步骤:1.通过LayoutInflater加载xml布局文件2.从载入的 ...
- cocos2dx游戏开发学习笔记3-lua面向对象分析
在lua中,能够通过元表来实现类.对象.继承等.与元表相关的方法有setmetatable().__index.getmetatable().__newindex. 详细什么是元表在这里就不细说了,网 ...
- C#开源汇总
原文:C#开源汇总 商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Pho ...