[ZT] Eclipse Quick Start Guide

Source: Internet
Author: User
Tags cdata
I have read a good article on j2medev, but I have not read it yet. I will collect it first. It briefly introduces several key eclipse usage, including installation configuration, junit, ant, cvs, etc, very good.

Eclipse is a very good open-source IDE and is very suitable for Java Development. Thanks to the plug-in technology, it is welcomed by more and more developers. The latest Eclipse 3.0 not only greatly enhances the interface, but also increases the speed of code folding and many other excellent functions. With a variety of dazzling plug-ins, it can fully meet the needs of development from enterprise-level Java applications to mobile terminal Java games. This article will show you how to develop common Java programs, Web applications, J2EE applications, mobile Java programs, and how to perform unit testing and restructuring in Eclipse, configure details such as CVS.

My development environment is JDK1.4.2 + Eclipse3.0 + Windows XP SP2. If you have any questions on other platforms, please send me a letter.

1. Install JDK1.4

Eclipse is a development environment based on the Java platform. It must also run on the Java Virtual Machine and use the JDK compiler. Therefore, we must first install JDK. JDK1.4 is currently the most stable version, and is also a required condition for Eclipse operation. Download JDK1.4 windows from SUN's official site http://java.sun.com. The latest version is 1.4.2_06. Then run j2sdk-rj4_2_06-windows-i586-p.exe. You can set the installation directory by yourself.D:/software/j2sdk1.4Directory.

Next, configure the environment variables so that the Java program can find the installed JDK and other configuration information. Right-click "my computer" and select "properties". In the displayed dialog box, select "advanced" and "environment variable" to see the environment variable dialog box:

 
The preceding user variables are valid only for the current user. The following are system variables, which are valid for all users. If you want all users to use it, click "new" under the system variable and fill in:

 
JAVA_HOME is the JDK installation directory. Many development environments that depend on JDK depend on it to locate JDK. Therefore, it must be correct.

Next, find the Path of the system variable, click "edit", and add the directory where the JDK executable file is located, namely % JAVA_HOME %/bin. My corresponding directory is D: /software/j2sdk1.4/bin, which can be appended to the Path. Separate them with semicolons:

 
NOTE: If multiple Java virtual machines are installed in the system (for example, if Oracle 9i is installed with JDK1.3), you must put the JDK1.4 path in front of other JVMs; otherwise, an error will be reported when Eclipse is started.

The last system variable is CLASSPATH. the Java Virtual Machine searches for the directory where the class file is located based on the CLASSPATH settings. However, this is not required. You can specify CLASSPATH when running the Java program, for example, when running a written Java program in Eclipse, it will automatically set CLASSPATH, but in order to run the Java program conveniently on the console, I suggest setting a CLASSPATH, set its value ". ", note that it is a point ". "indicates the current directory. Users who are familiar with Windows may think that the Java Virtual Machine will search for the current directory during the search. In fact, this is a UNIX habit, out of security considerations. Many beginners of Java are eager to write Hello and world programs in the book, but java pops up. lang. noClassDefFoundError is actually not configured with CLASSPATH, as long as you add a current directory "..

2. Install Eclipse 3.0

After the JDK is configured, the next step is to install Eclipse 3.0, you can download from the official site 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

The Eclipse SDK includes the Eclipse development environment, Java development environment, Plug-in development environment, and all source code and documents. If you need all the functions, you can download this version.

If you are just like me, just use Eclipse to develop Java applications, instead of developing Eclipse plug-ins or researching Eclipse code, downloadPlatform Runtime BinaryIn additionJDT Runtime BinaryIs the best choice.

When the slave node is running, the startup screen is displayed:

 
Wait a moment and the Eclipse interface will pop up.

If an error occurs and the startup fails, you can check the log file in the Eclipse directory. I have encountered an XmlParser exception. Check carefully and find that there is an Oracle Java Virtual Machine in the original Path, after removing it from the Path, Eclipse starts normally.

3. The first Java program

Run Eclipse and select "File", "New", "Project" from the menu to create a Java Project. I name it HelloWorld and create a New Java Class:

 
I named it HelloWorld, filled in the Package as example, hooked it with "public static void main (String [] args)", and clicked "Finish". Eclipse automatically generated the code framework, we only need to fill in the main method:

 
By default, Eclipse automatically compiles data in the background. You only need to save the data and select "Run", "Run As", and "Java Application" to view the output on the Eclipse console.

Debugging Java programs is also very simple. The Run menu contains standard Debugging commands, which can be used to conveniently debug applications in the IDE environment.

Version 1.4 supports:

Select "Window", "Preferences", find "Java", "Compiler", "Compliance and Classfiles" in the dialog box, and change the compilation option to 1.4, you can use the assert (assert) syntax of JDK1.4 to make the test more convenient:

4. Use JUnit in Eclipse

Testing plays an important role in ensuring the quality of software development, and unit testing is essential. JUnit is a very powerful unit testing package, you can test one or more methods of one or more classes, and combine different TestCase into TestSuit to automate the test tasks. Eclipse also integrates JUnit, which makes it easy to compile TestCase.

We create a Java project and add an example. Hello class. First, we add an abs () method to the Hello class to return the absolute value:

Next, we are going to test this method to ensure its functionality is normal. Select Hello. java, right-click, and choose New-> JUnit Test Case:

Eclipse will ask whether to add the junit. jar package and create a HelloTest class to test the Hello class.

Select setUp () and tearDown (), and then click "Next ":

Select the method to be tested. We select the abs (int) method and enter the following in HelloTest. java:

 

JUnit performs the test in the following order: (rough code)

Try {
HelloTest test = new HelloTest (); // create a test instance
Test. setUp (); // initialize the test environment
Test.TestAbs(); // Test A Method
Test. tearDown (); // clear Resources
}
Catch...

SetUp () is to create a test environment. Here we create a Hello class instance; tearDown () is used to clear resources, such as releasing open files. Methods Starting with test are considered as test methods. JUnit will execute the testXxx () method in sequence. In the testAbs () method, we select positive, negative, and 0 for the abs () test. If the return value of the method is the same as the expected result, assertEquals will not generate an exception.

If there are multiple testXxx methods, JUnit will create multiple XxxTest instances. Each time a testXxx method is run, setUp () and tearDown () will be called before and after testXxx. Therefore, do not depend on testB () in a testA ().

Run-> Run As-> JUnit Test directly to view the JUnit Test results:

Green indicates that the test is successful. If one test fails, the red color is displayed and the method for failing the test is listed. You can try to change the abs () code, intentionally return the error result (such as return n + 1;), and then run JUnit to report the error.

If the JUnit panel is not available, choose Window> Show View> Other to open the View of JUnit:

Through unit tests, JUnit can identify many bugs in the development phase. In addition, multiple Test cases can be combined into Test Suite to automatically complete the Test, which is especially suitable for the XP method. Each time you add a small new function or make minor changes to the Code, you can immediately run Test Suite to ensure that the newly added and modified code does not destroy the original function, this greatly enhances Software maintainability and avoids code corruption ".

5. Use Ant in Eclipse

Ant is a great batch processing command execution program on the Java platform. It can easily and automatically compile, test, package, and deploy a series of tasks, greatly improving the development efficiency. If you haven't started to use Ant now, you need to learn how to use it to bring your development level to a new level.

Ant has been integrated in Eclipse. We can directly run Ant in Eclipse.

The preceding Hello project is used as an example to create the following directory structure:


 
Create a new build. xml file and place it in the project root directory. Build. xml defines the batch processing command to be executed by Ant. Although Ant can also use other file names, compliance with standards makes development more standard and easy to communicate with others.

Generally, src stores Java source files, classes stores compiled class files, lib stores all jar files used for compilation and running, and web stores web files such as JSP files, dist stores the packaged jar file and doc stores the API documentation.

Create the build. xml file in the root directory and enter the following content:

<textarea>
<xml version="1.0">
<project name="Hello world" default="doc">

<! -- Properies -->
<Property name = "src. dir" value = "src"/>
<Property name = "report. dir" value = "report"/>
<Property name = "classes. dir" value = "classes"/>
<Property name = "lib. dir" value = "lib"/>
<Property name = "dist. dir" value = "dist"/>
<Property name = "doc. dir" value = "doc"/>

<! -- Define classpath -->
<Path id = "master-classpath">
<Fileset file = "$ {lib. dir}/*. jar"/>
<Pathelement Path = "$ {classes. dir}"/>
</Path>

<! -- Initialize the task -->
<Target name = "init">
</Target>

<! -- Compile -->
<Target name = "compile" depends = "init" Description = "compile the source files">
<Mkdir dir = "$ {classes. dir}"/>
<Javac srcdir = "$ {SRC. dir}" destdir = "$ {classes. dir}" target = "1.4">
<Classpath refID = "Master-classpath"/>
</Javac>
</Target>

<! -- Test -->
<Target name = "test" depends = "compile" description = "run junit test">
<Mkdir dir = "$ {report. dir}"/>
<Junit printsummary = "on"
Haltonfailure = "false"
Failureproperty = "tests. failed"
Showoutput = "true">
<Classpath refid = "master-classpath"/>
<Formatter type = "plain"/>
<Batchtest todir = "$ {report. dir}">
<Fileset dir = "$ {classes. dir}">
<Include name = "**/* Test. *"/>
</Fileset>
</Batchtest>
</Junit>
<Fail if = "tests. failed">
**************************************** *******************
* *** One or more tests failed! Check the output ...****
**************************************** *******************
</Fail>
</Target>

<! -- Package it into jar -->
<Target name = "pack" depends = "test" description = "make. jar file">
<Mkdir dir = "$ {dist. dir}"/>
<Jar destfile = "$ {dist. dir}/hello. jar" basedir = "$ {classes. dir}">
<Exclude name = "**/* Test. *"/>
<Exclude name = "**/Test *. *"/>
</Jar>
</Target>

<! -- Output api documentation -->
<Target name = "doc" depends = "pack" description = "create api doc">
<Mkdir dir = "$ {doc. dir}"/>
<Javadoc destdir = "$ {doc. dir }"
Author = "true"
Version = "true"
Use = "true"
Windowtitle = "Test API">
<Packageset dir = "$ {src. dir}" defaultexcludes = "yes">
<Include name = "example/**"/>
</Packageset>
<Doctitle> <! [CDATA [<Bottom> <! [CDATA [<I> All Rights Reserved. </I>]> </bottom>
<Tag name = "todo" scope = "all" description = "To do:"/>
</Javadoc>
</Target>
</Project>
</Textarea>

The preceding xml defines init (initialization), compile (Compilation), test (test), doc (generate document), and pack (Package) Tasks in sequence and can be used as templates.

Select "Hello Project", "Properties", "Builders", "New...", and select "Ant Build"

Enter Name: Ant_Builder; Buildfile: build. xml; Base Directory: $ {workspace_loc:/Hello} (Press "Browse Workspace" to select the project root Directory), because junit is used. jar package, search for the Eclipse directory, and find junit. jar, copy it to the Hello/lib directory, and add it to the Classpath of Ant:

Then hook Ant_Build In the Builder panel and remove Java Builder:
 

Compile again to view the Ant output in 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/classes

Test:
[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 cocould override future standard tags: @ todo. To avoid potential overrides, use at least one period character (.) in custom tag names.
[Javadoc] Note: Custom tags that were not seen: @ todo
BUILD SUCCESSFUL
Total time: 11 seconds

Ant executes initialization, compilation, testing, packaging, and generation of a series of API documentation tasks in turn, greatly improving the development efficiency. You can also add deployment and other tasks when developing a J2EE project in the future. In addition, even if you are out of the Eclipse environment, you only need to correctly install Ant and configure the environment variable ANT_HOME =, Path = ...; % ANT_HOME %/bin, switch to the Hello directory at the command line prompt, and simply type ant.

6. Use CVS in Eclipse

Version control is essential for team development. CVS is an excellent open-source version control software. Eclipse itself has built-in support for CVS, which can be used simply by simple configuration.

First, we need to correctly install and configure the CVS Server. Generally, the Linux Server comes with the CVS service, but the command line operations are cumbersome. A simple and easy-to-use CVS server is also available in Windows. We recommend CVSNT to download CVSNT 2.0.51a, install and start CVSNT:

Switch to the Repositories panel and add a Repository named/cvs-java. CVSNT will prompt whether to initialize the Repository. Select:

Then select "Pretend to be a Unix CVS version" on the Advanced panel ":

Then, add the user name and password for each developer in the Windows account.

Now, the installation and configuration of CVSNT have been completed. Next, start Eclipse. We can use the original Hello Project or create a new Project, and then select the menu Window> Show View> Other, open CVS-> CVS Repositories:

Click the button to add a Repository:

Note: Enter the Windows user name and password, select "Validate Connection on Finish", and click Finish:

First, we need to put an existing Project into the CVS server, switch to Package Explorer, select Hello Project, right-click and choose Team> Share Project ...:

Use the Repository we just added, continue, and add all the files to CVS. Finally, the Eclipse prompts Commit:

Enter a simple comment and confirm. Eclipse then submits the entire project to the CVS server. You can see the icon changed in Package Explorer. The Hello. java file will be followed by version 1.1. Refresh the CVS Repositories panel to see the newly added project:

In team development, after a basic project is created and submitted to CVS, other developers must first Check Out the project to their local computers for demonstration, first, delete the Hello project in Package Explorer, and then open CVS Repositories (if you do not see the Repository, add the Repository according to the above method), select Hello project, right click, select Check Out...:


As a Project, you can view the checked-out Project in Package Explorer.

After modifying some source files, submit the changes to the CVS server. Select the modified file or project, right-click and choose Team> Commit ...:

Enter a simple comment and submit it to the CVS server. The source file version is changed to 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.