热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#制作Java+Mysql+Tomcat环境安装程序,一键式安装教程

本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQLServer5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld--installMySQL5命令。


原文:
C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装

要求:



  1. JDK、Mysql、Tomcat三者制作成一个安装包,


  2. 不能单独安装,安装过程不显示三者的界面,


  3. 安装完成要配置好JDK环境、Mysql服务、Tomcat 服务

目的:



  1. 解决客户在安装软件的复杂配置和繁琐


  2. 便于管理软件版本


  3. 便于系统集成

分析:

由于不能使用软件的原始安装版本,故只能将JDK的安装目录拷贝出来,放在D盘的SoftSource文件夹,由于要管理三者,将这三个放进一个文件夹里面

Mysql、Tomcat只能用解压版,要让软件运行起来,要做的事情如下:



  1. 配置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

);
}


///


///


安装与卸载Mysql服务



///

///
///
///


为true时安装,否则为卸载






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

);
}


///
///


写日志



///

///
///



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

{


///


///


获取系统环境变量



///

///
///



public static string


GetSysEnvironmentByName

(

string


name

)
{


string


result

=

string

.

Empty

;


try


{


result

=

OpenSysEnvironment

().

GetValue

(

name

).

ToString

();

//读取


}


catch

(

Exception

)
{


return string

.

Empty

;
}


return


result

;
}


///
///


打开系统环境变量注册表



///

///


RegistryKey






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

;
}


///
///


设置系统环境变量



///

///


变量名



///









public static void


SetSysEnvironment

(

string


name

,

string


strValue

)
{


OpenSysEnvironment

().

SetValue

(

name

,

strValue

);
}


///
///


检测系统环境变量是否存在



///

///
///



public bool


CheckSysEnvironmentExist

(

string


name

)
{


if

(!

string

.

IsNullOrEmpty

(

GetSysEnvironmentByName

(

name

)))


return true

;


else
return false

;
}


///
///


添加到PATH环境变量(会检测路径是否存在,存在就不重复)



///

///



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

;


///


///


ini文件路径



///

///



public


IniFile

(

string


iniFilePath

)
{


m_iniFileFullPath

=

iniFilePath

;
}


///
///


写入信息



///

///
///
///



public void


Write

(

string


iniSection

,

string


iniKey

,

string


iniValue

)
{


WritePrivateProfileString

(

iniSection

,

iniKey

,

iniValue

,

this

.

m_iniFileFullPath

);
}


///
///


读取信息



///

///
///
///



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

();
}


///
///


写入信息



///

///
///
///
///



public void


Write

(

string


iniSection

,

string


iniKey

,

string


iniValue

,

string


iniPath

)
{


WritePrivateProfileString

(

iniSection

,

iniKey

,

iniValue

,

iniPath

);
}


///
///


读取信息



///

///
///
///
///



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 环境安装程序,一键式安装




推荐阅读
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 标题: ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
author-avatar
禁灭19
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有