Problem loading java properties - java

I am trying to load properties from a file (test.properties)
The code I use is as follows:
URL url = getClass().getResource("../resources/test.properties");
properties.load(url.openStream());
But when executing the second line I get a NPE. (null pointer exception)
I'm not sure what's wrong here... I have checked that the file exists at the location where URL points to...
Any help is appreciated....

The javadoc for Class.getResource(String) says:
Returns: a URL object or null if no resource with this name is found
Most likely, the problem is that getResource is not finding the resource it is looking for. I am very suspicious of the use of ".." in the resource name. The javadoc does not say that getResource treats "." or ".." path components as having special meaning.
It is also possible that properties is null ...

I could be wrong, but I don't believe you can use ".." like that in a call to getResource(). I suggest you try an "absolute" resource:
URL url = getClass().getResource("/path/to/resources/test.properties");

Is it perhaps the properties object that is null?

The answer to whether or not getResource will find your file depends on your system classloader. The classloader is called, but before the classloader is called, the following transformation is made on the string you pass in.
From the Class javadoc:
If the name begins with a '/' ('\u002f'), then the absolute name of
the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object
with '/' substituted for '.' ('\u002e').
So, the question becomes: will the classloader you are using be able to resolve modified_package_name/../resources/test.properties?

getClass().getResource() resolves the resource relative to the given class. Try getClass().getClassLoader().getResource().

You can also use Apache's PropertiesConfiguration

Related

Nested jar: URI

The format of the jar: URI scheme is simply defined to be jar:<url>!/[<entry>].
Is it possible to "nest" two such URIs together and refer to a file inside an archive inside an archive this way? Something like:
jar:jar:http://example.com/!/archive!/file
This should designate /file inside an archive jar:http://example.com/!/archive, that is /archive found in a file provided by http://example.com/. However, URL.openConnection throws an exception:
java.net.MalformedURLException: no !/ in spec
I also cannot replace ! with %21 since that makes the jar: URI invalid, and I am not aware of any escaping that could be performed. Is this somehow possible? Or, as a more generic question, is it possible to store a URI with !/ inside <url> so that it is preserved?
As it is stated in the source code java.net.JarURLConnection does not handle nested Jar URIs.
Your example jar:jar:http://example.com/!/archive!/fileis parsed as entry archive!/file in the archive jar:http://example.com/. The latter is not a valid jar URI. You can not either escape the !/ sequences, because the text after jar: and before !/ must be a valid URI without any translation.
You can of course provide your own URLConnection implementation that supports nested Jar URIs.
While the other answer is indeed correct in the case of JarURLConnection, the use of jar: and similar URIs is not restricted to it. Such is the case of the Apache Commons VFS:
jar:// arch-file-uri[! absolute-path]
Where arch-file-uri refers to a file of any supported type, including other zip files. Note: if you would like to use the ! as normal character it must be escaped using %21.
"Normal character" means usage of the character in path to the file inside the archive, not in the inner URI. This makes jar:jar:http://example.com/!/archive!/file valid, as it is the last occurence of ! that should be used as a delimiter.
Note that the inner URI should probably still remain unescaped, thus it is impossible to use # there.

How can I obtain the parent of the path inside an URL object in Java?

I have the following problem in a Java application
By this statment I retrieve the current location of my application:
URL location = Main.class.getProtectionDomain().getCodeSource().getLocation();
So for example my location contains a path like: /C:/Projects/edi-sta/out/production/edi-sta/
Ok, how can I easilly obtain the parent path?
For example, considering the previous path, how can I obtain the ****/C:/Projects/edi-sta/out/production/** path (that is the direct parent of the last edi-sta/ folder) ?
Java provide me something that do it out of the box or have I to implement a string manipulation?
You can use URI#resolve(String) to get a URL that points to the parent:
URL parentUrl = location.toURI().resolve("..").toURL(); // .. for parent
Use the getParentFile method from standard Java.

Jersey Path Param : Multiple backslash separated param

#Path("/assetViewCount/{path}")
above annotation can be used to retrive myValue into path for url like /assetViewCount/myValue.
How can I get myValue/nextValue from /assetViewCount/myValue/nextValue in path
Actually I found the solution to my problem, following annotation gives whatever there is after /assetViewCount/ into {path}
#Path("/assetViewCount/{path:.*}")
You can use an embedded parameter in Path like this:
#Path("/assetViewCount/{path}/nextValue")

Servlet Real Path

I am running a webapp under the directory blog. (e.g. www.example.com/blog).
I would like to get the real filesystem path of a request.
e.g. www.example.com/blog/test-file.html ->
/usr/share/tomcat7/webapps/blog/test-file.html
I tried the following:
public String realPath(HttpServletRequest request, ServletContext servletContext){
String requestURI = request.getRequestURI();
String realPath = servletContext.getRealPath(requestURI);
return realPath;
}
However this returns
/usr/share/tomcat7/webapps/blog/blog/test-file.html
What is the correct way to do this?
Short answer
To get the result you want, use HttpServletRequest#getServletPath() method as an argument to getRealPath() method.
This is the closest to what you want to accomplish (read the note below).
Explanation
The reason you're getting such path (with double blog) is that you're using the result returned by the getRequestURI() method.
The getRequestURI() method returns the path starting with application context. In your case it will be:
/blog/test-file.html
What happens then, the getRealPath() method appends the string returned by getRequestURI() method to the real/physical path to the folder, where you application resides on the file system, which in your case is:
/usr/share/tomcat7/webapps/blog/
So the resulting path is:
/usr/share/tomcat7/webapps/blog/blog/test-file.html
That is the reason of your double blog issue.
IMPORTANT NOTE
DISCLAIMER
Maybe the OP is already aware of the information written below, but it is written for the sake of completeness.
The real path you are trying to get does not mean you are getting, well, the real path on your file system. The url-pattern configured in web.xml (or if you're using Servlet 3.0+ in the related annotation) is actually a logical/virtual path, which may or may not relate to the actual, physical path on a file system, i.e. the patterns (paths) specified does not need to exist physically.
Also quote from the ServletContext.getRealPath(String) documentation (emphasis mine):
Gets the real path corresponding to the given virtual path.

Getting the inputstream from a classpath resource (XML file)

In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it?
ClassLoader.getResourceAsStream().
As stated in the comment below, if you are in a multi-ClassLoader environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader(). See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388.
ClassLoader.class.getResourceAsStream("/path/file.ext");
That depends on where exactly the XML file is. Is it in the sources folder (in the "default package" or the "root") or in the same folder as the class?
In for former case, you must use "/file.xml" (note the leading slash) to find the file and it doesn't matter which class you use to try to locate it.
If the XML file is next to some class, SomeClass.class.getResourceAsStream() with just the filename is the way to go.
ClassLoader.class.getResourceAsStream("/path/to/your/xml") and make sure that your compile script is copying the xml file to where in your CLASSPATH.
someClassWithinYourSourceDir.getClass().getResourceAsStream();
Some of the "getResourceAsStream()" options in this answer didn't work for me, but this one did:
SomeClassWithinYourSourceDir.class.getClassLoader().getResourceAsStream("yourResource");
I tried proposed solution and forward slash in the file name did not work for me, example: ...().getResourceAsStream("/my.properties"); null was returned
Removing the slash worked: ....getResourceAsStream("my.properties");
Here is from doc API:
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

Categories