Unit01:java Development Environment Case

Source: Internet
Author: User

Top

JAVA Fundamental DAY01
    1. JDK and Eclipse directory structure operations
    2. Installation and configuration of JDK
    3. Console version of Java HelloWorld
    4. Developing Java applications with eclipse

1 JDK and Eclipse directory structure Operations 1.1 issues

In order to master the directory operation under Linux, this case needs to complete the following actions:

    • Under the Linux system, browse the directory structure of the JDK.
    • Under Linux systems, browse the directory structure of Eclipse.
1.2 Solutions

To complete this case, you need to use some common Linux commands. These commands are as follows:

    • PWD: Displays the current working path.
    • CD: Changes the current working directory.
    • ls: Browse directory structure.
1.3 Steps

The implementation of this case needs to follow the steps below.

Step One: Browse the directory structure of the JDK

First, open the terminal, as shown in effect 1.

Figure-1

Then, use the PWD command to see where the directory is currently located. The effect-2 shows:

Figure-2

As can be seen from Figure 2, the current directory is/home/soft01, and this directory is the user home directory. Because the JDK directory is in the/OPT directory, you first need to use the CD command to enter the OPT directory, and then use the CD command again to access the JDK directory under the OPT directory. The interface effect-3 shows:

Figure-3

After entering the JDK directory, use the LS command to view the relevant files and directories in the current directory, as shown in effect 4:

Figure-4

In Figure 4, Blue is identified as a folder, Black is identified as a file, green is identified as an executable file, and red is identified as a compressed package.

Step Two: Browse the directory structure of Eclipse

Eclipse is also in the OPT directory, and we are currently under the/OPT/JDK directory. Therefore, you first need to enter the/OPT directory before you can enter the Eclipse directory under OPT.

First, use the command CD: Return to the previous level directory, the/OPT directory, as shown in effect 5:

Figure-5

Then, use the CD command to enter the Eclipse directory, as shown in 6:

Figure-6

After entering the Eclipse directory, use the LS command to view the current directory structure, as shown in effect 7:

Figure-7

1.4 Full Code

The code in this case is command code, so there is no full code rendering.

2 installation and configuration of JDK 2.1 issues

Install the JDK and configure the environment variables.

2.2 Solutions

Complete the construction of the Java development environment according to the instructor's lectures and the steps in this document.

2.3 Steps

The implementation of this case needs to follow the steps below.

Step one: Download and install the JDK

Learn the Java language to start with the Java SE platform.

Oracle provides two products--JRE and JDK for the Java SE platform, which can be downloaded free of charge from the official website (http://www.oracle.com/technetwork/java/index.html). The JRE (Java SE runtime Environment) is called the Java SE Runtime environment and provides the software environment necessary to run Java applications, including Java virtual machines (JVMs) and Rich class libraries (Libraries). Whether you are developing a Java application or just running a Java application that has already been developed, you must install the JRE. The JDK (Java Development Kit), called the Java SDK, is a superset of the JRE, or the JDK contains the JRE. In addition to all the content that contains the JRE, the JDK provides the compiler and debugging tools necessary to write Java programs. For people writing Java development, it is generally necessary to download the JDK, the current mainstream version is JDK 6.

Java officially provides JDK versions for different operating system platforms, such as Windows, Linux, Solaris, etc., when downloading the JDK, you can choose according to your own needs, 8:

Figure-8

In the case of the Windows version of the JDK, installation is required after the download is complete. As with installing other software under Windows, select the installation directory based on the prompts from the installation interface (-9), then "Next", then "next" until the entire installation process is complete. The effect-9 shows:

Figure-9

It is important to note that in the list of JDK installation features in Figure 9, Java DB can be chosen not to be installed, which is a relational database of Java-native implementations of the JDK, which is typically not used in Java development. Also, for the Windows version of the JDK installer, in addition to installing a JDK that already contains a JRE, install a separate JRE (the so-called "common JRE") and register it in the registry. The purpose is for some applications that need to use the JRE to automatically find the JRE through registration information.

The "Public JRE" is an option that you can choose not to install, after all the JDK already contains a full JRE (except that it is not registered in the registry), and if it is selected, the installer also prompts the user to select the installation directory for the public JRE after the JDK is installed.

In addition to downloading the JDK, developers typically download the official Java documentation, which is essential for learning and developing the Java language. The specific download location for the official Java document is shown in the Http://www.oracle.com/technetwork/java/javase/downloads/index.html page, 10:

Figure-10

Document download will be rendered in HTML format, using the browser to open the Document Home page (index.html), you can see the "JDK Panorama" shown in 11, click the relevant link to learn its details.

Figure-11

As can be seen from Figure 11: The JDK contains the JRE and Development Kit (Tools & Tool APIs), while the JRE contains a Java Virtual machine (JVM) and various class libraries (Libraries).

Step Two: Configure environment variables

After the JDK installation is complete, you can see the following in the JDK installation path:

    • Bin directory: Used to hold JDK tool commands, such as the Javac command for compiling Java programs, Java commands to start the JVM running Java programs, Javadoc commands for generating documents, and JAR commands for packaging, and so on;
    • JRE directory: Used to store the JRE contained in the JDK, which contains the JVM and the core class library;
    • LIB Directory: the Toolkit (tool APIs) for storing JDK tool commands;
    • Demo directory: For storing some sample programs;
    • Src.zip file: The Java source code used to hold the core class library.

The Javac command in the bin and the Java command are the commands we will use soon. If you want to use these commands, you need to let the operating system find the path where the command files are located. In the Windows operating system, you can configure the PATH environment variable to implement it. The PATH environment variable is a list of directories separated by semicolons (;), and when you run a command through the console, Windows looks for the command in each of the directories in the PATH environment variable, and if it does, there is a 12 error message:

Figure-12

In Windows System, open the dialog box shown in 13 via control Panel → system → advanced system settings:

Figure-13

Click the Environment Variables button in Figure 13, and in the popup dialog box, double-click the system variable path and edit it. Append the JDK development tool path at the end of the path variable value ("JDK installation path \ Bin"), assuming that the JDK installation path is: "D:\Program files\java\ jdk1.6.0_24" The path path to append is: ";D: \program Files\java\jdk1.6.0_24\bin ". Note that the paths need to be separated by semicolons. The effect-14 shows:

Figure-14

When the environment variable is set up, open the console via the "accessories" → "Command Prompt" (or run the cmd command), type the Java or Javac command, and see the output of the normal prompt indicating that the environment variable is configured successfully. The effect-15 shows:

Figure-15

It is important to note that if you choose to install a public JRE when installing the JDK, you do not need to configure the PATH environment variable to run the Java command because the public JRE path is written to the registry.

2.4 Full Code

This case is practical, so there is no full code rendering.

3 Console version of Java HelloWorld3.1 issues

Use VI to write the Helloworld.java program, after running, in the console output "Hello World".

3.2 Solutions

Please follow the instruction of the teacher and the steps of this document, and slowly realize the charm of Java.

3.3 Steps

The implementation of this case needs to follow the steps below.

Step One: Open VI, enter edit mode

First, open the terminal, and then, after entering VI and file name Helloworld.java in the terminal, go to the VI Full Screen editing screen, the terminal interface-16 shown.

Figure-16

VI Editing interface-17 is shown.

Figure-17

Step Two: Write the code that creates the class

First, in the VI editing interface, input command I, the vi into the INSERT mode, and then, type the code to create the class, interface-18 is shown.

Figure-18

The Java language is a purely object-oriented language, and any piece of Java code needs to start with defining a class. As for the details of the "class" will be detailed in the following course content, here can be tentatively understood.  public class class name {...} is to define the syntax of the class, except that the class name can be specified by itself, the other public and class are Java keywords (the so-called Java keyword refers to the Java language predefined to represent the specific meaning of the character), here just make sure not to write wrong.

Also, it is important to note that the Java language is strictly case-sensitive, and that "H" and "H" are two different characters that need to be noted when writing.

Step three: Define the Main method

Next, in the HelloWorld class, define the entry method for the Java application, main, code-19:

Figure-19

The exact meaning of this line of code needs to be described in the following course content, just remember two points: first, if a class contains such a method, then the class is a class that can be run, and the method is the portal of the program, that is, the program from the first line of the method of code to run line by row. Second, slowly write, do not write wrong!

Step four: Output information to the console

In the main method, add code to output "Hello world" to the console interface shown in code 20.

Figure-20

Step Five: Save the code, Exit VI

First, the current VI in insert mode, click the ESC key to go to the command line mode, then, click: Colon key into the last line mode; Finally, enter the command Wq, save and Exit VI, back to the terminal interface, 21.

Figure-21

At this point, save the Helloworld.java file in the current directory, as shown in 22.

Figure-22

Step Six: Compile the source file

When the source file is written, it needs to be compiled to convert to a bytecode file that can be run in the JVM. This can be done with the Javac command in the JDK tool (under the bin subdirectory of the JDK installation directory).

At the terminal, then type the following command:

    1. Javac HelloWorld. Java

When the command runs successfully, the Helloworld.class file is generated in the current directory. Of course, there may be a variety of errors for beginners. You need to be patient with the error message (most of the errors may be due to a character write error, such as a case-size problem).

Step Seven: Start the JVM, run the Java program

Once the compilation is successful, you can run the bytecode file with the Java command (more precisely, start the JVM running the bytecode file).

Then at the terminal, type the following command:

    1. Java HelloWorld

If the code is written correctly, after executing the above command, the terminal displays the content-23 shown.

Figure-23

As can be seen from Figure 23, the output of the program is "Hello World".

3.4 Full Code

The complete code for this case is as follows:

Code

4 using Eclipse to develop Java application 4.1 issues

In the previous case, "Console edition Java Hello World", we used the simplest editing tools to write the source code and experienced the entire process of compiling and running Java programs by invoking the JDK Tools command. Theoretically, we can use this way to write any Java program, but in real enterprise projects, this primitive development method will inevitably bring a lot of tedious, repetitive, error-prone operation, will greatly reduce the work efficiency, but also not conducive to the overall management of the project. In real-world scenarios, developers are always using powerful "integrated development environments" for code authoring, debugging, testing, committing, refactoring, and so on, such as Eclipse.

This case requires the use of development tools eclipse to write the Helloworld.java program. In the Eclipse console, the program's output is 24:

Figure-24

4.2 Solutions

Follow the instructions of your instructor and the steps in this document to learn more about the ease with which the Eclipse tools develop Java programs.

4.3 steps

The implementation of this case needs to follow the steps below.

Step one: Open development tools Eclipse

The following dialog box pops up after Eclipse starts, as shown in 25.

Figure-25

The popup Dialog "Workspace Launcher" is used to select the "Workspace" (Workspace). The so-called "workspace" refers to the path that Eclipse uses to store the project. Eclipse organizes the material through project. The source files, compiled generated class files, etc. written by the programmer are stored in the project folder in a specific directory structure.

Step Two: Create a Java project

Select Menu Action File (New Java project is used to create a project that is suitable for writing Java basic applications. In the dialog box that pops up, fill in the Project name (project name) and click the "Finish" button. -26 Shows:

Figure-26

When the project is created, it generates a folder with the same name as the project names in the workspace directory (specified at eclipse startup). Open this folder and you will see the directory structure shown in-27:

Figure-27

Where: the "src" folder is used to store user-written Java source files; the "bin" folder is used to store the class files generated by eclipse Auto-compiling. Eclipse has the ability to compile automatically, and when a user writes a Java source file, Eclipse automatically compiles the file by invoking the Java compiler of the system and stores the compiled class file in the Bin directory. In addition, the ". Classpath" and ". Project" two files, as well as the contents of the ". Settings" folder, are used by eclipse to maintain engineering information and can generally be ignored.

Step three: Create a class named HelloWorld

After the project has been created, you can create a Java source file from the menu operation "File" (New Class). In the popup dialog box, fill in the class name (name) and package name of the Java class you want to create, as shown in 28. (The meaning of the package will be detailed in the follow-up course.) )

Figure-28

After completing the class name ("HelloWorld" in this example) and the package name ("Day01" in this example), click the "Finish" button. Eclipse creates a source file named Helloworld.java.

Step Four: Defining classes

Write the HelloWorld program in Eclipse, as shown in 29.

Figure-29

After the program is written, verify that there is no compile error, you can run the program using the menu action "Run as (Java application"). The results of the run are displayed in the console view below the code. It should be noted here that this operation is essentially not different from the previous run of the Java program through the command line, it can be understood that eclipse calls the JDK Java command, with a more user-friendly graphical interface to implement the operation of the Java program.

4.4 Full Code

The complete code for this case is as follows:

Code

Unit01:java Development Environment Case

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.