IT story

IntelliJ의 클래스 경로에 속성 파일 추가

hot-time 2020. 7. 20. 07:25
반응형

IntelliJ의 클래스 경로에 속성 파일 추가


Run-> Run 메뉴를 사용하여 IntelliJ IDE에서 간단한 Java 프로그램을 실행하고 있습니다. 잘 작동합니다. 이제 log4j 로깅을 추가하고 싶습니다.

프로젝트 루트 아래에 resources 폴더를 추가했습니다. 해당 폴더에 log4j.properties 파일을 추가했습니다. 무언가를 기록하도록 코드를 변경했습니다.

속성 파일을 볼 수 있도록 IntelliJ에 클래스 폴더에 resources 폴더를 포함 시키도록 올바른 방법은 무엇입니까?

IntelliJ 8을 사용하면 취한 원숭이처럼 추측하여 결국 작동시킬 수 있습니다. 나는 지금 9가 있고 나는 완전히 실패합니다. 한 시간 동안 노력했습니다. 어딘가에 "클래스 패스에 추가"옵션은 어떻습니까? / fume / vent / rant


이 시도:

  • 프로젝트 구조로 이동하십시오.
  • 모듈을 선택하십시오.
  • 오른쪽의 트리에서 폴더를 찾아서 선택하십시오.
  • 해당 트리 위의 소스 단추 (파란색 폴더 포함)를 클릭하여 해당 폴더를 소스 폴더로 만듭니다.

실제로 두 가지 방법이 있습니다. 첫 번째 방법은 ColinD에 의해 설명 된 것입니다. IDEA에서 "resources"폴더를 Sources 폴더로 구성하기 만하면됩니다. 경우 자원 패턴이 자원의 확장을 포함 당신이 때, 다음은 출력 디렉터리에 복사됩니다 확인 프로젝트를 출력 디렉토리가 자동으로 응용 프로그램의 클래스 경로입니다.

또 다른 일반적인 방법은 "resources"폴더를 클래스 경로에 직접 추가하는 것입니다. 프로젝트 구조로 이동 | 모듈 | 모듈 | 종속성 에서 추가 , 단일 항목 모듈 라이브러리를 클릭 하고 "resources"폴더의 경로를 지정하십시오.

또 다른 해결책은 log4j.properties 파일을 프로젝트의 소스 루트 (기본 패키지 디렉토리에 있음) 바로 아래 에 두는 것 입니다. 모듈 경로 설정 에서 다른 소스 루트를 추가 할 필요가 없다는 점을 제외하고는 첫 번째 방법과 동일합니다 . 파일은 Make의 출력 디렉토리에 복사됩니다.

다른 log4j 구성으로 테스트하려는 경우 Run / Debug configuration , VM 매개 변수 파일에 직접 사용자 정의 구성 파일을 지정하는 것이 더 쉬울 수 있습니다 .

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


나는 같은 문제가 있고 그것은 나를 귀찮게합니다!

나는 항상 대답 2로 할 것이라고 생각했습니다. Intellij 9 (현재 10 사용)에서 작동했습니다.

그러나이 줄을 maven pom 파일에 추가하면 도움이된다는 것을 알았습니다.

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

Intellij 13x 에서이 작업을 수행하는 방법을 알아내는 데 많은 시간을 보냈습니다. 필자는 속성 파일을 필요한 아티팩트에 추가하지 않았으며 Intellij의 별도 단계입니다. 아래 설정은 여러 모듈이 공유하는 특성 파일이있는 경우에도 작동합니다.

  • 프로젝트 설정으로 이동하십시오 (CTRL + ALT + SHIFT + S).
  • 목록에서 하나 이상의 특성 파일을 추가 할 모듈을 선택하십시오.
  • 오른쪽에서 종속성 탭을 선택하십시오.
  • 녹색 더하기를 클릭하고 "Jars 또는 디렉토리"를 선택하십시오.
  • 이제 속성 파일이 들어있는 폴더를 선택하십시오. (개별 파일 포함을 시도하지 않았습니다)
  • Intellij는 이제 선택한 파일의 "카테고리"가 무엇인지 묻습니다. "클래스"를 선택하십시오 (그렇지 않더라도).
  • 이제 특성 파일을 이슈에 추가해야합니다. Intellij는 아래에 표시된 바로 가기를 제공합니다. 아래의 빨간색 부분에 오류가 표시되고 클릭하면 아티팩트에 파일을 추가하는 옵션이 표시되는 '빨간 전구'가 표시됩니다. '아티팩트'섹션으로 이동하여 파일을 아티팩트에 수동으로 추가 할 수도 있습니다.

여기에 이미지 설명을 입력하십시오


.ini 확장자를 가진 파일을 클래스 경로에 추가하는 것과 비슷한 문제에 직면했습니다. 이 답변을 찾았습니다.이 설정은 환경 설정-> 컴파일러-> 리소스 패턴-> [...]; *. ini에 추가됩니다.


Scala 및 SBT와 동일한 문제가 발생하는 경우 :

  • 프로젝트 구조로 이동하십시오. 바로 가기는 (CTRL + ALT + SHIFT + S)입니다.

  • 맨 왼쪽 목록에서 프로젝트 설정> 모듈을 선택하십시오.

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

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

여기에 이미지 설명을 입력하십시오


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.

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.)

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)

INTELLIJ

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

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)

ECLIPSE

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

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

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:

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


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.

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".


단위 테스트를 위해 log4j.xml 파일과 비슷한 문제가 있었으며 위의 모든 작업을 수행했습니다. 그러나 실패한 테스트 만 다시 실행했기 때문이라고 생각했습니다 .... 전체 테스트 클래스를 다시 실행하면 올바른 파일이 선택됩니다. 이것은 Intelli-j 9.0.4에 있습니다.


나는 바보 같은 사람이라는 것을 알고 있으므로 피곤한 하루를 보낸 후이 문제를 디버깅하는 데 많은 시간을 보냈으며 위에 게시 된 모든 응답을 시도했지만 결국 그것은 많은 많은 실수 중 하나였습니다.

나는 org.apache.logging.log4j.Logger(: fml :)을 사용하고 있었지만 사용해야했습니다 org.apache.log4j.Logger. 이 올바른 로거를 사용하면 생명을 구할 수있었습니다.

참고 URL : https://stackoverflow.com/questions/3765998/add-a-properties-file-to-intellijs-classpath

반응형