how to get project's classpath under wildfly
hai_feng Jun 12, 2014 10:55 AMHi everybody!
Today I meet a problem what is described as follows:
In the past,I used the follow code to get the project's classpath:
URL resourceURL = Thread.currentThread().getContextClassLoader().getResource("");
this function can get the classpath of project on tomcat,but can't on wildfly.
Following is my test demo:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
URL resourceURL = Thread.currentThread().getContextClassLoader().getResource("com/classloader/test/TestPath.class");
URL resourceURL1 = Thread.currentThread().getContextClassLoader().getResource("");
URL resourceURL2 = Thread.currentThread().getContextClassLoader().getResource("/");
URL resourceURL3 = TestPath.class.getResource("");
System.out.println("Function-------Thread.currentThread().getContextClassLoader().getResource('com/classloader/test/TestPath.class'):");
System.out.println(resourceURL.getPath());
System.out.println();
System.out.println("Function-------Thread.currentThread().getContextClassLoader().getResource(''):");
System.out.println(resourceURL1.getPath());
System.out.println();
System.out.println("Function-------Thread.currentThread().getContextClassLoader().getResource('/'):");
System.out.println(resourceURL2.getPath());
System.out.println();
System.out.println("Function-------TestPath.class.getResource(''):");
System.out.println(resourceURL3.getPath());
}
now I deploy it on tomcat,the console print as follow:
but when i deploy it on wildfly,the console print as follow:
you can see that the function:
URL resourceURL1 = Thread.currentThread().getContextClassLoader().getResource("");
has different result between tomcat and wildfly,it can get project's classpath on tomcat,but it get "JBOSS_HOME/modules/system/layers/base/org/jboss/as/ejb3/main/timers/",we all know that this path isn't project's classpath.
So i want to know what mean of "JBOSS_HOME/modules/system/layers/base/org/jboss/as/ejb3/main/timers/",why a same fuction can get different result path on different web server?
And who can tell me how to get project's classpath(JBOSS_HOME/bin/content/testpath.war/WEB-INF/classes/) directly under wildfly?what api i can use?
and you can see that my war is located under JBOSS_HOME/bin ,the follow methor can sovle it:
public String getRealFilePath(String aFilePath) throws Exception
{
org.jboss.vfs.VirtualFile vFile = org.jboss.vfs.VFS.getChild(aFilePath);
URI fileNameDecodedTmp = org.jboss.vfs.VFSUtils.getPhysicalURI(vFile);
String path = fileNameDecodedTmp.getPath();
return path;
}
Please help!
Thanks!

