2

i have 2 projects A and B in eclipse. B has dependency to A. In project A is a text file "file.txt". This file can be loaded in project B by for example getClass().getResourceAsStream(...)... because the location of the file is in classpath. I want to provide this same file name in project B and do some action only if file exists in project B. If there is no file in project B then no file should be found during loading with getClass().getResourceAsStream(...). Currently the file will be always found.

Thank you for help.

Kind regards Sebastian


I need to provide a little bit more information about the problem. Sorry.

There is a project C which is the core. In project C there is class which do the following:

  1. Check if file.txt exists. This is the base code which checks only the file name. No package name. And i dont want to change it.
  2. if yes then do something.
  3. if now then do something else.

A depends on C and B also depends on C. If A will be executed then the base code from C will be try to find file.txt.

If B will be executed then the base code will be also try to find file.txt. If there is no file in B project then it will found because it is in A. I dont want it.

Thank your very much.

sebastian
  • 65
  • 2
  • 7

1 Answers1

0

Add the file in project B in a different classpath location, you try first the location in project B and then check the file in project A. Say the location is com.myPackage.file1 in project B and com.myPackage.projA.file1 in project A.

Victor Ionescu
  • 1,967
  • 2
  • 21
  • 24
  • Thank you for your answer. This is not exactly solution for me. Please take look up i have edited my question. – sebastian Mar 10 '11 at 09:30
  • You could try to call getClass().getClassLoader().getResources() which returns all the resources found. Eventually you could play with the classloaders, making the method recursive by walking all the classloader hierarchy via the getParent() method of ClassLoader. – Victor Ionescu Mar 10 '11 at 10:42
  • Thank you for answer. I'm calling `getClass().getClassLoader().getResources("file.txt")`. Then i iterate through all paths in classpath for this file and check from which project comes this file. – sebastian Mar 10 '11 at 11:33
  • Does it work? If yes, you should make sure that it works also on the production server, because the classloaders can be such a mess. – Victor Ionescu Mar 10 '11 at 11:58