向IntelliJ的类路径添加一个属性文件。

时间:2023-02-09 21:47:18

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。属性文件在该文件夹中。我修改了代码来记录一些东西。

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岁了,完全没有成功。我已经试了一个小时了。在什么地方添加“类路径”选项呢?/通风/通风/咆哮

9 个解决方案

#1


65  

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.
  • 单击树(与蓝色文件夹)上方的source按钮,使该文件夹成为源文件夹。

#2


42  

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 Patterns contains the extension of your resource, then it will be copied to the output directory when you Make the project and output directory is automatically a classpath of your application.

实际上,您至少有两种方法来完成它,第一个方法是ColinD描述的,您只需将“resources”文件夹配置为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.

另一种常见的方法是直接将“资源”文件夹添加到类路径。到项目结构|模块|你的模块|依赖,点击添加,单条目模块库,指定路径到“资源”文件夹。

Yet another solution would be to put the log4j.properties file 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 Paths settings, the file will be copied to the output directory on Make.

另一个解决方案是使用log4j。属性文件直接在项目的源根目录下(在默认的包目录中)。它与第一种方法相同,只是不需要在模块路径设置中添加另一个源根,该文件将被复制到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 parameters filed like:

如果您想要使用不同的log4j配置进行测试,那么在运行/调试配置中直接指定自定义配置文件可能更容易,VM参数是这样的:

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

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

#3


35  

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>

#4


14  

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)
  • 进入你的项目设置(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.
  • 在右边,选择Dependencies选项卡。
  • Click the green plus and select "Jars or directories".
  • 点击绿色加号并选择“jar或目录”。
  • 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).
  • Intellij现在会问你所选文件的“类别”是什么。选择“类”(即使它们不是)。
  • 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.
  • 现在必须将属性文件添加到工件。Intellij会给你下面的快捷方式。它将显示底部红色部分的错误和一个“红色灯泡”,当单击时,您可以选择将文件添加到工件。您还可以访问“artifacts”部分,并手动将文件添加到工件。

向IntelliJ的类路径添加一个属性文件。

#5


12  

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 ->编译器->资源模式->[…];* . ini

#6


2  

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)

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

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。属性需要在项目根的父目录中,以便在运行时像这样在IntelliJ中被选中。(项目根是/src文件夹所在的地方)

ECLIPSE

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

当xxxx出现时,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:

这也会影响您在拥有相同代码行(新FileInputStream(“xxxx.properties”)时必须执行代码的方式;)在你出口. jar。当您想要变得敏捷时,并不想将.properties文件打包到您的jar中,您将不得不执行下面的jar文件,以便从命令行正确地引用.properties文件:

INTELLIJ EXPORTED JAR

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文件所在的位置。

#7


2  

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)

    项目结构。快捷方式是(CTRL + ALT + SHIFT + S)

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

    在最左边的列表中,选择Project Settings >模块。

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

    在模块列表右侧,选择项目名称的模块(没有构建)并选择sources选项卡。

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

    在中间,展开你的项目的根目录,这是/home/ <用户名> /IdeaProjects/

  • 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/resources and 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).

    看右边的内容根部分,红色的路径是你没有做的目录。您将希望将属性文件放在资源目录中。所以我创建了src/main/resources,并将log4j。属性。我相信您也可以修改内容根来将其放到您想要的地方(我没有这样做)。

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

    我用一个SBT配置运行我的代码,它找到了我的log4j。属性文件。

向IntelliJ的类路径添加一个属性文件。

#8


1  

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 some of the unit test resources were copied to the output folder upon compilation. My persistence.xml in the META-INF folder got copied but nothing else.

也许这有点离题了,因为这个问题已经回答过了,但我也遇到过类似的问题。在我的情况下,只有一些单元测试资源在编译时被复制到输出文件夹。我的坚持。在META-INF文件夹中的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项目与文件系统有点不同步,重命名操作触发了某种内部的“资源重新扫描”。

#9


0  

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。

#1


65  

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.
  • 单击树(与蓝色文件夹)上方的source按钮,使该文件夹成为源文件夹。

#2


42  

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 Patterns contains the extension of your resource, then it will be copied to the output directory when you Make the project and output directory is automatically a classpath of your application.

实际上,您至少有两种方法来完成它,第一个方法是ColinD描述的,您只需将“resources”文件夹配置为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.

另一种常见的方法是直接将“资源”文件夹添加到类路径。到项目结构|模块|你的模块|依赖,点击添加,单条目模块库,指定路径到“资源”文件夹。

Yet another solution would be to put the log4j.properties file 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 Paths settings, the file will be copied to the output directory on Make.

另一个解决方案是使用log4j。属性文件直接在项目的源根目录下(在默认的包目录中)。它与第一种方法相同,只是不需要在模块路径设置中添加另一个源根,该文件将被复制到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 parameters filed like:

如果您想要使用不同的log4j配置进行测试,那么在运行/调试配置中直接指定自定义配置文件可能更容易,VM参数是这样的:

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

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

#3


35  

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>

#4


14  

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)
  • 进入你的项目设置(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.
  • 在右边,选择Dependencies选项卡。
  • Click the green plus and select "Jars or directories".
  • 点击绿色加号并选择“jar或目录”。
  • 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).
  • Intellij现在会问你所选文件的“类别”是什么。选择“类”(即使它们不是)。
  • 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.
  • 现在必须将属性文件添加到工件。Intellij会给你下面的快捷方式。它将显示底部红色部分的错误和一个“红色灯泡”,当单击时,您可以选择将文件添加到工件。您还可以访问“artifacts”部分,并手动将文件添加到工件。

向IntelliJ的类路径添加一个属性文件。

#5


12  

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 ->编译器->资源模式->[…];* . ini

#6


2  

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)

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

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。属性需要在项目根的父目录中,以便在运行时像这样在IntelliJ中被选中。(项目根是/src文件夹所在的地方)

ECLIPSE

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

当xxxx出现时,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:

这也会影响您在拥有相同代码行(新FileInputStream(“xxxx.properties”)时必须执行代码的方式;)在你出口. jar。当您想要变得敏捷时,并不想将.properties文件打包到您的jar中,您将不得不执行下面的jar文件,以便从命令行正确地引用.properties文件:

INTELLIJ EXPORTED JAR

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文件所在的位置。

#7


2  

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)

    项目结构。快捷方式是(CTRL + ALT + SHIFT + S)

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

    在最左边的列表中,选择Project Settings >模块。

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

    在模块列表右侧,选择项目名称的模块(没有构建)并选择sources选项卡。

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

    在中间,展开你的项目的根目录,这是/home/ <用户名> /IdeaProjects/

  • 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/resources and 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).

    看右边的内容根部分,红色的路径是你没有做的目录。您将希望将属性文件放在资源目录中。所以我创建了src/main/resources,并将log4j。属性。我相信您也可以修改内容根来将其放到您想要的地方(我没有这样做)。

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

    我用一个SBT配置运行我的代码,它找到了我的log4j。属性文件。

向IntelliJ的类路径添加一个属性文件。

#8


1  

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 some of the unit test resources were copied to the output folder upon compilation. My persistence.xml in the META-INF folder got copied but nothing else.

也许这有点离题了,因为这个问题已经回答过了,但我也遇到过类似的问题。在我的情况下,只有一些单元测试资源在编译时被复制到输出文件夹。我的坚持。在META-INF文件夹中的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项目与文件系统有点不同步,重命名操作触发了某种内部的“资源重新扫描”。

#9


0  

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。