把java程式作成windows EXE程式或windows服務---經典總結

來源:互聯網
上載者:User

(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.。這樣製作的服務就成功了。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.