4

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 RESOURCEFILE 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

gim
  • 41
  • 1
  • 2
  • 1. Set "user.dir" property within project A to point to B's directory while C loads its resource. 2. Use "classpath:resource" or any other type of access in class C instead of using system properties. – ffriend Jul 28 '11 at 02:53
  • Thank you for your answer. Can you point me to some further information about 'classpath:resource'? And if you can tell me what you mean by 'any other type of access', it will be great. – gim Jul 28 '11 at 03:38
  • My answer is related to @Stephen C's one. In your case you can just use `YourClass.class.getResourceAsStream("/resource-package/resource-name.txt")`. In some other projects, that involve major Java frameworks, you may need to use other functions and specify resource as a full URI. That is, use http: //url.of.resource.com" or "file:///path/to/file" or "classpath:resource-name.txt". But in simplest case like yours `getResourceAsStream()` is enough. Look at [this](http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java) question for more info. – ffriend Jul 28 '11 at 11:42

2 Answers2

2

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

matt b
  • 138,234
  • 66
  • 282
  • 345
  • I think he have already done this, since he is able to instantiate class from project B in main function of project A. – ffriend Jul 28 '11 at 02:55
  • Maybe I am not clear in explaining the problem. I have added the second project to the first project's build path. And that's how I am able to instantiate class 'C'. – gim Jul 28 '11 at 02:56
  • woops I pressed enter key in wrong time. sorry. The thing is when I am trying to load a file from class 'C', it tries to find a file from project 'A', not in project 'C' because the working folder is the project 'A'. – gim Jul 28 '11 at 02:58
1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you for your answer. They are not a separate command-line applications. One project has the 'main' function and the component in the other project is just instantiated from the 'main' function. I will look into Class.getResource and Class.getResourceAsStream and update the posting. Thank you again. – gim Jul 28 '11 at 03:07
  • Umm.. I got some trouble already. I don't know what to put as an argument in using Class.getResource. Can you give me a hint? I don't think I should use an absolute path. I had no idea it's so hard to load a file from a different project. – gim Jul 28 '11 at 03:29