Eclipse,带有链接源的java项目。类路径失败
我正在使用 Eclipse 创建一个 Java 项目。问题是:
- 当源文件夹位于 项目文件夹下,我可以正确运行 来自 elipse 的 junit 测试。
- 当来源 文件夹位于项目之外 文件夹(喜欢的源文件夹)我的 junit 测试失败。
原因很简单:junit 测试使用 java.util.Properties 和相对文件路径加载文件:data/my_soft.properties 其中data是源文件夹的名称。
在 #1 的情况下,文件 *my_soft.properties* 位于类路径上,并且可以被测试类看到。
如果未找到 #2 文件 *my_soft.properties*,则测试失败。
我已经编写了 ant 脚本来运行测试,但我想知道,是否可以修复 Eclipse?我想用Eclipse来调试,这样更舒服。而且我不知道每次都创建一个项目。我只想链接来源并运行测试。
谢谢。
I'm using Eclipse to create a Java project. The problem is:
- When source folders are inside the
project folder, I can correctly run
junit tests from elipse. - 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 classes.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您从源目录加载文件,那么这不是类路径问题 - 除非您打算将源目录包含在类路径中。尝试从类路径加载文件。
http:// /download.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
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.
在“项目属性/Java 构建路径...”下,您可以指定要包含在类路径中的外部文件夹。我想如果你在那里添加带有属性文件的目录,应该没问题。
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.