Notes 20180507 : Java programming environment construction and HelloWord Knowledge Point 20180303 : Comparing compilers, interpreters and Javac compilation principles Knowledge Point 20180303 Explain the main function in detail

3 HelloWorld  

  No matter what job you are engaged in, a working environment is always essential, even if you just want to write an article, a flat desk and a fluent writing pen can always help us to complete the work, especially Java development. Before starting today's HelloWorld, let's talk about the basic environment of Java .

3.1 Construction of Java Environment

  If a worker wants to do a good job, he must first sharpen his tools. Before learning the Java language, you must understand and build the development environment it needs. To compile and execute Java programs, JDK (Java Developers Kits) is required. The following will give a detailed understanding of JDK and JRE, and introduce how to download and install JDK and configure environment variables. In fact, many times we simply refer to JDK as the development environment of Java, which can also see its importance. Now let's talk about some important overviews related to the Java development environment, but before we start, we need to review and talk about cross-platform.

3.1.1 Java Cross-Platform

  The concept of cross-platform is an important concept in software development, that is, it does not depend on the operating system and does not depend on the hardware environment. An application 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, then the more abstract the language is, the lower the hardware control power (that is, the lower the limitation of hardware), and it is only suitable for developing highly abstract model system. Such as Java, delphi and easy language, have achieved cross-platform. They will be able to be developed, run and maintained under a variety of systems . With this understanding, let's talk about Java's cross-platform.

  Java's cross-platform means that applications written in the Java language can run on different system platforms . Here we want to interpret the platform. We know that the emergence of OS provides a series of development interfaces for all development. All current drivers and high-level development languages ​​are developed through the interfaces provided by OS, without the need for any further development. Considering the instruction set of the CPU, the platform here refers to the OS. Programs written in Java can run on different OSes. We often develop on Windows in our daily work, but deploy on Linux. The best explanation of the platform.

  The principle is: the source code written in Java generates an intermediate code that conforms to the JVM specification after one compilation, and the compiled bytecode is saved in the form of machine code optimized for the JVM. This intermediate code is the same no matter how many times it is compiled. On the platform of the Java application that needs to be run, the JVM will interpret the intermediate code as machine code that can be recognized by the computer, so the compilation is only performed once, and the interpretation is performed in the program. It needs to be done every time it is run. As shown in Figure 1.6 below.

 

 

Figure 1.6 The principle of Java's cross-platform

  Because of the JVM, the same Java program can be executed in three different operating systems. In this way, the cross-platform nature of Java programs is realized. Also known as Java has good portability. The Java language is cross-platform, while the JVM is not.

  Through the development history of Java, we know that the original intention of Java development is to cope with the rapidly developing hardware, so that the code we write will not be inappropriate as the hardware environment changes . Today, especially when the Internet has become a part of daily life, the code we write faces a large number of users, but also faces a variety of deployment environments. At this time, cross-platform is particularly important, compared to different platforms. Developing different software, developing a limited number of versions of the JVM, seems to be a little easier to develop.

3.1.2 JDK

  JDK is a software development kit for the Java language. It is provided to Java developers and is the smallest environment for Java development. It includes Java development tools and JRE (Java Runtime Environment). So after installing the JDK, there is no need to install the JRE separately. The basic components included in the JDK include:

  • javac - Compiler, converts source code to bytecode
  • jar - a packaging tool that packages related class files into one file
  • javadoc - Documentation generator that extracts documentation from source code comments
  • jdb - debugger, error checking tool
  • java - run the compiled java program (.class suffix)
  • appletviewer: applet browser, a Java browser that executes Java applets on HTML files .
  • Javah: Generates a C procedure that can call a Java procedure, or creates a header file for a C procedure that can be called by a Java program. In Java, native code can be called, and this function is used at this time.
  • Javap: A Java disassembler that shows accessible functions and data in compiled class files, while showing byte code meaning.
  • Jconsole: Java tool for system debugging and monitoring

Why is a JRE included in the JDK?

First, the developed program always needs to be run to see the effect.

Second, and most importantly, the development tools in the JDK are actually applications written in the Java language, which are packaged into exe files for convenience. If there is no JRE, then these tools cannot be run.

3.1.3 JRE

  JRE (Java Runtime Environment, Java Runtime Environment), a collection of environments necessary to run JAVA programs, including JVM standard implementation and Java core class library. If you want to run a developed Java program, you only need to install JRE in your computer.

  Simply put: Java programs developed using JDK are handed over to JRE to run.

3.1.4 JDK download

  Java's JDK, also known as Java SE (formerly known as J2SE), is a product of Sun. Since Sun has been acquired by Oracle, JDK can be found on Oracle's official website http://www.oracle.com/index.html download. After Java 5, J2SE, J2EE and J2ME were officially renamed, the 2 in the name was removed and changed to Java SE, Java EE and Java ME respectively

  The following takes the latest version of JDK as an example to introduce how to download and install JDK. The specific steps are as follows:

  • Open the IE browser, enter the URL "http://www.oracle.com/technetwork/java/javase/downloads/index.html", browse the Oracle official website, move the cursor to the Downloads menu item, and the download list drop-down menu will be displayed , click the Java for Developers hyperlink, as shown in Figure 1.7.

 

Figure 1.7 Oracle Home Page

  • It will jump to the JDK download page, click the hyperlink of the latest version of JDK in this page, that is, the Download button as shown in Figure 1.8.

 

 

Figure 1.8 JDK download page

  • In the new page you enter, you need to select the radio button in the agreement agreement first, then the page as shown in Figure 1.9 will be displayed, otherwise you cannot download when you click the link to be downloaded.

 

 

Figure 1.9 Download page of the latest version of JDK

When downloading, select the installation file suitable for your operating system platform. For example, the Windows system platform cannot install the installation file of the Linux system platform. If we need some old versions of JDK, we can select the old version to download after entering the JDK download link page, click to enter the history page, we take JDK 7 as an example as shown in Figure 1.10.

Figure 1.10 JDK download of historical versions

3.1.5 JDK environment of Windows system

3.1.5.1 JDK installation

We take the downloaded JDK 7 as an example to introduce the installation. The steps are as follows:

 

 

Announced by Oracle, JavaFX is an advanced Java user interface (UI) platform for enterprise business applications that helps developers seamlessly mix and match native Java functionality with the dynamic capabilities of Web technologies.

3.1.6 Configure JDK

  After installing the JDK, you need to configure the environment variables. The specific steps are as follows. [If you are interested, you can read my article: Why do we configure environment variables ]

3.1.6.1 The role of path environment variable configuration

  • The execution of the program needs to use the external command javac, but the javac command can only be in the bin directory under the JDK installation directory, so the program can only write to the bin directory
  • In the process of program development, the source code cannot be written into the JDK installation directory, so the source program needs to be saved to a specified directory (English directory) in any location, so the javac command needs to be run in any directory.

3.1.6.2 Path environment variable configuration method

  • By configuring the path environment variable, configure the directory where the javac command is located, that is, the bin directory under the JDK installation directory, to the path variable, so that the javac command can run in any directory.
  • Win7, win8 system: right-click on the desktop computer → select properties → select advanced system settings → select the advanced tab → click environment variables → find path in the system variables below → double-click path
  • XP system: right-click on the desktop computer → select properties → select the advanced tab → click environment variables → find path in the system variables below → double-click path to add the bin directory under the jdk installation directory to the far left and add a semicolon

 

  • The reference configuration method of the path environment variable

Create a new variable name: JAVA_HOME, add a variable value for JAVA_HOME: JDK installation directory, modify the JDK directory in the path environment variable, %JAVA_HOME%\bin;, the path environment variable is in order

 

After we successfully configured, we found that javac and java can run everywhere

 

3.1.6.3 How to configure classpath environment variables

  After we successfully configured the path environment variable, we found that java and javac can run everywhere, then we no longer need to put the source code in the bin directory, but we encountered such a problem that the tool class referenced in the source file cannot be found What should we do now? In fact, this is because Java is an object-oriented programming language that provides many development tool classes. We reference these classes in our code (these classes may not be in a file directory), so should we tell the compiler? Where to find these tool classes? The purpose of configuring CLASSPATH is to specify the path to find .class. Initially configure CLASSPATH because jre cannot find its own class library. After jdk1.5, there is no need to configure

 

CLASSPATH_8_64=.;D:\Java\JDK\JDK-1.8-64\lib;D:\Java\JDK\JDK-1.8-64\lib\tools.jar

Role: make the .class file in the classpath directory run in any directory

  • Skill: usually add the configuration directory to the front of the configuration, that is, the current directory, so that the current directory is searched first when the .class file is searched, and then it is searched according to the order of the directory configuration. order

Little knowledge: rt.jar is the JAVA basic class library, dt.jar is the class library about the runtime environment, and tools.jar is the tool class library

1. By default, rt.jar is placed on the claspath in the loading path of the root classloader. If you don’t believe me, you can remove the rt.jar in the classpath, and then run a simple class with java -verbose XXXX to know the system root of the JVM. Loader's path

Not only most of the jars under rt.jar jre\lib are in this path

2.tools.jar is used by the system when compiling a class, that is, when javac is used. In fact, some development tool classes provided by Java are placed in the tools

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 does not need to be added to the classpath

3.dt.jar is about the class library of the runtime environment, mainly the swing package. It is best to add it when you want to use swing.

  Before the jdk1.5 version, the classpath must be configured, and the JVM virtual machine can find the .class file. After the 1.5 version, the JVM virtual machine will find the .class file in the current directory by default, so we can not configure the classpath in the current 1.7 version, because it is meaningless, but It is recommended to configure and consider the issue of backward compatibility. See https://blog.csdn.net/pengpenglin/article/details/1682858 for a deeper understanding.

3.1.7 Summary

  The executable file is recorded in the path environment variable, such as .exe file. For the executable file, first look for it in the current path. If it is not found, go to the path configured in the path environment variable to find it.

  The classpath environment variable records the directory where the external tool class file referenced in the java class file is located. We know that Java is an object-oriented high-level programming language. To put it bluntly, we can program some tools that have already been written. Classes are used as objects, and objects are used to help us develop easily, just like string interception. Imported, ClassPath is used at this time, which tells the compiler where to find the .class file that needs to be executed. Here we have to distinguish the paths of .java and .class.

(1). When to use -classpath: When the class you want to compile or execute references other classes, but the .class file of the referenced class is not in the current directory, you need to import the class through -classpath
(2) .When do you need to specify the path: When the directory where the class you want to compile is located is not the same directory as the directory where you execute the javac command, you need to specify the path of the source file (CLASSPATH is used to specify the .class path, not to Specify the path of the .java file)

3.2 The first Java program "Hello World"

3.2.1 Tool Introduction

There are many tools available for writing Java programs

  • notepad (included with Microsoft operating system)
  • Notepad++
  • Eclipse
  • MyEclipse

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

3.2.2 HelloWorld

source code

copy code
copy code
class HelloWorld{
  public static void main(String[] args){     System.out.print("Hello World;");     System.out.println("Hello World;");   } }
copy code
copy code

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

At this point, we open the command line and define it in the JDK directory,

At this point, when we compile the program, we will find that

The reason is that javac.exe will only compile the files in the bin directory, and HelloWorld.java is not in the current bin directory. We put the files to be compiled in the current bin directory, and recompile and find that the encoding error is displayed. This is what we can do Solve the error prompt: encode the unmappable characters of GBK, use the command: javac -encoding utf-8 helloword.java, the compilation is successful - this sentence means to use the encoded character set utf-8 to encode HelloWord.java, and then compile and execute

Run our compiled class

 

Here we should note that when we change the source file, when we run the executable file .class, if it is not compiled, then the change is invalid, a simple example: when we add an input statement System.out to HelloWorld.java .print("uncompiled example"); at this time, we do not compile and run the executable directly,

 

The output result is what we compiled last time. We recompile and re-execute, and this change will be displayed. This is because the source code we wrote and the intermediate code executed by the JVM have to go through a compilation.

 

3.2.3 Program Analysis

copy code
copy code
class Demo{   /* First define a class class class name This class name will be the name of the bytecode file we compiled in the future

             Add a pair of curly braces {} after the class definition */

 /* Add a main (main) method/function between braces public static void main(String [] args){ } main is a method defined for a class. When a class is used as the main class in a package, there must be a main method , the main method is the entry point of the program, when the program is executed, it is called first. Then call other classes and methods in the main method to complete the program. */ public static void main(String[] args){ /* Add two lines of output statements between the braces of the main method System.out.print("hello world"); System.out.println("hello world") ;The difference between Ln is that a carriage return has Ln on behalf of a carriage return */ System.out.print("Hello World" ); System.out.println("Hello World" ); } }
copy code
copy code

When we don't enter the main method, our compilation still works, but an error is reported when the executable is executed

 

main is the entry point of the program; as if we were going to open the door when we got home, FQ would be hit.

3.2.4 Operation and working principle

 

3.2.5 Common mistakes

  • File extension hidden causes compilation to fail
  • class is wrong Class claas...
  • There is a problem with the format of the class name, all use English temporarily
  • Incorrect matching of curly braces after class name
  • malformed main method public static void main(String [] args){ }
  • main method curly braces are missing
  • The print statement is misspelled System.out.println("");
  • The use of quotation marks is wrong, use full-width quotation marks in Chinese

If the executable file cannot be generated after compilation, you can generally troubleshoot by following the steps below to find the problem:

1. First check whether the jdk environment is configured successfully

  Method: Input under DOS: java -version, to see if there is jdk version information; java, to see if there is a related command; javac, to see if there is this command; verify these three commands, you will have an intuitive understanding of the jdk environment configuration , If the relevant information is not displayed normally, you need to configure the jdk environment. For specific steps, please refer to Baidu experience.

2. Check if the path is correct

  Method: Enter: dir under DOS, check whether there really is the HelloWorld.java file in the current path

3. Use the command javac -d to check if there is a class file in the directory HelloWorld.java where you want to generate the class file

  Method: Input under DOS: javac -d C:\ HelloWorld.java , after running, see if there is a HelloWorld.class file in the C drive

In-depth knowledge points outside the topic

Knowledge Point 20180303: Comparing compilers, interpreters and Javac compilation principles

Knowledge Point 20180303 Explain the main function in detail

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337039&siteId=291194637