开发者

In Eclipse, how can I access a resource in another project?

I have two projects in Eclipse and one has the class which has the 'main' function. Let's call this project as 'A'. And in this 'main' function, it instantiate a class in another project. Let's call this project and and the class, 'B' and 'C' respectively. In class 'C', it tries to load a RESOURCEFILE from the project 'B' by using 'System.property("user.dir")".

When I try to run this application from the project 'A', it can't get the R开发者_JAVA技巧ESOURCEFILE opened which is in project B because the working directory is in project 'A'. But I need to put the file in project 'B'.

I don't what is the best way to get this problem solved. Do I need to pass the information of the project B when I am running this application? I am totally lost..

I will wait for someone's help!

gim


Add the second project to the build path of the first as a project dependency.


It sounds like you haven't thought through the problem of how these projects are going to be deployed. Are they separate command-line applications? Are they separate web-apps? At they components of a larger application / web-app?

If these projects are components and the resources you are trying to access (effectively) part of your source-code base, then the best way to access them is by using Class.getResource(...) or Class.getResourceAsStream(...). For the deployed version, you simply need to ensure that the JAR file for B contains the resources, and is on the runtime classpath.

If these projects are in fact distinct, then your problem is more tricky, and the best solution depends on exactly how they are to be deployed and configured.

(The getResource(...) method gives you a URL. If you actually want to read it, use getResourceAsStream(...).)


About the argument for getResourceAsStream: The argument is a path that is resolved in the namespace of the class container; e.g. your classes directory or (when deployed) your JAR file. You can choose between a relative or absolute path. An absolute path will be resolved relative to the root of the container. A relative path will be resolved relative to the path of the parent of the class (assuming you are using Class.getResourceAsStream.

AFAIK, Eclipse automatically copies resources from your source tree into the project's classes directory, but you may want to check that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜