基于ECLIPSE的J2EE和本地应用程序开发的JAVA环境配置
番禺职业技术学院软件工程与技术中心 曾青松
2005-6-15
摘要:重点介绍Eclipse以及相关插件的基本功能和如何配置一个J2ee开发环境。
关键字:gnu,j2ee,eclise,Jboss,tomcat,lomboz
本文的目的在于介绍使基于GNU的免费开发工具和插件构建一个J2EE开发环境。本文不打算详细介绍如何开发J2EE应用程序,在后续的章节中将做介绍,本文只讲述配置,以及简单的应用和功能介绍。所有的插件和开发工具,都经过测试在本文所用到的相关软件配合下能够正常工作,由于Eclipse对版本的依赖性,不同版本的插件不能保证一定工作,所以在安装之前一定要认真阅读软件的说明书。
1.安装ECLIPSE集成开发环境
下载地址:http://www.eclipse.org/
直接将下载的eclipse开发包解压缩到c:/eclipse就可以。
最好下载带有WTP,Eclipse,EMF,Gef,SDK的开发包如:wtp-eclipse-emf-gef-jem-sdk-1.0M4.zip这样省去一个个下载插件。如果想直接将此目录下的文件复制到其他计算机后也能运行,建议将jre拷贝到如c:/eclipse/jre(缺省的搜索路径)目录下,最好用最新的jre150。
如果想开发本地Windows界面的应用程序,请安装插件jigloo312以支持类似于Delphi一样的设计窗体的工具。下载地址:http://www.cloudgarden.com/jigloo
Eclipse的相关网站:
http://eclipse-plugins.2y.net/eclipse/index.jsp
http://www.eclipseplugincentral.com/
2.JBoss下载地址
JBoss是一个免费的开源的EJB容器,支持J2EE应用程序的开发。
下载地址:http://www.jboss.org/download.jsp,http://sourceforge/projecs/jboss
目前可用的版本为4.0.2
Before you run JBOSS, in order to check your installation of JBOSS, make sure have you have following environment variables set.
(Linux环境下)
JBOSS_HOME: /opt/jboss/jboss-4.0.2
JAVA_HOME: /usr/java/j2sdk1.4.1_02
CLASSPATH: /usr/java/j2sdk1.4.1_02/lib/tools.jar
在Windows环境下,我们需要把JBOSS解压缩到如C:/jboss-4.0.1目录即可。
安装JDK150
设置环境变量(便于DOS下调式和运行调用)
JBOSS_HOME:c:/jboss-4.0.1
JAVA_HOME: C:/Program Files/Java/jdk1.5.0_03
CLASSPATH: C:/Program Files/Java/jdk1.5.0_03/lib/tools.jar
测试服务器:
Go into the bin directory and you will find a script named run.sh.(run.bat)
[C:/jboss-4.0.1/bin/run.bat -c all]
Run this script from the command line. Hopefully it will start, and you will see this message.
08:47:49,010 INFO [Server] JBoss (MX MicroKernel) [s_4_0_1_SP1 date=200502160314)] Started in 58s:133ms
Note: This means that JBOSS is installed and running successfully. Now, it's time to configure it from within the eclipse IDE.
In order to test JBOSS is running correctly, go to your web browser and access this url 'http://localhost:8080/jmx-console/index.jsp'.
Note: This page is referred as the 'JMX Management Console'.
上述操作将启动管理界面。
你必须保证能够正确进入‘http://localhost:8080/’。如果你安装了Apache或者Tomcat请先停止占用8080端口的Web服务器程序。
修改C:/Tomcat5.5/conf/server.xml配置文件,将缺省http端口改为8081或者其他不会冲突的端口。如果是Apache也一样修改,这里就不介绍了。
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector
port="8081" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
下载Ejb3.0(jboss-EJB-3.0_Preview_5),遵照说明安装,以使JBoss支持Ejb3.0
下载JBossIDE-1.4.1-jre1.5-e31插件,解压缩后复制到Eclipse安装目录,以便在Eclipse中支持Jboss开发。
下载lomboz插件以支持JBOSS4.0.2
http://download.forge.objectweb.org/lomboz/
寻找最新的插件。(lomboz-I20050526.zip)
注意:lomboz也有一个集成WTP,Eclipse,EMF,Gef,SDK,lomboz的包,如果直接下载可以省去一些配置的麻烦,但是如果前面已经配置好,只需要下载lomboz插件就可以,注意版本一定要相同,否则你启动不了。
如果不下载插件只支持Jboss3.2.3。想手工配置支持4.0.x除非你特别熟悉否则建议不要手工配置,涉及的文件太多。
到此为止JBOSS已经安装完成。
3.安装插件配置支持Struts MVC开发
Struts console 插件地址:http://www.jamesholmes.com/struts/console/download.html
安装步骤:摘自上述网站的说明,有删减
6. Using Struts Console with Eclipse and IBM WebSphere Application Developer
The Struts Console software integrates with Eclipse/WSAD as a Plugin for seamless development of Struts applications in one development tool. Once the Struts Console Plugin for Eclipse/WSAD has been installed, you can manage Struts config files by simply right clicking on them and selecting "Open With" and then selecting "Struts Console".
a. Installing Struts Console Plugin
Eclipse 3.0m7 and higher
1. Shutdown Eclipse if it is currently running.
2. Navigate to the directory where you have Eclipse installed and then navigate into the "plugins" directory.
3. In another window navigate to the directory where you installed the Struts Console software.
4. Copy the "com.jamesholmes.console.struts" directory from the Struts Console installation directory into the Eclipse "plugins" directory.
5. Navigate into the "com.jamesholmes.console.struts" directory underneath Eclipse "plugins" directory.
6. Rename "plugin.xml" to "plugin.xml.bak".
7. Rename "plugin-3.0.xml" to "plugin.xml".
8. Restart Eclipse.
1. NOTE: If you are using Eclipse 3 under Linux/Unix, you must run it using JDK 1.5. This is necessary because Faces Console makes uses of a special library (SWT_AWT) for integrating SWT and Swing. The SWT_AWT library requires JDK 1.5 under Linux/Unix.
b. Using Struts Console inside Eclipse/WSAD
1. Startup Eclipse/WSAD if it is not already running.
2. Find your Struts, Tiles or Validator config file in the Navigator or Packages view on the left hand side of the screen.
3. Right click on the config file and select "Open With" from the menu and then select "Struts Console".
1. NOTE: Eclipse/WSAD will not recognize your Struts config files if they are not named "struts-config.xml" (Struts files), "tiles.xml", "tiles-defs.xml", (Tiles files) or "validation.xml" (Validator files). If you would like Eclipse to recognize other filenames as being Struts files you can edit the "filenames" attribute in the "plugin.xml" file in the "com.jamesholmes.console.struts" directory underneath your Eclipse/WSAD "plugins" directory.
如果没有可视化的向导,可以按照下述方法一支持Struts开发。
参考下面的目录就知道了。最后使用ANT来编译打包,发布。
支持Struts测试
http://strutstestcase.sourceforge.net/,http://sourceforge.net/projects/strutstestcase/
下载安装StrutsTestCase软件包支持JunitStruts测试。
如果希望在Elipse中使用StrutsTestCase,需要作如下工作:
复制junit3.8.1.jar到C:/eclipse/plugins/org.apache.ant_1.6.2/lib/junit3.8.1.jar
修改C:/eclipse/plugins/org.apache.ant_1.6.2/plugin.xml文件添加:
<library name="lib/junit3.8.1.jar">
<export name="*"/>
</library>
以支持<junit>任务。
注释:其他参考资料说只需要将junit.jar复制到<ANT_HOME>/lib目录,在Eclipse中就是C:/eclipse/plugins/org.apache.ant_1.6.2/lib/,但是不行,在C:/eclipse/plugins/org.apache.ant_1.6.2/plugin.xml中有个
<library name="lib/ant-junit.jar">
<export name="*"/>
</library>
但是lib目录下的ant-junit.jar文件只有72kb,而且使用缺省的设置编译StrutsTestCase时报告错误,找不到路径?如果直接覆盖该文件也会出现错误。但是我的上述设置编译没有问题。
在build.xml文件中增加下属编译任务。
<target name="test-all" depends="struts-tests" />
<target name="struts-tests" depends="build">
<junit printsummary="yes">
<classpath>
<pathelement location="${classes.home}" />
<pathelement location="${build.home}" />
<pathelement location="${build.home}/WEB-INF/classes" />
<path refid="compile.classpath" />
</classpath>
<formatter type="plain" />
<test name="hello.mocktest.TestHelloAction" />
<test name="hello.mocktest.TestHelloActionMultiple" />
</junit>
</target>
测试例子:(详细使用方法参考http://strutstestcase.sourceforge.net)
import servletunit.struts.MockStrutsTestCase;
public class TestHelloAction extends MockStrutsTestCase {
public TestHelloAction(String testName) { super(testName); }
public void testNoParameters() {
setRequestPathInfo("/HelloWorld");
actionPerform();
verifyInputForward();
// Test Form Bean validations
verifyActionErrors(new String[] {"hello.no.username.error"});
}
}
4.ANT的例子(开发Struts MVC的例子)
几乎所有的基于Struts MVC开发都可以参考下面的build.xml文件。
<project name="helloapp" default="help" basedir=".">
<property file="build.properties" />
<property name="app.home" value="." />
<property name="app.name" value="helloapp" />
<property name="javadoc.pkg.top" value="hello" />
<property name="src.home" value="${app.home}/src" />
<property name="lib.home" value="${app.home}/lib" />
<property name="classes.home" value="${app.home}/classes" />
<property name="deploy.home" value="${app.home}/deploy" />
<property name="doc.home" value="${app.home}/doc" />
<property name="web.home" value="${app.home}/web" />
<property name="build.home" value="${app.home}/build" />
<property name="build.classes" value="${build.home}/WEB-INF/classes" />
<property name="build.lib" value="${build.home}/WEB-INF/lib" />
<path id="compile.classpath">
<pathelement location="${classes.home}" />
<fileset dir="${lib.home}">
<include name="*.jar" />
<include name="*.zip" />
</fileset>
<fileset dir="${tomcat.home}/common/lib">
<include name="*.jar" />
</fileset>
<pathelement location="${tomcat.home}/common/classes" />
</path>
<target name="clean-all">
<delete dir="${build.home}" />
<delete dir="${classes.home}" />
<delete dir="${deploy.home}" />
<!-- deleting the deployed .war file is fine even if Tomcat is running -->
<delete dir="${webapps.home}/${app.name}.war" />
<!-- can't delete directory if Tomcat is running -->
<delete dir="${webapps.home}/${app.name}" failοnerrοr="false" />
<!-- delete the javadoc -->
<delete dir="${doc.home}" />
</target>
<target name="prepare">
<echo message="Tomcat Home = ${tomcat.home}" />
<echo message="webapps Home = ${webapps.home}" />
<mkdir dir="${classes.home}" />
<mkdir dir="${deploy.home}" />
<mkdir dir="${doc.home}" />
<mkdir dir="${doc.home}/api" />
<mkdir dir="${build.home}" />
<mkdir dir="${build.home}/WEB-INF" />
<mkdir dir="${build.home}/WEB-INF/classes" />
<mkdir dir="${build.home}/WEB-INF/lib" />
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.home}" destdir="${classes.home}" debug="yes">
<classpath refid="compile.classpath" />
</javac>
</target>
<target name="build" depends="compile">
<!-- Copy all the webapp content (jsp's, html, tld's, xml, etc. -->
<!-- Note that this also copies the META-INF directory -->
<copy todir="${build.home}">
<fileset dir="${web.home}" />
</copy>
<!-- Now, copy all the Java class files -->
<copy todir="${build.home}/WEB-INF/classes">
<fileset dir="${classes.home}" />
</copy>
<!-- Now, copy all the properties files, etc that go on the classpath -->
<copy todir="${build.home}/WEB-INF/classes">
<fileset dir="${src.home}">
<include name="**/*.properties" />
<include name="**/*.prop" />
</fileset>
</copy>
<!-- Now, copy all the jar files we need -->
<copy todir="${build.home}/WEB-INF/lib">
<fileset dir="${lib.home}" />
</copy>
<!-- Create the <war> file -->
<jar jarfile="${deploy.home}/${app.name}.war" basedir="${build.home}" />
</target>
<target name="deploy" depends="build">
<copy todir="${webapps.home}" file="${deploy.home}/${app.name}.war" />
</target>
<target name="javadoc" depends="compile">
<javadoc sourcepath="${src.home}" destdir="${doc.home}/api" packagenames="${javadoc.pkg.top}.*" />
</target>
<target name="test-all" depends="struts-tests" />
<target name="struts-tests" depends="build">
<junit printsummary="yes">
<classpath>
<pathelement location="${classes.home}" />
<pathelement location="${build.home}" />
<pathelement location="${build.home}/WEB-INF/classes" />
<path refid="compile.classpath" />
</classpath>
<formatter type="plain" />
<test name="hello.mocktest.TestHelloAction" />
<test name="hello.mocktest.TestHelloActionMultiple" />
</junit>
</target>
</project>
5.Java转exe
实现方式:Eclipse搭配JSmooth(免费)。
1.先由Eclipse制作包含Manifest的JAR。
2.使用JSmooth将做好的JAR包装成EXE。 JSmooth下载页面: http://jsmooth.sourceforge.net/index.php
3.制作完成的exe文件,可在有装置JRE的Windows上执行。
其他插件介绍:
Coverlipse是一个用于显示JUnit单元测试覆盖率的Eclipse插件。
JUnit是目前最常用的单元测试工具。在Eclipse IDE环境中,用Coverlipse插件可以让开发者直观地看到JUnit单元测试对代码的覆盖程度。目前该插件可以显示代码块覆盖率,但对分支结构的覆盖率统计还不准确。并且Coverlipse是一个开源产品。
下载地址:http://coverlipse.sourceforge.net/download.html
1. J2SDK
http://java.sun.com/j2se/1.4.2/download.html
2. Eclipse
http://www.eclipse.org/downloads/index.php
3. Lomboz
http://forge.objectweb.org/project/showfiles.php?group_id=97
4. EMF Runtime
http://download.eclipse.org/tools/emf/scripts/downloads.php
5. JBoss
http://www.jboss.org/downloads/index
6. MySql
http://dev.mysql.com/downloads/
7. MySql-JDBC Driver
http://www.mysql.com/products/connector/j/
8. Tomcat
http://jakarta.apache.org/site/downloads/downloads_tomcat.html
9. TomcatPlugin
http://www.sysdeo.com/eclipse/tomcatPlugin.html
10. Struts
http://struts.apache.org/download.cgi
11. Ant
http://ant.apache.org/bindownload.cgi