【发布时间】:2013-09-27 07:05:53
【问题描述】:
我正在尝试从另一个项目的类文件中获取输入流。
我正在使用日食。输出文件夹是:
mycurrentproject/WebContent/WEB-INF/classes.
导出库文件夹为:
mycurrentproject/WebContent/WEB-INF/lib.
当我打印“java.class.path”时,我得到了这个:
D:\apache-tomcat-7.0.42\bin\bootstrap.jar;D:\apache-tomcat-7.0.42\bin\tomcat-juli.jar;
我的WINDOWS系统CLASS PATH环境变量是:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;D:\work\workspace\myjar
我在包action 中获取资源流的代码是:
classfilePath = "/cc/Person.class";
InputStream isInputStream = ModifyMethodTest.class.getResourceAsStream(classfilePath);
包action在“mycurrentproject/WebContent/WEB-INF/classes”中输出。 action.jar 在“mycurrentproject/WebContent/WEB-INF/lib”中导出。
当cc/Person.class 在“mycurrentproject/WebContent/WEB-INF/classes”中时,我得到了正确的结果。当cc/Person.class 在“mycurrentproject/WebContent/WEB-INF/lib”或“D:\work\workspace\myjar”中时。 isInputStream 为空。我想从另一个项目中的类文件中获取输入流。类文件可能在文件夹中,也可能在目标项目文件夹中的 jar 文件中。该项目中应该有许多类或 jar 文件。怎么做?现在,为了简单起见,我如上所述测试我的想法,将cc/Person.class 放入“D:\work\workspace\myjar”。但它也失败了。谁有类似的经验或建议?谢谢。
编辑:
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);
这里 isInputStream 仍然为空。 getResourceAsStream() 的参数是String name。可能是什么?类似相对路径的东西?有参考吗?
EDIT2:
它适用于以下代码:
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);
【问题讨论】:
-
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);不行。 -
我已经编辑了我的问题。
-
它适用于 EDIT2 中的代码。我只是不知道为什么。
-
是的,您有没有关于本案原因的参考资料?
-
好的,谢谢您的宝贵时间。
标签: java inputstream aop javassist getresource