Java getResource() unexpected path - java

I wanted the url of a resource in the following form
C:/Users/.../build/classes/jam/lena1.png
To achieve this I wrote the following code
System.out.println(getClass().getResource("lena1.png").getPath());
but it returns
/C:/Users/.../build/classes/jam/lena1.png
Why is the extra forward slash appearing before the url?

Regard that Class.getResource() returns a URL, and URLs are not only file paths: A URL involves a protocol, a host, a port, and a path. And it has its own notation and format.
What you are getting in your example is the path part of the URL, and the path always starts by a slash, according to RFC2396.
If you want to get a File from a URL, you could use new File(url.toURI())... assumming that the input URL is actually referencing a local file path.

Because it's a URL, not a filename.
The question itself is odd. What do you care what the path of the URL is?

Leading slash to denote the root of the classpath. Try this : System.out.println(getClass().getResource("/lena1.png").getPath());

use this
String s = (getClass().getResource("lena1.png").getPath()).substring(1);
System.out.println(s);

Related

Add extra '\' in absolute file path in Java

I have a absolute path coming from my JSP page to my servlet. I want to add extra "\" to access the location to my file.
String filePath=request.getParameter("file1");
/*
filePath= D:\work
*/
I want in format like "D:\\work" in my servlet so that I can access the files reside in work folder.
I tried using file.pathSeperator() and file.seperator() but did not get desired output.
filePath = filePath.replace("\\", "\\\\");
The real question is why you need this at all?

what is absolute file path in windows machine using java?

I am refering file path in two places in my program. In one place, i pass file path in FileInputStream and in another place i pass to Spring getResource() method.
If i give file path in FileInputStream like "file:/C:/myfile" it is throwing error. I had to give C:\\myfile.
But in getResource() method, if i give C:\\myfile it is throwing error i had to give file:/C:/myfile.
Why this difference? Can you please clarify?
FileInputStream is taking a String representing file path. getResource() from Spring is taking URL string representation of the resource.
Those two are not the same.

getResource("/some.jar") returns null, although "some.jar" exists in getURLs()

After hours I am giving up on debugging the following:
This works:
URL[] urls = ((URLClassLoader) MyClass.class.getClassLoader()).getURLs();
URL myURL = null;
for (URL url : urls) {
if (url.getPath().endsWith("some.jar")) {
myURL = url;
}
}
System.out.println(myURL);
returns
file:/C:/Users/Me/.m2/repository/path/to/some.jar
However, all of the following returns null:
MyClass.class.getClassLoader()).getResource("/some.jar");
MyClass.class.getClassLoader()).getResource("some.jar");
MyClass.class.getClassLoader()).getResource("/C:/Users/Me/.m2/repository/path/to/some.jar");
MyClass.class.getClassLoader()).getResource("/path/to/some.jar");
As you can see, I would like to get a jar of the user's maven repository by not adressing it absolutely, if possible. The jar is in the classpath, as shown by getURLs()
But how the heck do I have to address it in getResource() in order to get it?
Any help is appreciated!
URLClassLoader.getURLs() returns the URLs to directories and JARs that are in the classpath. ClassLoader.getResource(String) is looking for the resource inside the classpath. So unless one of your JARs/directories in the classpath contains some.jar, this is expected to fail.
Put another way, if some.jar contains pkg/thingy.png, then getResource("pkg/thingy.png") would succeed.
Also note that getResource(String) returns a URL to the resource... a URL that you already have in myURL.
I suspect you want:
MyClass.class.getClassLoader().getResource("path/to/some.jar")
Note the lack of leading slash. That's assuming the "root" of your classloader is effectively repository. It's hard to tell just from the URL.
An alternative would be to use:
MyClass.class.getResource("/path/to/some.jar")
Note that this time there is a leading slash. Basically classloaders don't have any concept of a "relative" name, whereas classes do.

getResource with parent directory reference

I have a java app where I'm trying to load a text file that will be included in the jar.
When I do getClass().getResource("/a/b/c/"), it's able to create the URL for that path and I can print it out and everything looks fine.
However, if I try getClass().getResource(/a/b/../"), then I get a null URL back.
It seems to not like the .. in the path. Anyone see what I'm doing wrong? I can post more code if it would be helpful.
The normalize() methods (there are four of them) in the FilenameUtils class could help you. It's in the Apache Commons IO library.
final String name = "/a/b/../";
final String normalizedName = FilenameUtils.normalize(name, true); // "/a/"
getClass().getResource(normalizedName);
The path you specify in getResource() is not a file system path and can not be resolved canonically in the same way as paths are resolved by File object (and its ilk). Can I take it that you are trying to read a resource relative to another path?

ServletContext getResource not working

I'm trying to use ServletContext.getResource to retrieve a java.net.url reference to an image file (which I will then include in a PDF library using iText).
When I use ServletContext.getRealPath("picture.jpg"), I get back a string URL. However, getResource always returns null.
Example 1:
String picture = ServletContext.getRealPath("picture.jpg");
// picture contains a non-null String with the correct path
URL pictureURL = ServletContext.getResource(picture);
// pictureURL is always null
Example 2:
URL pictureURL = ServletContext.getResource("picture.jpg");
// pictureURL is always null
So what is the correct way to build a java.net.URL object pointing to a file in my webapps/ folder? Why does getRealPath work but not getResource?
In case it helps at all, here is my folder structure
webapps -> mySite -> picture.jpg
Does my picture need to be stored in either WEB-INF or WEB-INF/classes to be read by getResource?
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.
So you must provide the context-relative full path. For example:
URL pictureURL = servletContext.getResource("/images/picture.jpg");
(note the lower-cased servletContext variable)
getRealPath() provides the operating specific absolute path of a resource, while getResource() accepts a path relative to the context directory, and the parameter must begin with a "/". Try ServletContext.getResource ("/picture.jpg") instead.
Doc:
getResource

Categories

Resources