java - Loading class as resource fails -
i've wasted time on one. why code fails result url class?
package org.test; import java.net.url; public class testa { public testa() { string resourcename = "org/test/testa.class"; string clazz = "org.test.testa"; try { class.forname(clazz); } catch (classnotfoundexception e) { e.printstacktrace(); } url url = this.getclass().getresource(resourcename); system.out.println("why null? >>> " + url); } public static void main(string[] args) { new testa(); } }
it work , print url 1 correction below.
*. prefix "/" resourcename string.
string resourcename = "/org/test/testa.class";
or
*. string resourcename = "testa.class";//no need mention full path - resources work relative path
reason (from javadoc)
if name begins '/' ('\u002f'), absolute name of resource portion of name following '/'. otherwise, absolute name of following form: modified_package_name/name
where modified_package_name package name of object '/' substituted '.' ('\u002e').
Comments
Post a Comment