I have my .properties file in
com.someOtherpage
-somefolder
--theProperties.java `<--- This guy needs it`
com.somepackage
WEB-INF
-config
--project.properties `<--- Here is where he sits`
when deployed how can I call the properties file with out calling its absolute path like below
public class theProperties
{
private static Properties properties = new Properties();
public theProperties()
{
}
public String get(String attribute) throws Exception
{
//what do I need to set up to be able to call this file this way
//notice there is no '../../project.properties'
// -----
InputStream is = theProperties.class.getResourceAsStream("project.properties");
properties.load(is);
is.close();
return properties.getProperty(attribute);
}
}
The above isn't working, why?
If you put the properties file in the same package as the Class that reads it you specify its path relative to that class, that is if the properties file is in the exact same package as the class loading it you specify the path as project.properties.
If you put the properties file in the default package and the loading class isn't in the default package, you have to specify an absolute path like, /project.properties. Just a reminder no classes should be in the default class path as a general rule.
Either way, your properties file has to be on the classpath which yours isn't. In other words it has to be somewhere in WEB-INF/classes/.
A better solution, but more complex is to use Guice to inject properties and not write your own reader.
here is a nice explanation of how...
http://jaitechwriteups.blogspot.com/2007/01/how-to-read-properties-file-in-web.html
Assuming you want to avoid the absolute filepath, not the absolute path within the classpath, you need to do:
theProperties.class.getResourceAsStream("/WEB-INF/config/project.properties")
The forward slash at the front is important. Without it the path is relative to the loading class's package location.
Related
I have some placeholders in my flow. Values for these placeholders are specified in a properties file (in classpath). I am doing munit using java. Currently, these placeholders are not getting replaced by values in property file. Any idea how do I load this file while running munit?
My munit is like this:
Class MyClass extends FunctionalMunitSuite{
#Override
protected String getConfigResources() {
//try 1[gave give value pairs directly]: didnt worked
System.getProperties().put("prop.key", "value");
//try2[load prop files]:didn't worked
prop.load(this.getClass().getResourceAsStream("mypropertyfile.properties"));
System.setProperties(prop);
}
}
In Overriding getConfigResources() specify a test-config.xml which has your mock connector and context property placeholder of properties file. Store that test properties file in src/test/resources
#Override
protected String getConfigResources() {
return "mule-config-test.xml" + ",actual-flow-to-test.xml";
}
Inside mule-config-test.xml, define test property file like this:
<context:property-placeholder ignore-resource-not-found="true" location="wmo-mule-app.properties,wmo-mule-app-unit-test.properties" />
In this case, wmo-mule-app.properties is my actual app property file while wmo-mule-app-unit-test.properties is the over-riding unit-test property file. This unit-test property file will take precedence over wmo-mule-app.properties
Currently you might be having your files in /src/main/resources
try to put it in /src/test/resources
I have a properties file myprops.properties as follows:
Wsdl=someurl
UserName=user
UserPassword=pasword
Application=appName
And inside my controller I'm trying to access to set values in my service as follows
Properties prop = new Properties();
prop.load(new FileInputStream("resources/myprops.properties"));
myService.setWsdl(prop.getProperty("Wsdl"));
myService.setUserName(prop.getProperty("UserName"));
myService.setUserPassword(prop.getProperty("UserPassword"));
myService.setApplication(prop.getProperty("Application"));
my Issue is I just do not know what path to use. Its a Spring project if that makes any difference. and Idealy I would like to have the properties file in my "src/main/resources" folder
I realise this may be very simple to some but I have tried searching for the solution both here and on Google and I cannot seem to find a solution that has helped. I've tried moving the file around the project but cannot seem to figure it out
The Error I get is
java.io.FileNotFoundException: resources\drm.properties (The system cannot find the path specified)
any advice/explanation or even a link that clearly explains it would be great
well, src/main/resources are on the classpath, you just need to do.
Properties properties = PropertiesLoaderUtils.loadAllProperties("your properties file name");
If you are using spring, you could set your property placeholder.
<context:property-placeholder location="classpath:resources/myprops.properties" />
and in your beans you can inject the values from the properteis using the #Value annotation
#Autowired
public Foo(#Value("${Wsdl}") String wsdl) {
...
}
in the case above I used in the constructor, but its possible to use by Autowired field/setter.
So in your service you could have something like:
#Service
public class MyService {
private final String wsdl;
private final String username;
private final String password;
private final String application;
#Autowired
public MyService(
#Value("${Wsdl}") String wsdl,
#Value("${UserName}") String username,
#Value("${UserPassword}") String password,
#Value("${Application}") String application
) {
// set it to each field.
}
}
Given that src/main/resources is on the classpath, you could do:
Resource resource = new ClassPathResource("/myprops.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
Don't use a FileInputStream; use getResourceAsStream() to read it from the servlet context.
You can always count on mkyong. This is an example/tutorial on how to load and read property files.
http://www.mkyong.com/java/java-properties-file-examples/
This question should be marked as a duplicate:
Loading a properties file from Java package
How to use Java property files?
Load a property file in Java
Java Properties File not loading
Java NullPointerException on loading properties file
Not able to load properties file in Java
--myjar.jar
--config
----appconfig.xml
I'm trying create maven based java application which can read configuration file from a relative path config\appconfig.xml
The idea here is that, the configuration can be modified without recompiling the application.
My implementation is something like this:
#Service
public class JooxXmlConfigurationServiceImpl implements XmlConfigurationService {
#Value("${configuration.xml}")
private String path;
}
Where the value is fetched from a properties file that is uder src/main/java/app-defaults.properties
It works when the String path is defined as the complete address like: C:/myprojects/myapp/config/appconfig.xml
but I need to set the path to be something like config/appconfig.xml
I'm trying to use a resource from another module to import a file. My goal is to pass the filename by each custom class, and let the base class of another module fetch the file.
But I always get a Nullpointer Exception.
What am I doing wrong?
Module A:
src/main/java/foo/bar/MyBaseClass.java
src/main/resources/foo/bar/test.xml
Module B:
src/main/java/other/path/MyCustomClass extends MyBaseClass
classes:
abstract class MyBaseClass {
public static String TESTFILE = "foo/bar/test.xml";
getData(String filename) {
InputStream inputStream = MyBaseClass.class.getResourceAsStream(String filename); //NPE
}
}
class MyCustomClass extends MyBaseClass() {
doSomething() {
getData(TESTFILE);
}
}
/edit:
should I maybe use something like this?
super.getClass().getResourceAsStream(..)
It's very likely you should be using ClassLoader.getResourceAsStream()
e.g.
Thread.currentThread().getContextClassLoader().getResourceAsStream()
(probably safer, might work in different environments more correctly, i.e., where a special classloader is being used, Java EE?)
or at least
aClass.getClassLoader().getResourceAsStream()
this is how you should load resources on the classpath which may be in a different JAR (or classpath entry) than the given class you're calling getResourceXXX on.
If you're using a class that's in module B and you want to load resources from module A, you need to use ClassLoader.getResourceXXX.
So in Java you should generally use this approach (unless you care about restricting resource loading to a smaller area...)
Another thing to be careful about: pay attention to the need for a leading "/", always double-check the javadocs of whichever method you're using
see also: http://www.xyzws.com/servletfaq/what-is-different-between-classloadergetresourceasstream-and-classgetresourceasstream/21
The getResourcesAsStream() expects a name which is NOT a filename cause it's a resource name. Furthermore it looks like you are trying to access a resource from an other maven module. And not to forget you are trying to access the resource via a relative path which should be changed into an absolute resources path like /foo/bar/test.xml instead of foo/bar/test.xml.
I have a property file that is bundled in an external jar file in my WEB-INF/lib. If I want to override the values in that property file and use my own values from my own property file. Is there a better way to do it.
For example:
in the default property file, I see
banner.ad.link={0}
I want to change it to something like:
banner.ad.link=<a class="mycss" href="{1}" title="Click here {0}">{0}</a>
So far this is what I have:
public class MainListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event)
{
ResourceBundle bundle = ResourceBundle.getBundle("com.comResources");
bundle = ResourceBundle.getBundle("org.displaytag.messages",
Locale.getDefault());
for (Enumeration<String> e = bundle.getKeys(); e.hasMoreElements() ;) {
System.out.println(e.nextElement());
}
}
}
I'm not too sure where to go from here to override the key value pair from the bundle object to have a new value from my own property file. Any help, I would be greatly appriciated! Thanks in advance!
I see three ways of doing it.
just put your properties file in the classpath, but before the properties file that you want to override. Putting it in WEB-INF/classes should work.
Change the jar file and remove the properties file from it
Create a class having the same basename as the properties file, and delegate to your properties file in the class to implement the bundle. See The ResourceBundle api doc to understand how resource bundles are loaded. The class file will be loaded before the properties file for a given bundle base name and locale.
Note that if all you want to do is to customize displaytag, you just have to put your displaytag.properties at the root of the classpath (i.e. in the default package) rather than in the org.displaytag.properties package, as documented here. Displaytag will load your bundle rather than the default one if found.