简体   繁体   中英

Netbeans - GlassFish - Java EE Configuration File

Since it was really difficult for me to find an answer to this question I'm gonna post both the question and the answer I found to this problem.

Problem: How to use a configuration file in java while working with Netbeans and deploying into a GlassFish Server?

Main problem is to actually access the file (a lot of trouble with the path in which things as getResource, creating a new File and getting it's absolute path, and many other tricks didn't work).

In this particular case I wanted the file to be in my ejb Project.

  • Create a configuration File (eg "config.properties") in ProjectName-ejb\\src\\conf

在此处输入图片说明

You will be able to see the file from Netbeans in your project configuration Files:

在此处输入图片说明

  • Insert all the properties you want:

在此处输入图片说明

  • Create an attribute in the class from which you will access the file like this: private final String BAD_WORDS_FILE_NAME = "\\META-INF\\config.properties";

Once your code is deployed to GlassFIsh, all conf files seem to be deployed to this META-INF folder: 在此处输入图片说明

  • Access Properties using sth like:

private String[] getBadWordsFromFile() throws IOException { InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(BAD_WORDS_FILE_NAME); Properties properties = new Properties(); properties.load(resourceAsStream); String badWordsAsString = properties.getProperty(BAD_WORDS_PROPERTY_NAME); return badWordsAsString.split(BAD_WORDS_SEPARATOR); }

This was the Solution I found, which worked but was only tested on a local machine... this might get some trouble on Release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM