开发者

Eclipse, java project with linked source. Classpath fail

开发者 https://www.devze.com 2023-03-02 15:36 出处:网络
I\'m using Eclipse to create a Java project. The problem is: When source folders are inside the project folder, I can correctly run

I'm using Eclipse to create a Java project. The problem is:

  1. When source folders are inside the project folder, I can correctly run junit tests from elipse.
  2. When source folders are outside the project folder (liked source folders) my junit tests are failed.

The reason is simple: junit tests are loading file using java.util.Properties and relative file path: data/my_soft.properties where data is the name of the source folder.

In the case #1 file *my_soft.properties* is on the classpath and can be seen by test class开发者_如何学Goes.

In the case #2 file *my_soft.properties* is not located, tests are failed.

I've wrote ant script to run tests, but I would like to know, is it possible to fix Eclipse? I would like to use Eclipse for debugging, it's more comfortable. And I don't what to create a project each time. I just want to link sources and run tests.

Thank you.


If you're loading a file from the source directory then it isn't a classpath problem - unless you mean to include the source directory in your classpath. Try loading files off the classpath instead.

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)


Sounds like you need to amend the classpath your tests run with. Highlight the tests and go Run on the toolbar. Select run from the dropdown menu. Now in the Classpath tab you can add in your source folder so the properties file can be found. (click Advanced button to be able to add a Folder).


Try adding the folder with the properties to the classpath for the run configuration for your tests. In under run configurations select the appropriate one, go into the classpath tab, then click Advanced and add the folder that contains your data folder.


Under Project Properties/Java Build Path... you can specify an external folder to include in your classpath. I think if you add the directory with the properties file there, you should be OK.

0

精彩评论

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

关注公众号