A good article seen on J2Medev, has not finished reading, first collected several critical uses of Eclipse, including installation configuration, junit, ant, cvs, etc., very nice to install configuration, JUnit, Ant, CVS, etc.
Eclipse is a very good open source IDE, which is ideal for Java development, due to support plug-in technology, has received more and more developers. The latest Eclipse 3.0 not only has a large increase in interface, but also increases many excellent functions such as code folding, and there is also a significant increase in speed. With a lot of dazzling plug-ins, it can fully meet the development of the company's Java to the mobile terminal Java game. This article will take you into the vast world of Eclipse, in detail how to develop ordinary Java programs, web applications, J2EE applications, mobile Java programs, and how to perform unit testing, reconstruction, configuring CVS, etc. under Eclipse.
My development environment is JDK1.4.2 Eclipse3.0 Windows XP SP2, if you encounter any problems on other platforms, welcome to communicate.
1. Install JDK1.4
Eclipse is a development environment based on the Java platform. It itself is also available on the Java virtual machine, but also use JDK compiler, so we must first install JDK. JDK1.4 is currently the most stable version, and is also the required condition of Eclipse running. First download the JDK1.4 Windows version from Sun's official site http://java.sun.com, the latest is 1.4.2_06, then run J2SDK-1_4_2_06-Windows-i586-P.exe installation, you can set it yourself Install the directory, I installed it into the D: /software/j2sdk1.4 directory.
Next to configure the environment variable so that the Java program can find the installed JDK and other configuration information. Right click on "My Computer", select "Properties", select "Advanced", "Environment Variable" in the pop-up dialog box, you can see the environment variable dialog box: The above is the user variable, only valid for the current user, below It is a system variable that is valid for all users. If you want all users to use, click "New" under the system variable, fill in: Java_home is the JDK installation directory, and many developments that depend on JDK rely on JDK, so it must be guaranteed correctly.
Next, find the system variable PATH, click "Edit", in the last purchase of the Directory of the JDK executable, ie% java_home% / bin, my corresponding directory is D: /software/j2sdk1.4/bin, attached In the PATH, pay attention to the semicolon ";" separation: Note: If the system has multiple Java virtual machines installed (such as the JDK1.3 that own Oracle 9i), JDK1.4 must be The path is placed in front of other JVMs, otherwise the Eclipse starts an error.
The last system variable is ClassPath, and the Java virtual opportunity searches for the ClassPath settings, but this is not required, you can specify ClassPath when running the Java program, such as running a good Java program in Eclipse, it ClassPath will be set automatically, but in order to easily run the Java program in the console, I suggest it to set a classpath and set it to ".", Note that it is a point "." Represents the current directory. Using users who are usually Windows may think that the Java virtual machine searches for the current directory when searching, in fact, this is the habit in UNIX, for security considerations. Many beginners Java friends are in a hurry to write Hello, World program, and play the java.lang.noclassdeffounderror, which is just not set, just add a current directory "." 2. Install ECLIPSE 3.0
After configuring JDK, the next step is to install Eclipse 3.0, you can download from the official website of Eclipse http://www.eclipse.org, you will see the following version:
● Eclipse SDK ● RCP runtime binary ● RCP SDK ● Platform Runtime Binary ● Platform SDK ● JDT Runtime Binary
Eclipse SDK includes an Eclipse development environment, a Java development environment, a PLUG-IN development environment, all source code, and documents. If you need all your features, you can download this version.
If you are like me, just develop Java applications with Eclipse, not developing an Eclipse plugin or studying an Eclipse code, then downloading a Platform Runtime Binary plus JDT runtime binary is the best choice.
After downloading Eclipse-platform-3.0-win32.zip and Eclipse-jdt-3.0.zip, unzip them to the same directory, do not install, find the Eclipse.exe running in the directory, start screen: Something, The Eclipse interface came out.
If you encounter an error, startup failed, you can check the log file under the Eclipse directory. I have encountered XmlParser anomalies. Carefully check that there is also a virtual machine in the original PATH, put it from the PATH. After the Eclipse is started normally.
3. First Java program
Run Eclipse, select menu "file", "new", "project", create new Java Project, I name it to HelloWorld, then create a Java Class: I name it to HelloWorld, and fill in package for Example, hook "Public Static Void Main (String [] args", click "Finish", Eclipse automatically generates the code frame, we only need to fill in the main method: Under the default setting, Eclipse will automatically compile in the background, we only need Save, then select "Run", "Run As", "Java Application", you can see the output at Eclipse's console. To debug the Java program is also very simple, the Run menu contains standard debug commands, which can be very convenient to debug applications in the IDE environment.
1.4 version support:
Select menu "Window", "preferences", find "Java", "Compiler", "CompLiance and ClassFiles" in the dialog, change the compilation option to 1.4, you can use the JDK1.4 version of Assert, Make the test more convenient:
4. Using JUnit in Eclipse
Testing for ensuring that software development quality has a very important role, unit test is essential, JUnit is a very powerful unit test package, can test a single / multiple method of one / class, can also be different TestCase is combined into testsuit to automate test tasks. Eclipse is also integrated with JUnit, which can be easily written in TestCase. We created a Java project, add an Example.Hello class, first we add an ABS () method to the Hello class, the role is to return absolute values: Next, we are ready to test this method to ensure normal function. Select Hello.java, right click, select New-> JUnit Test Case:
Eclipse will ask if you add a junit.jar package, and then create a Hellotest class after you have created the Hello class.
Check Setup () and Teardown (), then click "Next":
Select the method you want to test, we select the ABS (int) method, enter the Hellotest.java after completion:
JUnit will perform tests in the following order: (rough code)
TRY {Hellotest Test = New Hellotest (); // Establish test class instance test.setup (); // Initialization test environment test.testabs (); // Test a method Test.teardown (); // Clean resources} Catch ...
Setup () is an instance of a Hello class; Teardown () is used to clean the resources, such as released files, and more. The method starting with TEST is considered to be a test method, and JUnit will execute the TestXxxxx () method in turn. In the Testabs () method, we select positive, negative numbers, and 0 for the test of ABS (). If the method return value is the same as the expected result, Assertequals does not produce an exception. If there are multiple TestXxxx methods, Junit creates multiple XXXTEST instances, running a Testxxxx method each time, setup () and Teardown () will be called before and after Testxxx, so don't depend on Testb () in a Testa (). You can see the JUnit test results directly in Run-> Run as-> JUnit Test:
Green indicates that the test passes, as long as one test is not passed, it will be displayed and the method that does not pass the test. You can try to change the code of the ABS (), deliberately returning the result of the error (such as Return n 1;), then run the junit will report an error.
If there is no Junit panel, select Window-> show view-> Other, open the view of Junit:
JUnit is tested through unit testing, and many bugs can be found in the development phase, and multiple Test Cases can be combined into Test Suite, allowing the entire test to complete, especially suitable for XP methods. Each time a small new function is added or a small modification is made to the code, it is immediately running a Test Suite, ensuring that the new and modified code does not destroy the original features, greatly enhance the maintenanceability of the software, and avoid code gradually. " rot".
5. Using Ant in Eclipse
Ant is a very great batch command executor under the Java platform, which is very convenient to automatically complete compilation, test, packaging, deployment, etc., greatly improve development efficiency. If you haven't started using Ant yet, then you will start learning to use, so that your development level is a new level. Ant has been integrated in Eclipse, we can run Ant directly in Eclipse. Take the Hello project established as an example, create the following directory structure: Create a build.xml, put it in the root directory. Build.xml defines the batch command to be executed. Although Ant can also use other file names, compliance can be more standardized and easy to communicate with others.
Typically, the SRC stores the Java source file, Classes stores compiled Class files, lib stores all JAR files for compilation and running, Web stores JSP, etc., DISS is stored, the packaged JAR file, and the DOC stores the API document.
Then create a build.xml file in the root directory, enter the following:
Select Hello Engineering, then select "Project", "Properties", "Builders", "New ...", select "Ant Build
Fill in Name: Ant_Builder; BuildFile: Build.xml; Base Directory: $ {workspace_loc: / hello} (press "Browse Workspace" to select the root directory), search for the eclipse directory due to the junit.jar package, find junit.jar Put it into the Hello / Lib directory and add to the ClassPath in Ant:
Then hook Ant_build in the Builder panel, remove Java Builder:
Compiled again, you can see the output of Ant on the console:
BuildFile: f: /eclipse-projects/hello/build.xml
INIT:
Compile: [MKDIR] Created Dir: f: / Eclipse-Projects / Hello / Classes [Javac] Compiling 2 Source Files To f: / Eclipse-Projects / Hello / ClasseStest: [MKDIR] CREATED DIR: F: / Eclipse-Projects / Hello / Report [JUnit] Running Example.Hellotest [JUnit] Tests Run: 1, Failures: 0, Errors: 0, Time Elapsed: 0.02 Sec
Pack: [mkdir] created Dir: f: / eclipse-projects / hello / dist [jar] building jar: f: /eclipse-projects/hello/dist/hello.jar
doc: [mkdir] Created dir: F: / eclipse-projects / Hello / doc [javadoc] Generating Javadoc [javadoc] Javadoc execution [javadoc] Loading source files for package example ... [javadoc] Constructing Javadoc information ... [ javadoc] Standard Doclet version 1.4.2_04 [javadoc] Building tree for all the packages and classes ... [javadoc] Building index for all the packages and classes ... [javadoc] Building index for all classes ... [javadoc] Generating F: /eclipse-projects/Hello/doc/stylesheet.css ... [javadoc] Note: Custom tags that could override future standard tags:. @todo To avoid potential overrides, use at least one period character in (.) Custom Tag Names.
Ant sequentially performs initialization, compile, test, packages, and generates a series of tasks in API documents, which greatly improves development efficiency. When developing J2EE projects, you can also add tasks such as deployment. Also, even if you are separated from the Eclipse environment, just install Ant, configure environment variables Ant_Home =, PATH = ...;% ANT_HOME% / bin, switch to the hello directory at the command line prompt, simply type Ant .
6. Using CVS version of CVS in Eclipse is essential in team development. CVS is an excellent open source version control software. Eclipse itself has built-in support for CVS, just simple configuration, you can use CVS. First we have to install and configure the CVS server correctly, usually Linux Server comes with CVS services, but the command line operation is more cumbersome. Under Windows, there is a simple-easy CVS server. Here we recommend cvsNT, you can download CVSNT 2.0.51a, install and start CVSNT: and switch to the Repositories panel, add a repository, named / cvs-java, CVSNT will prompt to initialize this Repository, the selection is: then select "PretenD to Be a Unix CVS Version" on the Advanced panel: Then add the username and password for each developer in the Windows account. Now, the installation configuration of CVSNT has been completed. To start Eclipse, start Eclipse, we can use the original Hello project, or create a new project, then select Menu Window-> Show View-> Other, open CVS-> CVS Repositories: Then click Buttons, add a repository: Note that the username and password fill in the username and password of Windows, then select "Validate Connection On Finish", click Finish to complete: First, we must put an existing project into the CVS server and switch to Package. Explorer, select Hello Engineering, Right-click, Select Team-> Share Project ...: Use the repository you just added, continue, and add all files to CVS, and finally Eclipse Tips Commit: Fill in a simple note, Determine, then Eclipse will submit the entire project to the CVS server, you can see the icon changes in the package cost, and the Hello.java file will have a version number 1.1. Refresh in the CVS Repositories panel, you can see the project just added in:
In the team development, when a basic engineering is created and submitted to CVS, other developers must first check out this project to their respective local computers. Here we first remove the Hello project in Package Explorer, then Open CVS Repositories (If repository is added to the above method, select Hello Engineering, right click, select Check Out as ...: As a project, you can see the check out in Package Explorer. engineering. When you modify some source files, you need to submit a change to the CVS server. Select the changed file or project, right click, select Team-> Commit ...: Then fill in a simple comment, you can submit it to the CVS server, you can see the version number of the source file becomes 1.2. The above briefly introduces how to build a CVS server and how to use CVS in Eclipse, you can refer to the CVS manual to learn more about BRANCH, MERGE and other functions.