Java program configuration file selection of middle-strength separator

Source: Internet
Author: User

A small road hard separator, can be a full delay in the day's work time. These two days are stuck on a small program that builds a restful webservice based on Apache CXF. Fortunately, the random speculation and test, found the problem. I can not imagine, if the character back dot do not know how much time will be delayed.

The problem is the mapping file path configured in Hibernate's Cfg.xml. At that time the diagram was convenient and to prevent spelling errors, the Cfg.xml address was copied directly from the Windows browser. The path separator for Windows is "\". a path like this, if it is in a Java file, will need to use the translator, or the compilation will be error. Of course, using File.separator is the best way to do it.

String str = "Cn\\edu\\hdu\\grs\\tdlab\\domain\\hbm\\user.hbm.xml";

In the XML configuration file, however, this is not strictly checked and is configured as follows:

<mapping resource= "Cn\edu\hdu\grs\tdlab\domain\hbm\user.hbm.xml" ></mapping>

Then run the unit test to implement a simple Crud,ok pass. Finally, use Gradle's application plugin and publish it as a zip package.

I thought I was done, but when I ran the bat script, the error occurred, prompting Hbm.xml to find it.

Exception in thread "main" org.hibernate.mappingnotfoundexception:resource:cn\edu\hdu\grs\tdlab\domain\hbm\ User.hbm.xml not found

Although the error is clearly prompted to find the file, but because the unit test all OK, so did not suspect the road problem. At first I thought it was the problem of the jar bag, and then the Niang came out a bunch of hibernate articles containing hbm files in the jar package. However, the configuration of these articles is integrated with spring, and the direct use of cfg.xml I was not found.  Spring's Org.springframework.orm.hibernate3.LocalSessionFactoryBean has mappingjarlocations "1", Mappinglocations and other properties, you can also use wildcards to configure HBM files. I'm not going to use spring, however, because the project is small, beans, and doesn't require facets, I simply use a static map in main to maintain the beans.

Then I tried things without a basis, such as:

    • Add classpath,classpath* in the mapping path;

    • Merge the bin and Lib directories, modify the Classpath in the bat file, and leave the bat and jar in the same directory.

are not successful. The information that I feel useful on the internet has also been searched. No way, only one step back, the project must run up AH. So, holding the dead horse as a living horse medicine mentality, abandon package structure, the HBM files and CFG files in a directory, configuration into

<mapping resource= "User.hbm.xml" ></mapping>

Clean,distzip. Then run the startup script and succeed. That's a good luck!

This suggests that I was beginning to suspect that the cfg.xml didn't look for HBM in the jar package was wrong. Then began to doubt the problem of road, continue to add the road, use "/" to replace the original "\", the problem is solved perfectly.

But I still wonder why JUnit doesn't have a problem to run. Unit testing can also give me the solution to the problem has a great impact. Is it because I did development on the Windows platform for "2"? It seems that later to write code or to go to Linux more reliable.

Resources:

The "1" Hbm.xml file is http://blog.csdn.net/ironskinspirit/article/details/6271093 in the jar file

"2" Java Path delimiter introduction http://www.169it.com/tech-java/article-13812651845528076664.html

Java program configuration file selection of middle-strength separator

Related Article

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.