Reputation: 3736
I am trying to get inputstream from class file of the other project.
I am using eclipse. The output folder is:
mycurrentproject/WebContent/WEB-INF/classes
.
The export library folder is:
mycurrentproject/WebContent/WEB-INF/lib
.
When I print "java.class.path", I got this:
D:\apache-tomcat-7.0.42\bin\bootstrap.jar;D:\apache-tomcat-7.0.42\bin\tomcat-juli.jar;
My environment variable of CLASS PATH of WINDOWS system is:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;D:\work\workspace\myjar
My code in package action
to get resource stream is:
classfilePath = "/cc/Person.class";
InputStream isInputStream = ModifyMethodTest.class.getResourceAsStream(classfilePath);
Package action
is outputed in "mycurrentproject/WebContent/WEB-INF/classes". action.jar
is exported in "mycurrentproject/WebContent/WEB-INF/lib".
When cc/Person.class
in "mycurrentproject/WebContent/WEB-INF/classes", I got right result. When cc/Person.class
in "mycurrentproject/WebContent/WEB-INF/lib" or in "D:\work\workspace\myjar". isInputStream
got null. I want to get inputstream form a class file in the other project. The class file maybe in a folder or in a jar file in the target project folder. There should be many classes or jar files in that project. How to do that? For now, and for simple, I test my idea as above to put cc/Person.class
in "D:\work\workspace\myjar". But It failed either. Any one have similar experience or advices? Thanks.
EDIT:
classfilePath ="file:D:/work/workspace/myjar/cc/Person.class";
URL[] urls = new URL[] { new URL(classfilePath) };
URLClassLoader ucl = new URLClassLoader(urls);
InputStream isInputStream = ucl.getResourceAsStream(classfilePath);
Here isInputStream still got null. The parameter of getResourceAsStream() is String name
. What could be? Something like relative path? Any references?
EDIT2:
It works with code as follow:
String Path1 = "file:D:/";
String Path2 = "work/workspace/myjar/cc/Person.class";
URL[] urls = new URL[] { new URL(Path1) };
URLClassLoader ucl = new URLClassLoader(urls);
InputStream isInputStream = ucl.getResourceAsStream(Path2);
Upvotes: 1
Views: 1943
Reputation: 168825
Use an URLClassLoader
. Once you've established it, call:
getResourceAsStream("/work/workspace/myjar/cc/Person.class")
For the InputStream
.
Upvotes: 1