Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does class.getResource("." /*dot*/) have any special meaning?

Sometimes I see this in other people's code. But when I try, it returns null.

baseUrl = org.company.UploadService.class.getResource(".");
url = new URL(baseUrl, "http://192.168.164.32:9080/mka-web/services/UploadService?wsdl");
like image 840
basin Avatar asked Sep 30 '22 13:09

basin


2 Answers

getResource(".") will, in some cases, return a URL pointing to the directory that class file is in.

This thread describes the behaviour as:

a URL pointing to the directory in the class path used to load the class if the class is loaded from a directory, or null when loaded from a jar.

The last part is likely why you're getting null. Another reason could be:

It does not work with all JVMs

like image 156
Joe Avatar answered Oct 03 '22 16:10

Joe


Line UploadService.class.getResource(".") returns URL matches package of class UploadServiece, i.e. something like

file://your-path/org/company if you are running from file system or jar:file://yourjar-path/org/company if you are running from jar.

Constructor of URL that you are using returns the url in given context. For example

new URL(new URL("http://google.com"), "yahoo.com") returns http://google.com/yahoo.com, but new URL(new URL("http://google.com"), "http://yahoo.com") returns http://yahoo.com because the second URL is absolute.

So, I your case the code does not make any sense: it is absolutely equialent to new URL("http://192.168.164.32:9080/mka-web/services/UploadService?wsdl")

like image 28
AlexR Avatar answered Oct 03 '22 15:10

AlexR