J2. The environment construction and helloword of Java programming

Source: Internet
Author: User
Tags class definition throw exception java se

No matter what the job is, a working environment is always essential, and it's very important that you just write an essay, a flat desk and a fluent writing pen that will always help us get the job done, especially in Java development. Let's talk about the basic environment building for Java development.

1.1 The construction of the Java environment

工欲善其事 its prerequisite. Before you learn the Java language, you must understand and build the development environment that it requires. To compile and execute Java programs, the JDK (Java Developers Kits) is a must. The following is a specific understanding of the JDK and JRE, and describes how to download the installation JDK and configure environment variables. In fact, many times we simply refer to the JDK as the Java development environment, which can also be seen as important. Let's talk about some important overviews related to the Java development environment.

1.1.1 Java cross-platformcross-platform concept is an important concept in software development, that is, it does not depend on the operating system and hardware environment. Applications developed under one operating system can still run under another operating system. Relatively speaking, if a computer language can be highly cross-platform without modifying the code, the more abstract the language is, the lower the hardware control is, and it is only suitable for developing highly abstract model systems. such as Java,delphi and language, have achieved cross-platform. They will be developed, run and maintained in a variety of systems. With this understanding, let's talk about the cross-platform of Java.

The cross-platform of Java means that applications written in the Java language can run on different system platforms . Here we have to interpret the platform, we know that the advent of the OS for all development provides a series of development interfaces, all the current drivers and advanced development language are implemented through the interface provided by the OS to implement the development, and no longer consider the CPU instruction set, so the platform here refers to the OS, Java-written programs can run on different OSes, which are often developed on Windows on a daily basis, but deployed on Linux, which is the best cross-platform explanation.

  The principle is: The source code written in Java after a compilation to generate the JVM specification of the intermediate code, the compiled bytecode with a JVM optimized for the machine code form saved. This intermediate code, no matter how many times the compilation is the same, on the platform of the Java application that needs to run, the JVM will interpret the intermediate code as a machine code that can be recognized by the computer, so the compilation is done only once, and the explanation is required every time the program runs. as shown in 1.6.

Figure 1.6 The cross-platform principle of Java

Because of the JVM, the same Java program can be executed in three different operating systems. This realizes the cross-platform of the Java program. Also known as Java has good portability. The Java language is cross-platform, and the JVM is not cross-platform.

Through the history of Java development, we know that Java development is designed to deal with the rapid development of hardware, you can do the code we write does not adapt to the hardware environment changes in the situation. Today, especially when the Internet becomes a part of everyday life, the code we write faces a huge number of users at the same time, also facing a variety of deployment environment, at this time cross-platform is particularly important, compared to different platforms to develop different software, a limited number of versions of the JVM, looks and easy to develop some.

1.1.2 JDK

  The JDK is the Java language SDK for Java developers, and is the smallest environment for Java development, which includes Java development tools and JRE. So with the JDK installed, you don't have to install the JRE separately. the basic components included in the JDK include:

    • javac– compiler, turn source program into bytecode
    • jar– Package tool, package the related class file into a file
    • javadoc– Document builder, Extracting documents from source notes
    • Jdb–debugger, error-checking tool
    • java– Run the compiled Java program (. class suffix)
    • appletviewer: Applet browser, a Java browser that executes the Java applet on the HTML file .
    • Javah: Produces a C procedure that can invoke a Java procedure, or a header file that can be called by a Java program to a C procedure.
    • Javap:java Disassembler, which displays the accessible features and data in the compiled class file and displays the byte Code meaning.
    • Jconsole:java tools for system debugging and monitoring

Why does the JDK include a JRE?

First, the development of the program, always need to run a look at the effect.

Second, and most importantly, the development tools in the JDK are actually written in the Java language applications, in order to facilitate the use of the package as an EXE file, if there is no JRE, then these tools can not run.

1.1.3 JRE

JRE (Java Runtime Environment,java Runtime Environment), a collection of environments necessary to run Java programs, including the JVM standard implementation and the Java Core Class library. If you want to run a well-developed Java program, you only need to install the JRE in your computer.

In simple terms: Java programs developed using the JDK are delivered to the JRE to run.

1.1.4 jdk Download

Java's JDK, also known as Java SE (formerly known as J2SE), is Sun's product, and since Sun has been acquired by Oracle, the JDK can be downloaded http://www.oracle.com/index.html the Oracle company's official website. After Java 6 was published, J2SE, Java EE, and J2ME were formally renamed, removing 2 from the name and changing them to Java SE,

Here's an example of how to download and install the JDK in the latest version of the JDK, as follows:

    • Open IE browser, enter the URL "http://www.oracle.com/index.html", browse the Oracle website, move the cursor when the Downloads menu item, the download list drop-down menu appears, click Java for Developers Hyperlinks, shown in 1.7.

Figure 1.7 Oracle Home Page

    • You will be redirected to the download page of the JDK, where you can click the latest version of the JDK's hyperlink, which is the download button shown in 1.8.

Figure 1.8 download page for JDK

    • In the new page that you enter, you need to select the radio button in the consent agreement, and the page shown in 1.9 will be displayed, otherwise you won't be able to download the link you want to download.

Figure 1.9 The latest version of the JDK download page

Download the installation files that are appropriate for your operating system platform, such as the installation files for the Linux system platform that cannot be installed on the Windows System platform. If we need some older versions of the JDK, we can go to the JDK Download link page after selecting the old version download, click on the history page, we use JDK 7 as shown in Example 1.10.

Figure 1.10 Historical version of JDK download

1.1.5 the JDK Environment for Windows systemsinstallation of the 1.1.5.1 JDK

We download the following JDK 7 as an example of installation introduction, the steps are as follows:

JavaFX, announced by Oracle, is an advanced Java user interface (UI) platform for enterprise business applications that enables developers to seamlessly blend and match native Java functionality with Web technology dynamics.

1.1.5.2 Configuring the JDK

The environment variables need to be configured after the JDK is installed, as shown in the following steps.

1.1.5.2.1 The role of the PATH environment variable configuration
    • The execution of the program requires the use of an external instruction Javac, but the javac instruction can only be written to the bin directory under the JDK installation directory.
    • During the program development process, the source code cannot be written to the JDK's installation directory, so the source program needs to be saved to the specified directory in any location (English directory), so the JAVAC directive needs to be run in any directory
1.1.5.2.2 Path environment variable configuration method
    • By configuring the PATH environment variable, the directory in which the javac instruction is located, the bin directory under the JDK installation directory, is configured under the PATH variable to enable the javac instruction to run in any directory
    • WIN7,WIN8 System: Right click on desktop computer → select Properties → select advanced system settings → select advanced tab → Click environment variables → system variables below find path→ double-click Path
    • XP system: Right click on desktop computer → select Properties → select advanced tab → Click environment variables → system variables below find path→ double-click Path to add the bin directory under the JDK installation directory to the leftmost and add a semicolon

    • How to configure the PATH environment variable in reference form

Create a new variable name: java_home, add variable value for java_home: JDK install directory, modify JDK directory in PATH environment variable,%JAVA_HOME%\BIN;,PATH environment variable has order

After our successful configuration, we found that Javac and Java can run everywhere

1.1.5.2.3 CLASSPATH environment variable configuration method

After we have configured the successful PATH environment variable and found that Java and Javac can run everywhere, then we have no need to put the source code in the bin directory, but we have encountered a problem is that the source file referenced in the tool class could not find what to do at this time? In fact, this is because Java is an object-oriented programming language, which provides a lot of development tool classes, we refer to these classes in our own code, then it is not time to tell the compiler where to find these tool classes, the purpose of configuring Classpath is to specify the path to find the. class. The CLASSPATH was originally configured because the JRE could not find its own class library. After jdk1.5, you do not need to configure the

classpath_8_64=.;D: \ Java\jdk\jdk-1.8-64\lib;d:\java\jdk\jdk-1.8-64\lib\tools.jar

Role: Enables. class files in the Classpath directory to run in any directory

    • Tip: You will typically add the configuration to the front of the directory. Configuration, even if the current directory, the. class file search for the current directory, and then based on the order of the directory configuration to find and run, so the configuration in the Classpath directory is in order

Tip: Rt.jar is the Java base Class library, Dt.jar is the class library for the runtime environment, Tools.jar is the tool class library

1.rt.jar by default in the root ClassLoader loading path is placed in Claspath is superfluous, do not believe you can remove classpath inside Rt.jar, and then use Java-verbose XXXX way to run a simple class to know Inside the path of the JVM's system root Loader

Not only Rt.jar Jre\lib, but most of the jars underneath are in this path.

2.tools.jar is used when the system compiles a class, that is, when Javac is used, in fact tools is placed in Java to provide some of the development tool classes

Javac Xxx.java

is actually running

Java-calsspath=%java_home%\lib\tools.jar Xx.xxx.Main Xxx.java

Javac is the encapsulation of the above command, so Tools.jar doesn't have to be added to Classpath.

3.dt.jar is a class library about the operating environment, mainly swing packages. When you want to use swing, it's best to add

The jdk1.5 version must be configured before the CLASSPATH,JVM virtual machine can find the. class file, after the 1.5 version of the JVM virtual opportunity to find the. class file by default in the current directory, so we now version 1.7 can not configure the classpath, because there is no meaning, But the proposed configuration, consider the problem of backward compatibility.

the difference between 1.1.5.2.4 path and Classpath

The PATH environment variable is an executable file, such as an. exe file, where the executable file is located in the current path, and if it is not found, go to the path configured in the PATH environment variable to find

The directory where the Java class's running files are recorded in the CLASSPATH environment variable

1.1.5.2.5 Why should I configure environment variables

Learn to configure environment variables when learning Java, but when you go back to organizing your notes to prepare a deep-seated study, you ask yourself a question: "Why should I configure environment variables?" ”。

When we do not configure the JDK environment variables, if we want to run Javac under DOS, then we have to go into the bin directory of the JDK before we can do the compilation, then we configure the JDK to solve this problem, after the environment variable is configured, then execute Javac, The system will search through path to find Javac, if we do not configure, then is not able to find, after the configuration system can easily find Javac, and then in our source code can compile and execute the Java code we wrote.

We know that Java is an object-oriented high-level programming language, and plainly, we can programmatically use some already written tool classes as objects, through objects to help us to develop briefly, like the interception of strings, we do not have to write their own as long as the corresponding method of invoking string can be, But we do not have a package import through some code written by non-Ides, and we use Classpath, which tells the compiler where to find the. class file to execute. Here we are going to distinguish between the. Java and. Class paths.

(1). When to use-classpath: When you are compiling or executing a class that references other classes, but the referenced class's. class file is not in the current directory, you need to introduce the class by-classpath
(2). When you need to specify a path: When you are compiling a directory that contains a class that is not the same directory as the directory where you execute the javac command, you need to specify the path to the source file (Classpath is used to specify the path of the. class path, not for the. java file)

1.2 First Java program "Hello World" 1.2.1 Tools Introduction

There are a number of tools you can use to write Java programs

    • Notepad (Microsoft OS comes with)
    • notepad++
    • Eclipse
    • MyEclipse

We learn through notepad++ at the initial learning stage, so that we can see the process of running the program more intuitively.

1.2.2 HelloWorld

Source

Class helloworld{ void main (string[] args) {System.out.print ("Hello world;"    ); System.out.println ("Hello world;"  ); }}

Store source code in K:\WorkSpace\JavaJC\Chapter 1

At this point we open the command line, defined in the JDK directory,

At this point we compile the program will find

The reason is that javac.exe it will only compile files in the bin directory, and Helloworld.java is not in the current bin directory, we will need to compile the file in the current bin directory, recompile found prompt encoding error error, which is we can resolve the error message: Encoding GBK The non-mapped characters, using the command: javac-encoding utf-8 Helloword.java, compile successfully-this sentence means using the coded character set Utf-8 to encode Helloword.java, and then compile the execution

Run the class that we compiled successfully

Here we should note that when we change the source file, we run the executable. class, if not compiled, then the change is not valid, simple example: when we add a helloworld.java in the input statement System.out.print (" Not compiled example "); At this point we do not compile, directly run the executable file,

The output is the last time we compiled the rendering, we recompile, re-execute, this change will not be shown, this is because we write the source code and the JVM executes the middle of the middle code to go through a compilation.

1.2.3 Program Parsing
Class demo{/*First, define a class class name that will be the future of our compiled bytecode file name after the class definition with a pair of curly braces {}*  /* Adds a main (main) method/function to the middle of the curly braces public static void main (String [] args) {} Main is a method defined for a class that must have a main method when the class is the main class in the package. The Main method is the entry for the program, which is called first when the program executes. Then the program is completed by invoking another class and method in the Main method. * /public static void main (string[] args) {/* Add two lines of output statement in the middle of the main method's braces System.out.print ("Hello World "); System.out.println ("Hello World"); The difference between ln is that a carriage return has an ln representing a carriage return */System.out.print ("Hello World"); System.out.println ("Hello World");}}             

Our compilation is still available when we do not enter the main method, but we report an error when executing the executable file

Main is the entrance to the program, as if we were going home to open the door, FQ would be beaten.

1.2.4 operation and working principle

1.2.5 Common Errors
    • File name extension shadowing causes compilation to fail
    • Class Claas the wrong class ...
    • The class name format has problems temporarily all use English
    • The curly braces that follow the class name are not matched correctly
    • Main method format error public static void Main (String [] args) {}
    • Main method Curly Brace missing
    • Print Statement spelling error System.out.println ("");
    • Quotation marks using error, use into Chinese full-width quotation marks

If you cannot build the executable after compiling, you can usually follow the steps below to find the problem:

1. First check that the JDK environment is configured successfully

Practice: dos Input: java-version, see if there is a JDK version of the information, Java, see if there is a relevant command, JAVAC, see if there is this command, verify that the 3 commands, the JDK environment configuration has a visual understanding, if not properly display the relevant information, To configure the JDK environment, the specific steps can refer to Baidu experience.

2. Check if the path is correct

Practice: dos Input: dir, see if there is really this Helloworld.java file under the current path

3. Through the command javac-d you want to generate the class file directory Helloworld.java to see if there is a class file

Practice: dos input: javac-d \ C Helloworld.java, after running, see if there are helloworld.class files under C disk

Question20180104 comparison compiler, interpreter and Javac compiler principle
    • 1 compiler (Compiler)
    • 2 Interpreter
    • 3 Comparison of interpreter and compiler
    • 4 Javac Compilation Principle 5 Java You're slower than C.
      • What is 4.1 javac?
    • 5 Java you are slower than C
Question20180105 The principle of cross-platform features of Java
    • 1. What is a platform
    • The principle of 2.Java cross-platform
    • 3. Advantages of cross-platform
    • 4. Talk about "Chicken" Java cross-platform
Question20180106 Configuration of Java environment Variables and why to configure environment variables
    • Q1.1 Why to configure environment variables Q1.2 Java environment variable configuration--illustrated version
      • Basic principle
    • The role of Q1.3 CLASSPATH environment variables
    • Configuration of the Q1.4 classpath
    • Q1.5 internal commands, external commands, and batch files
    • Q1.6 settings for Catalina_home environment variables (for TOMCAT6/7)
Question20171027 detailed main function
    • First, say the class:
    • Second, the main () method
    • Three, the main () method can throw Exception
    • Iv. function of string parameter array in main () method
    • V. Give another version of HelloWorld
    • Vi. Interpretation of Main

J2. The environment construction and helloword of Java programming

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.