java 使用 Intellij IDEA 时找不到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16643406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:32:50  来源:igfitidea点击:

File not found when using Intellij IDEA

javamavenintellij-idea

提问by Liping Huang

I have a file named test-mule-flow.xmlunder src/main/appfolder, and I added a unit test:

test-mule-flow.xmlsrc/main/app文件夹下有一个文件,我添加了一个单元测试:

MuleFlowTest extends FunctionalTestCase
@Override
public String getConfigResources() {
    return "src/main/app/test-mule-flow.xml";
}

@Test
public void test......

Actually those tests are executed correctly when I execute it in Eclipse(Mule Studio) and also in maven build, but unfortunately these tests are failed in Intellij IDEA version 12.

实际上,当我在 Eclipse(Mule Studio) 和 maven build 中执行这些测试时,这些测试是正确执行的,但不幸的是,这些测试在 Intellij IDEA 12 版中失败了。

And I suppose this is caused by the classloader, in IDEA, the sources files are house in target/classesfolder, and the test class are house in target/test-classes, this is maybe the reason, but how can I configure it? and it it better in maven way, I don't want to do some move configuration in idea manually.

我想这是由classloader, 在 IDEA 中,源文件在target/classes文件夹中,而测试类在文件夹中target/test-classes,这可能是原因,但是我该如何配置呢?并且它以 maven 方式更好,我不想手动在想法中进行一些移动配置。

Thanks.

谢谢。

回答by JB Nizet

It looks like you're using a relative file path to load the file. So the actual result will depend on the directory from which the JVM executing your tests is started (it's "current" directory).

看起来您正在使用相对文件路径来加载文件。因此,实际结果将取决于启动 JVM 执行测试的目录(它是“当前”目录)。

This is generally a bad idea. If the file is supposed to get bundled with the application, store it is src/main/resources and load it using the class loader:

这通常是一个坏主意。如果文件应该与应用程序捆绑在一起,请将其存储在 src/main/resources 并使用类加载器加载它:

InputStream in = MuleFlowTest.class.getResourceAsStream("/test-mule-flow.xml"));

If it's a file used only by your tests, store it in src/test/resources, and load it using the class loader (same as above).

如果它是一个仅供测试使用的文件,请将其存储在 src/test/resources 中,并使用类加载器(同上)加载它。

If you really need to load it as a file, then ensure that the current directory is always the right one, or pass a system property telling the code what base directory it should use to create an absolute path.

如果你真的需要将它作为文件加载,那么确保当前目录总是正确的,或者传递一个系统属性,告诉代码它应该使用哪个基目录来创建绝对路径。

回答by UnclickableCharacter

You have the relative path specified and you did not provide information how the file under this path is actually read.

您指定了相对路径,但没有提供该路径下的文件实际读取方式的信息。

In the case it is read from classpath, you should make sure it is there. With Maven there is a convention to place resources under src\main\resourcesor src\test\resources. Maven-aware IDEs usually add these locations to classpath automatically. If for some reason you cannot put your file into resourcesor other location included in classpath, you can manually configure source directories for project in IDEA.

如果它是从类路径中读取的,您应该确保它在那里。Maven 有一个约定将资源放在src\main\resources或下src\test\resources。Maven-aware IDE 通常会自动将这些位置添加到类路径中。如果由于某种原因您无法将文件放入resources或包含在类路径中的其他位置,您可以在 IDEA 中手动配置项目的源目录。

In the case the file is read from filesystem, you should check what you have as a working directory in corresponding Run/Debug Configuration in IDEA.

如果文件是从文件系统读取的,您应该在 IDEA 的相应运行/调试配置中检查您的工作目录。

回答by Zilvinas

Maven uses the following 4 folders for its class path:

Maven 使用以下 4 个文件夹作为其类路径:

  • src/main/java- outputs in target/classes
  • src/main/resources- outputs in target/classes
  • src/test/java- outputs in target/test-classes
  • src/test/resources- outputs in target/test-classes
  • src/main/java- 目标/类中的输出
  • src/main/resources- 目标/类中的输出
  • src/test/java- 目标/测试类中的输出
  • src/test/resources- 目标/测试类中的输出

If you want to use any other folder for your tests ( and you have a decent reason for that ) then you should specify it explicitly for maven resource plugin as described in http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

如果您想为您的测试使用任何其他文件夹(并且您有充分的理由),那么您应该为 maven 资源插件明确指定它,如http://maven.apache.org/plugins/maven-resources- 中所述插件/示例/资源目录.html

Also, you should notuse files from srcfolder in your tests, but rather from target ( and you shouldn't be specifying "target" either, just use the file name ), which is already in JVM's classpath.

此外,你应该使用来自文件的src在你的测试文件夹,而是从目标(和你不应该指定“目标”要么,只是使用的文件名),这已经是在JVM的类路径中。