This paper mainly introduces the structure of MAVEN Web project in Java EE and several other minor problems.

Source: Internet
Author: User
Tags lenovo

    Let's start with the catalogue of this essay.

1. Introduce the difference between the environment variable path and classpath in Windows.

2. May cause the command line to run Javac compile successfully, but the Java Command + is the class name of the class to be executed is invalid reason.

3, introduce the structure of MAVEN project.

4. There are two ways to add jar packages in Eclipse, the difference between add Jars and add External Jars.

5. The cause of the loss of the jar package may be caused by Eclipse releasing the project to Tomcat.

    

The difference between the environment variable path and the classpath in Windows.

Path variable: When you open the Windows cmd (Command Line window), if you want to execute an EXE file, you need to first enter the directory of the EXE file, and then enter the name of this EXE file to execute the exe file.

So the question is, why do we just have to open the cmd window, no matter what path we are currently in, the input Java command will have a corresponding hint? (That is, you do not need to go to the Java.exe directory to execute the Java.exe file) (e.g.)

          

My Java.exe file is in the C:\Program Files\java\jdk1.8.0_20\bin directory, which means that although I am now in the D-Packing directory, I entered the Java command and still executed the Java.exe file. This is where the PATH environment variable is working.

   

The above image is the setting of the PATH environment variable on my computer. One of them is%java_home%\bin, which is the bin directory under the JDK installation directory on my computer, and in this bin directory we have just executed the Java.exe file.

When we enter a command in CMD, such as the above JAVA command, the operating system will look in the current directory for the name of the Java.exe executable, if not found in the current directory, will be set in the environment variable path under the paths to find, such as the above%java_ Home%\bin path, if found, will execute the corresponding EXE file, if not found, will prompt the current input command does not exist.

In other words, the PATH environment variable can simplify the function of our input command, if we use the Java command frequently, we can set the path variable to enter only Java can be executed, and do not have to go to its directory to execute, can reduce our duplication of work.

    

Classpath variable: classpath, as the name implies, can be understood as a Java class lookup path. This is also the main difference between classpath and path, where the PATH environment variable is used by the operating system, and classpath is the path that the Java compiler uses to find the class used in the code.

Example:

1 Package com.cnblog;
2 Import Undact.utils.GetTime; 3 Public class Test {4 Public Static void Main (string[] args) {5 System.out.println (Gettime.getnowtime ()); 6 }7 }

In this test class, I imported a custom package undact.utils in the GetTime class, so when I compile this file in cmd with Javac, why can I find this class correctly, by compiling it?

Because I added the path of the Undact.utils.GetTime class to the CLASSPATH environment variable in the settings of the environment variable, the compiler would compile the test class correctly by looking for Classpath to find the class I introduced. This is also the function of the CLASSPATH environment variable.

  

Second, it may cause the command line to run Javac compile successfully, but the Java Command + is the class name of the class to be executed invalid reason.

Take the Test.java file above as an example, assuming that the file's directory is C:\Users\lenovo\Desktop\com\cnblog\Test.java, then when we enter the C:\Users\lenovo\ in cmd In the desktop directory, you can compile the Test.java file with the Javac Test.java command, and you can actually see that the Test.class file is generated in the current directory, but when you enter Java The com.cnblog.Test found that the main class was missing. This is because Test.java has its own package and does not belong to the default packages, so you have to go to the top-level directory input command in its bundle to execute, i.e. in this case you need to go to C:\Users\lenovo\ In the Desktop directory, enter the Java com.cnblog.Test command to execute correctly.

Third, introduce the structure of MAVEN project.

As shown in the diagram on the left, for the typical MAVEN Web project structure, the Src/test/java and Src/test/resource folders are used in the normal. The image on the right is a normal Web project structure, built using Eclipse for Java EE Developers version of Eclipse. About the benefits of the MAVEN build project and how to use maven I'm not talking about it here, just talking about how to understand the structure. There are a lot of people who have just built Web projects with Maven, and perhaps the most puzzling question is what is the difference between this project structure and the normal Web project structure I'm building? How do I apply my previous project experience to such a project structure?

In fact, whether it is a MAVEN Web project, or a normal Web project, after deploying to the server, its structure is similar, then why do you see from eclipse that the two structure differences are so large, deployed to the server structure is similar? This is actually the configuration option from Eclipse, which is understood in this configuration.

     

is an optional configuration for deploying a MAVEN Web project to the server. The source column represents the structure of the project you see in Eclipse, and the Deploy Path column represents the corresponding path after deployment. That is to say Src/main/java This folder will be deployed to the corresponding Web-inf/class folders when deployed to the server, and the jar packages involved in MAVEN dependencies will be deployed to the Web-inf/lib folder. The rest and so on.

Next, let's look at an optional configuration for a normal Web project when it is deployed to the server.

     

You can see that the/webcontent folder in the project was placed on the server when it was deployed to the root of the project, and the/src folder was placed under the Web-inf/classes folder on the server, with the above MAVEN project src/main/ The path to the default placement of the Java folder is the same.

Whether it's a normal Web project or a MAVEN Web project, the project structure on the server is roughly as follows:

        

This also facilitates the server to unify the management of the Web project.

For these reasons, both the MAVEN Web project and the normal Web project have a similar structure after deployment to the server. This makes it easy for us to apply the experience of a previously common Web project to a project structure such as the MAVEN web.

  

The two ways to add jar packages in Eclipse are the difference between add Jars and add External Jars.

When we need to introduce the external jar package in the project, we can use the following two kinds of selection methods,

Fig. 1 Fig. 2

Figure 1 is the option in Eclipse after clicking Add Jars, Figure 2 is the option in Eclipse after add External Jars. The comparison of the two graphs shows that the add jars is for you to select the jar package to add in the current project, and the add External jars is to select the jar package at any location. Both ways, for me personally, the usual way is to create a new Lib folder in the current project, and then copy the jar package to be added to this folder, and then use Add jars to add the Lib jar package to the project reference Library. The advantage of this is that the jar package used is always in the project and will not be deleted by mistake, which facilitates the porting of the project. The jar package introduced by the ADD External Jars method may inadvertently be deleted, such as the introduction of the jar package on the desktop, the removal of the desktop accidentally deleted jar package will cause the project to run an error.

  

V. The cause of the loss of the jar package that may result from Eclipse releasing the project to Tomcat.

As mentioned in four, the jar package introduced by the Add External Jars method is not copied when deployed to the server, causing the jar package to be lost and the jar package not found at runtime.

By introducing an external jar package in the form of a Lib folder in your project, you will automatically copy the jar package to the appropriate folder based on your configuration options when deploying to the server, without the problem of JAR packet loss.

      

      

      

This paper mainly introduces the structure of MAVEN Web project in Java EE and several other minor problems.

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.