programing tip

JAR 내에 JAR을 포함하는 클래스 경로

itbloger 2020. 7. 2. 08:01
반응형

JAR 내에 JAR을 포함하는 클래스 경로


classpath다른 JAR 파일에 포함 된 JAR 파일을 포함 하는 Java를 지정할 수 있습니까?


응용 프로그램과 필요한 라이브러리가 포함 된 단일 jar을 만들려고하면 두 가지 방법이 있습니다 (내가 아는). 첫 번째는 One-Jar 이며, 특수 클래스 로더를 사용하여 항아리를 중첩 할 수 있습니다. 두 번째는 UberJar (또는 Shade )이며 포함 된 라이브러리를 분해 하고 모든 클래스를 최상위 jar에 넣습니다.

또한 UberJar와 Shade는 각각 Maven1과 Maven2 용 플러그인입니다. 아래에서 언급했듯이 어셈블리 플러그인을 사용할 수도 있습니다 (실제로는 훨씬 강력하지만 올바르게 구성하기가 훨씬 어렵습니다).


이러한 "JAR 컨텐츠 분해"솔루션을 사용하지 않으려 고합니다. 그것들은 확실히 물건을보기 어렵게 만듭니다 (모든 것이 같은 레벨에서 폭발하기 때문에). 또한 이름 충돌이있을 수 있습니다 (사람들이 적절한 패키지를 사용하는 경우에는 발생하지 않아야하지만 항상이를 제어 할 수는 없습니다).

원하는 기능은 Sun이 무한한 지혜로 우선 순위가 낮은 것으로 지정한 상위 25 개의 Sun RFE : RFE 4648386 중 하나입니다 . 우리는 태양이 깨어나기를 바랄뿐입니다 ...

한편, 내가 찾은 최고의 솔루션 (Sun이 JDK로 복사하기를 원함 )은 사용자 정의 클래스 로더 JarClassLoader 를 사용하는 입니다.


일부 연구 결과, maven 또는 타사 확장 프로그램을 필요로하지 않는 방법을 찾았습니다.

매니페스트 파일에서 "클래스 경로"를 사용할 수 있습니다.

예를 들면 다음과 같습니다.

매니페스트 파일 MANIFEST.MF 만들기

Manifest-Version: 1.0
Created-By: Bundle
Class-Path: ./custom_lib.jar
Main-Class: YourMainClass

모든 클래스를 컴파일하고 실행 jar cfm Testing.jar MANIFEST.MF *.class custom_lib.jar

c아카이브 생성 f파일을 지정하고 싶다는 것을 나타냅니다. v자세한 입력 m은 사용자 정의 매니페스트 파일을 전달 함을 의미합니다.

jar 패키지에 lib를 포함했는지 확인하십시오. 일반적인 방법으로 jar를 실행할 수 있어야합니다.

기반 : http://www.ibm.com/developerworks/library/j-5things6/

클래스 경로에 필요한 다른 모든 정보는 여기에서 찾을 수 있습니다


zipgroupfileset 태그를 사용하십시오 ( 파일 세트 태그 와 동일한 속성을 사용함 ). 디렉토리의 모든 파일을 압축 해제하고 새 아카이브 파일에 추가합니다. 자세한 정보 : http://ant.apache.org/manual/Tasks/zip.html

이것은 jar-in-a-jar 문제를 해결하는 매우 유용한 방법입니다. 무엇을 해야할지 알아 내려고 하면서이 정확한 StackOverflow 질문을 봤기 때문에 알고 있습니다. 항아리 또는 항아리 폴더를 Ant가있는 하나의 빌드 된 항아리에 패키지하려면이 클래스 경로 또는 타사 플러그인을 잊어 버려라.

<jar destfile="your.jar" basedir="java/dir">
  ...
  <zipgroupfileset dir="dir/of/jars" />
</jar>

개미로 빌드하는 경우 (나는 개미의 개미를 사용하고 있습니다) 개미에게 추가하여 추가 jar 파일을 추가 할 수 있습니다 ... 여러 사람이 프로젝트를 관리하지만 작동하는 경우 반드시 가장 좋은 방법은 아닙니다. 한 사람의 프로젝트를 위해 쉽고 쉽습니다.

예를 들어 .jar 파일을 작성하는 대상은 다음과 같습니다.

<jar destfile="${plugin.jar}" basedir="${plugin.build.dir}">
    <manifest>
        <attribute name="Author" value="ntg"/>
        ................................
        <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/>
    </manifest>
</jar>

방금 한 줄을 추가하여 만들었습니다.

<jar ....">
    <zipgroupfileset dir="${external-lib-dir}" includes="*.jar"/>
    <manifest>
        ................................
    </manifest>
</jar>

어디

<property name="external-lib-dir" 
          value="C:\...\eclipseWorkspace\Filter\external\...\lib" />

외부 항아리가있는 디렉토리였습니다. 그리고 그게 다야...


Not without writing your own class loader. You can add jars to the jar's classpath, but they must be co-located, not contained in the main jar.


You need to build a custom class-loader to do this or a third-party library that supports this. Your best bet is to extract the jar from the runtime and add them to the classpath (or have them already added to the classpath).


I use maven for my java builds which has a plugin called the maven assembly plugin.

It does what your asking, but like some of the other suggestions describe - essentially exploding all the dependent jars and recombining them into a single jar


If you have eclpise IDE, you just need to export your JAR and choose "Package Required libraries into generated JAR". eclipse will automatically add the required dependant JARs into the generated JAR as well as generated some eclipse custom class loader that load these JARs automatically.


I was about to advise to extract all the files at the same level, then to make a jar out of the result, since the package system should keep them neatly separated. That would be the manual way, I suppose the tools indicated by Steve will do that nicely.


Winstone is pretty good http://blog.jayway.com/2008/11/28/executable-war-with-winstone-maven-plugin/. But not for complex sites. And that's a shame because all it takes is to include the plugin.


Well, there is a very easy way if you're using Eclipse.

Export your project as a "Runnable" Jar file (right-click project folder from within Eclipse, select "Export..."). When you configure the export settings, be sure to select "Extract required libraries into generated Jar." Keep in mind, select "Extract..." and not "Package required libraries...".

Additionally: You must select a run-configuration in your export settings. So, you could always create an empty main( ) in some class and use it for your run configuration.

Anyway, it isn't guaranteed to work 100% of the time - as you will notice a pop-up message telling you to make sure you check the licenses of the Jar files you're including and something about not copying signature files. However, I have been doing this for years and have never encountered a problem.

참고URL : https://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar

반응형