把java程序作成windows EXE程序或windows服务---经典总结

时间:2021-06-19 16:40:32

(1)制作WINDOWS EXE程序

    java做成window的exe程序或者服务程序实在是吃饱撑的,有点变态,但是如果你不想起在一台服务器的后台程序,被哪个笨蛋不小心给关了,或者人家把进程中的java给杀了,那就有必要照着下面我说的方法去做了:  

 borland不公开的一个使用技巧,能够通过jbuilder来制作exe文件来启动java文件。jbuilder并不支持本地编译机制。但是有一个隐藏的技巧可以让你从可执行文件来启动java程序,可以出现或者不出现console窗口。想做到这些,需要jbuilder的bin目录下的这些文件:
jbuilder.exe (带DOS窗口)
jbuilderw.exe (不带DOS窗口)
jbuilder.config
jdk.config


  “jbuilder.exe”是一个通用的可执行外壳文件,用以启动java程序,”jbuilderw.exe“好像是javaw.exe一样,它把”jbuilder.exe”包装起来,但是运行时候不显示那个console的窗口。使用这些文件的关键是文件名。“jbuilder.exe”查找一个文件叫”jbuilder.config”的配置文件,里面包含了运行java程序的必须信息。同样的”jbuilderw.exe”查找”jbuilder.exe”来启动不带console窗口的java程序。

  如果把jbuilder.exe重命名为”smsDialTest.exe”,那”smsDialTest.exe”将去寻找”smsDialTest.config”配置文件,同样”jbuilderw.exe”被重命名为”smsDialTestw.exe”,它会去寻找”smsDialTest.exe”文件。

      所以,我们只要把jbuilder .exe,jbuilerw.exe,jbuilder.config改名成相应的文件名,在jbuilder.config里面指定主
类和类路径,就能够通过执行jbuilder.exe(或者被改名后的exe文件)来启动java应用程序了。  

  设置jdk.conifg中的"javapath",例子:“javapath ../jdk1.5/jre/bin/client/jvm.dll”

      设置smsDialTest.config:

      # +-----------------------------------------------------+
# |                                                     |
# | This is the configuration file for the smsDialTest  |
# |                                                     |
# |   Do NOT make modifications without maintaining a   |
# |      backup copy.  See config_readme.html for a     |
# |      description of the recognized directives.      |
# |                                                     |
# +-----------------------------------------------------+

# Read the shared JDK definition
include jdk.config

# Tune this VM to provide enough headroom to work on large
# applications
vmparam -Xms128m
vmparam -Xmx512m

# Put the Light AWT wrapper and various JDK fixes on the boot path
addbootpath ../image


# Add all JAR files located in the patch, lib and lib/ext directory
addjars ../lib
addjars ../classes

 

# Activate the shell integration
socket 8888

# Add all the configuration files located in the lib/ext directory

 

# JBuilder needs to have access to the environment
exportenv

# Start smsDialTEST using the main class
mainclass smsdialtest.gui.SmsDialTestMainFrame

(2)把JAVA程序做成windows服务程序

我试过把JAVA做成EXE,然后再做成windows服务,但是问题出来了,没地方把环境变量以及很多参数设置进去。

最后,用“java service  Wrapper”来搞定这个问题。

Wrapper 下载页面
   http://sourceforge.net/project/showfiles.php?group_id=39428

  最新的是3.1.2  以下例子都是在此版本下进行
  环境:Win2003server + JDK1.5 + Wrapper3.1.2


  配置wrapper.conf文件>
    
#********************************************************************
# Wrapper Properties
#********************************************************************
# Java Application
wrapper.java.command=E:/project/developIDE/project/unicomsms/dialtest/jdk1.5/bin/java

# Java Main class.  This class must implement the WrapperListener interface
#  or guarantee that the WrapperManager class is initialized.  Helper
#  classes are provided to do this for you.  See the Integration section
#  of the documentation for details.
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp

# Java Classpath (include wrapper.jar)  Add class path elements as
#  needed starting from 1
wrapper.java.classpath.1==%PATH%
wrapper.java.classpath.2=../lib/wrapper.jar
wrapper.java.classpath.3=../lib/*.jar
wrapper.java.classpath.4=../classes
wrapper.java.classpath.5=../conf
wrapper.java.classpath.6=../log


# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=../lib
wrapper.java.library.path.2=../jdk1.5/jre/lib/ext/win32com.dll
wrapper.java.library.path.3=../jdk1.5/jre/bin/win32com.dll
# Java Additional Parameters
#wrapper.java.additional.1=

# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=128

# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=512

# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=smsdialtest.util.SmsServerUtil

#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Format of output for the console.  (See docs for formats)
wrapper.console.format=PM

# Log Level for console output.  (See docs for log levels)
wrapper.console.loglevel=INFO

# Log file to use for wrapper output logging.
wrapper.logfile=../log/wrapper.log

# Format of output for the log file.  (See docs for formats)
wrapper.logfile.format=LPTDM

# Log Level for log file output.  (See docs for log levels)
wrapper.logfile.loglevel=INFO

# Maximum size that the log file will be allowed to grow to before
#  the log is rolled. Size is specified in bytes.  The default value
#  of 0, disables log rolling.  May abbreviate with the 'k' (kb) or
#  'm' (mb) suffix.  For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=0

# Maximum number of rolled log files which will be allowed before old
#  files are deleted.  The default value of 0 implies no limit.
wrapper.logfile.maxfiles=0

# Log Level for sys/event log output.  (See docs for log levels)
wrapper.syslog.loglevel=NONE

#********************************************************************
# Wrapper Windows Properties
#********************************************************************
# Title to use when running as a console
wrapper.console.title=增值业务综合网管业务拨测客户端软件

#********************************************************************
# Wrapper Windows NT/2000/XP Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when an application
#  using this configuration file has been installed as a service.
#  Please uninstall the service before modifying this section.  The
#  service can then be reinstalled.

# Name of the service
wrapper.ntservice.name=服务名字

# Display name of the service
wrapper.ntservice.displayname=服务名字

# Description of the service
wrapper.ntservice.description=服务名字

# Service dependencies.  Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=

# Mode in which the service is installed.  AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START

# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=false

  把wrapper-windows-x86-32-3.2.1/src/bin目录下的,几个文件COPY到你的BIN目录下,把他们的.bat.in扩展名,改为.bat就可以了。
App.bat.in//手工启动你的程序,带着DOS窗口,一开始你要用这个文件测试你的环境是否搭好了
InstallApp-NT.bat.in//安装服务程序,安装成功后会在WINDOWS服务中显示出你的服务程序
UninstallApp-NT.bat.in//卸载服务程序。
wrapper.exe//wrapper主程序

   把wrapper-windows-x86-32-3.2.1/lib目录下的wrapper.dll,wrapper.jar2个文件COPY到你的LIB目录下。

最后,你先用App.bat测试你的程序,如果一切正常,就运行InstallApp-NT.bat.。这样制作的服务就成功了。