Java 将属性文件添加到 IntelliJ 的类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3765998/
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-08-14 04:37:11  来源:igfitidea点击:

Add a properties file to IntelliJ's classpath

javaintellij-ideaclasspath

提问by Tony Ennis

I'm running a simple Java program from the IntelliJ IDE using the Run->Run menu. It works fine. Now I want to add log4j logging.

我正在使用 Run->Run 菜单从 IntelliJ IDE 运行一个简单的 Java 程序。它工作正常。现在我想添加 log4j 日志记录。

I added a resources folder under my project root. I added a log4j.properties file in that folder. I changed the code to log something.

我在我的项目根目录下添加了一个资源文件夹。我在该文件夹中添加了一个 log4j.properties 文件。我改变了代码来记录一些东西。

What is the right way to tell IntelliJ to include the resources folder in the classpath so the properties file is seen?

告诉 IntelliJ 在类路径中包含资源文件夹以便看到属性文件的正确方法是什么?

With IntelliJ 8 I could guess like a drunk monkey and eventually get it to work. I have 9 now and I am wholly unsuccessful. I've been trying for an hour. How about an "Add to classpath" option somewhere? /fume /vent /rant

使用 IntelliJ 8,我可以像喝醉的猴子一样猜测并最终让它发挥作用。我现在有9个,我完全不成功。我已经尝试了一个小时。某处的“添加到类路径”选项怎么样?/发烟 /发泄 /咆哮

采纳答案by ColinD

Try this:

尝试这个:

  • Go to Project Structure.
  • Select your module.
  • Find the folder in the tree on the right and select it.
  • Click the Sources button above that tree (with the blue folder) to make that folder a sources folder.
  • 转到项目结构。
  • 选择您的模块。
  • 在右侧的树中找到文件夹并选择它。
  • 单击该树上方的 Sources 按钮(带有蓝色文件夹)以使该文件夹成为源文件夹。

回答by CrazyCoder

Actually, you have at least 2 ways to do it, the first way is described by ColinD, you just configure the "resources" folder as Sources folder in IDEA. If the Resource Patternscontains the extension of your resource, then it will be copied to the output directory when you Makethe project and output directory is automatically a classpath of your application.

实际上,您至少有两种方法可以做到,第一种方法由 ColinD 描述,您只需将“资源”文件夹配置为 IDEA 中的“源”文件夹即可。如果资源模式包含您的资源的扩展,那么它会在您复制到输出目录制作项目和输出目录自动为您的应用程序的类路径。

Another common way is to add the "resources" folder to the classpath directly. Go to Project Structure | Modules | Your Module | Dependencies, click Add, Single-Entry Module Library, specify the path to the "resources" folder.

另一种常见的方法是将“资源”文件夹直接添加到类路径中。转到项目结构 | 模块 | 您的模块 | Dependencies,单击AddSingle-Entry Module Library,指定“resources”文件夹的路径。

Yet another solution would be to put the log4j.propertiesfile directly under the Source root of your project (in the default package directory). It's the same as the first way except you don't need to add another Source root in the Module Pathssettings, the file will be copied to the output directory on Make.

另一种解决方案是将log4j.properties文件直接放在项目的 Source 根目录下(在默认包目录中)。除了不需要在模块路径设置中添加另一个源根目录外,它与第一种方式相同,文件将被复制到 Make 上的输出目录。

If you want to test with different log4j configurations, it may be easier to specify a custom configuration file directly in the Run/Debug configuration, VM parametersfiled like:

如果要使用不同的 log4j 配置进行测试,直接在Run/Debug 配置中指定自定义配置文件可能更容易,VM 参数文件如下:

-Dlog4j.configuration=file:/c:/log4j.properties.

-Dlog4j.configuration=file:/c:/log4j.properties.

回答by Peter Thygesen

I have the same problem and it annoys me tremendously!!

我有同样的问题,这让我非常恼火!!

I have always thought I was surposed to do as answer 2. That used to work in Intellij 9 (now using 10).

我一直认为我应该按照答案 2 来做。这曾经适用于 Intellij 9(现在使用 10)。

However I figured out that by adding these line to my maven pom file helps:

但是我发现通过将这些行添加到我的 maven pom 文件有助于:

<build>
  ...
  <resources>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>
  ...
</build>

回答by James B

I had a similar problem with a log4j.xml file for a unit test, did all of the above. But figured out it was because I was only re-running a failed test....if I re-run the entire test class the correct file is picked up. This is under Intelli-j 9.0.4

我在用于单元测试的 log4j.xml 文件中遇到了类似的问题,执行了上述所有操作。但发现这是因为我只是重新运行了一个失败的测试....如果我重新运行整个测试类,就会选择正确的文件。这是在 Intelli-j 9.0.4 下

回答by prototype

Faced a similar challenge adding files with .ini extensions to the classpath. Found this answer, which is to add it to Preferences -> Compiler -> Resource Patterns -> [...] ;*.ini

面临类似的挑战,将带有 .ini 扩展名的文件添加到类路径。 找到这个答案,就是将它添加到 Preferences -> Compiler -> Resource Patterns -> [...] ;*.ini

回答by user1426162

Perhaps this is a bit off-topic, seeing as the question has already been answered, but I have experienced a similar problem. In my case only someof the unit test resources were copied to the output folder upon compilation. My persistence.xmlin the META-INFfolder got copied but nothing else.

也许这有点题外话,因为问题已经得到了回答,但我也遇到过类似的问题。在我的例子中,只有一些单元测试资源在编译时被复制到输出文件夹。我在META-INF文件夹中的persistence.xml被复制了,但没有其他内容

In the end I "solved" the problem by renaming the problematic files, rebuiling the project and then changing the file names back to the original ones. Do not ask me why this worked but it did. My best guess is that, somehow, my IntelliJ project had gotten a bit out of sync with the file system and the renaming operation triggered some kind of internal "resource rescan".

最后,我通过重命名有问题的文件、重新构建项目然后将文件名改回原始文件来“解决”了问题。不要问我为什么这有效,但确实有效。我最好的猜测是,不知何故,我的 IntelliJ 项目与文件系统有点不同步,重命名操作触发了某种内部“资源重新扫描”。

回答by DataHacker

For those of you who migrate from Eclipse to IntelliJ or the other way around here is a tip when working with property files or other resource files.

对于那些从 Eclipse 迁移到 IntelliJ 或其他方式的人来说,这是处理属性文件或其他资源文件时的一个技巧。

Its maddening (cost my a whole evening to find out) but both IDE's work quite different when it comes to looking for resource/propertty files when you want to run locally from your IDE or during debugging. (Packaging to a .jar is also quite different, but thats documented better.)

它令人抓狂(我花了一整个晚上才发现),但是当您想从 IDE 本地运行或在调试期间查找资源/属性文件时,这两个 IDE 的工作完全不同。(打包到 .jar 也大不相同,但记录得更好。)

Suppose you have a relative path referral like this in your code:

假设您的代码中有这样的相对路径引用:

new FileInputStream("xxxx.properties");

(which is convenient if you work with env specific .properties files which you don't want to package along with your JAR)

(如果您使用不想与 JAR 一起打包的特定于 env 的 .properties 文件,这很方便)

INTELLIJ

智能

(I use 13.1 , but could be valid for more versions)

(我使用 13.1 ,但可能对更多版本有效)

The file xxxx.properties needs to be at the PARENT dir of the project ROOT in order to be picked up at runtime like this in IntelliJ. (The project ROOT is where the /src folder resides in)

文件 xxxx.properties 需要位于项目 ROOT 的 PARENT 目录中,以便在 IntelliJ 中像这样在运行时被拾取。(项目 ROOT 是 /src 文件夹所在的位置)

ECLIPSE

Eclipse is just happy when the xxxx.properties file is at the project ROOT itself.

当 xxxx.properties 文件位于项目 ROOT 本身时,Eclipse 会很高兴。

So IntelliJ expects .properties file to be 1 level higher then Eclipse when it is referenced like this !!

因此,IntelliJ 期望 .properties 文件在像这样引用时比 Eclipse 高 1 级!

This also affects the way you have to execute your code when you have this same line of code ( new FileInputStream("xxxx.properties"); ) in your exported .jar. When you want to be agile and don't want to package the .properties file with your jar you'll have to execute the jar like below in order to reference the .properties file correctly from the command line:

当您在导出的 .jar 中有相同的代码行( new FileInputStream("xxxx.properties"); )时,这也会影响您必须执行代码的方式。当您想要敏捷并且不想将 .properties 文件与您的 jar 打包时,您必须像下面这样执行 jar 以便从命令行正确引用 .properties 文件:

INTELLIJ EXPORTED JAR

INTELLIJ 出口罐

java -cp "/path/to_properties_file/:/path/to_jar/some.jar" com.bla.blabla.ClassContainingMainMethod

ECLIPSE EXPORTED JAR

日食出口罐

java -jar some.jar

where the Eclipse exported executable jar will just expect the referenced .properties file to be on the same location as where the .jar file is

Eclipse 导出的可执行 jar 只会期望引用的 .properties 文件与 .jar 文件所在的位置相同

回答by Julius

I spent quite a lot of time figuring out how to do this in Intellij 13x. I apparently never added the properties files to the artifacts that required them, which is a separate step in Intellij. The setup below also works when you have a properties file that is shared by multiple modules.

我花了很多时间来弄清楚如何在 Intellij 13x 中做到这一点。我显然从未将属性文件添加到需要它们的工件中,这是 Intellij 中的一个单独步骤。当您有一个由多个模块共享的属性文件时,下面的设置也适用。

  • Go to your project setup (CTRL + ALT + SHIFT + S)
  • In the list, select the module that you want to add one or more properties files to.
  • On the right, select the Dependencies tab.
  • Click the green plus and select "Jars or directories".
  • Now select the folder that contains the property file(s). (I haven't tried including an individual file)
  • Intellij will now ask you what the "category" of the selected file is. Choose "classes" (even though they are not).
  • Now you must add the properties files to the artifact. Intellij will give you the shortcut shown below. It will show errors in the red part at the bottom and a 'red lightbulb' that when clicked shows you an option to add the files to the artifact. You can also go to the 'artifacts' section and add the files to the artifacts manually.
  • 转到您的项目设置(CTRL + ALT + SHIFT + S)
  • 在列表中,选择要向其添加一个或多个属性文件的模块。
  • 在右侧,选择依赖项选项卡。
  • 单击绿色加号并选择“罐子或目录”。
  • 现在选择包含属性文件的文件夹。(我还没有尝试包含单个文件)
  • Intellij 现在会询问您所选文件的“类别”是什么。选择“类”(即使它们不是)。
  • 现在您必须将属性文件添加到工件中。Intellij 将为您提供如下所示的快捷方式。它将在底部的红色部分显示错误和一个“红色灯泡”,单击该灯泡时会显示将文件添加到工件的选项。您还可以转到“工件”部分并手动将文件添加到工件中。

enter image description here

在此处输入图片说明

回答by user2361174

If you ever end up with the same problem with Scala and SBT:

如果您最终遇到 Scala 和 SBT 相同的问题:

  • Go to Project Structure. The shortcut is (CTRL + ALT + SHIFT + S)

  • On the far left list, choose Project Settings > Modules

  • On the module list right of that, select the module of your project name (without the build) and choose the sources tab

  • In middle, expand the folder that the root of your project for me that's /home/<username>/IdeaProjects/<projectName>

  • Look at the Content Root section on the right side, the red paths are directories that you haven't made. You'll want to put the properties file in a Resources directory. So I created src/main/resourcesand put log4j.properties in it. I believe you can also modify the Content Root to put it wherever you want (I didn't do this).

  • I ran my code with a SBT configuration and it found my log4j.properties file.

  • 转到项目结构。快捷键是 (CTRL + ALT + SHIFT + S)

  • 在最左侧的列表中,选择 Project Settings > Modules

  • 在右侧的模块列表中,选择项目名称的模块(不包括构建)并选择源选项卡

  • 在中间,展开我的项目根目录 /home/<username>/IdeaProjects/<projectName>

  • 查看右侧的Content Root 部分,红色路径是您尚未创建的目录。您需要将属性文件放在 Resources 目录中。所以我创建了src/main/resourceslog4j.properties 并将其放入其中。我相信您还可以修改 Content Root 以将其放在您想要的任何位置(我没有这样做)。

  • 我用 SBT 配置运行了我的代码,它找到了我的 log4j.properties 文件。

enter image description here

在此处输入图片说明

回答by avp

This is one of the dumb mistakes I've done. I spent a lot of time trying to debug this problem and tried all the responses posted above, but in the end, it was one of my many dumb mistakes.

这是我犯过的愚蠢错误之一。我花了很多时间试图调试这个问题,并尝试了上面发布的所有回复,但最终,这是我的许多愚蠢错误之一。

I was using org.apache.logging.log4j.Logger(:fml:) whereas I should have used org.apache.log4j.Logger. Using this correct logger saved my evening.

我使用的是org.apache.logging.log4j.Logger(:fml:) 而我应该使用org.apache.log4j.Logger. 使用这个正确的记录器拯救了我的夜晚。