About java web project file path

The article goes to: http://blog.sina.com.cn/s/blog_8dc68b4501014t8f.html

When trying to parse the spring configuration file with DOM4j, when the first step is to read the file, there is a problem with obtaining the path. . . After google n+log n times of pages, I have a superficial understanding. Let me summarize it in short words (it seems that this paragraph is all nonsense...). . .

1. The storage location of the spring configuration file is determined by the contextConfigLocation in web.xml, which is generally placed in the web-inf or src root directory

2. Regarding the release of files in the src folder: the .java file in the source folder, and the generated .class file in the output folder. And copy the non-.java files in the source folder directly to the output folder (eclipse will directly copy all non-.java files, and intellij IDEA can configure which extension files are copied). The output folder generally refers to the bin of c/s and the classes of b/s.

3. When reading with SAXReader.reader, you need to know the path of the file.

I list a piece of code, let's feel it

  System.out.println(this.getClass().getClassLoader().getResource("com/lavasoft/res/a.txt"));  
  System.out.println(this.getClass().getResource("/com/lavasoft/res/a.txt"));  
  System.out.println(this.getClass().getResource("").getPath());   
  System.out.println(this.getClass().getResource("/").getPath());   
  System.out.println(this.getClass().getClassLoader().getResource("").getPath());  
  System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());  
 System.out.println(System.getProperty("user.dir")); 
  System.out.println(this.getClass().getClassLoader().getResource("com/lavasoft/res/a.txt"));
 System.out.println(this.getClass().getResource("/com/lavasoft/res/a.txt"));
 System.out.println(this.getClass().getResource("").getPath());
 System.out.println(this.getClass().getResource("/").getPath());
 System.out.println(this.getClass().getClassLoader().getResource("").getPath());
 System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
 System.out.println(System.getProperty("user.dir"));

getclass() is the current class instance and gets the directory of the current class under src; getclassloader() is the class loader and gets the src root directory. "/" represents the root directory.

How to obtain the web-inf directory is still in the process of exploration. If you have any knowledge, please do not hesitate to enlighten me.

Finally, I posted an article that I think summed it up well:

http://imyself.javaeye.com/blog/460706

Another article on the principle of class loading path jwitch:

http://www.blogjava.net/Unmi/archive/2007/09/10/144101.html

There is also this one that needs to be studied:

http://www.javaeye.com/topic/7871

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326933302&siteId=291194637