Development Tip

jar 파일의 버전을 확인하는 방법은 무엇입니까?

yourdevel 2020. 10. 18. 19:42
반응형

jar 파일의 버전을 확인하는 방법은 무엇입니까?


저는 현재 J2ME 폴리쉬 애플리케이션을 개발 중입니다. jar 파일의 정확한 버전을 얻는 데 어려움이 있습니다. 클래스에서 수행 된 가져 오기에 대한 jar 파일의 버전을 찾는 방법이 있습니까? 제 말은 뭔가 있으면 import xyz; jar xy 패키지의 버전을 알 수 있습니까?


JAR 파일의 압축을 풀고 매니페스트 파일 ( META-INF\MANIFEST.MF)을 찾습니다 . JAR 파일의 매니페스트 파일에는 버전 번호가 포함될 수 있습니다 (항상 버전이 지정되지는 않음).


압축을 풀고 META-INF/MANIFEST.MF파일을 확인해야 합니다. 예 :

unzip -p file.jar | head

또는 더 구체적 :

unzip -p file.jar META-INF/MANIFEST.MF

위의 답변을 확장하기 위해 JAR의 META-INF / MANIFEST.MF 파일 안에 다음 줄이 표시 될 것입니다. Manifest-Version: 1.0← 이것은 jar 버전 번호 아닙니다 !

당신을 위해 볼 필요가 Implementation-Version존재, 당신은 거기에 확인할 수있는 것들에로 JAR의 저자에게 이렇게 전적으로 자유 텍스트 문자열 인 경우. Oracle 문서패키지 버전 사양 도 참조하십시오.


위의 답변을 완료하기 위해.

매니페스트 파일은 jar의 META-INF\MANIFEST.MFpath에 있습니다.

zip을 지원하는 모든 아카이버에서 jar의 내용을 검사 할 수 있습니다.


각 jar 버전에는 고유 한 체크섬이 있습니다. 버전 정보가없는 jar의 체크섬을 계산하고 다른 버전의 jar와 비교할 수 있습니다. 체크섬을 사용하여 항아리를 검색 할 수도 있습니다.

체크섬을 계산하려면이 질문을 참조하십시오 . 내 컴퓨터에있는 파일의 체크섬을 계산하는 가장 좋은 방법은 무엇입니까?


기본적으로 java.lang.Package클래스에 대한 정보를 제공하기 위해 클래스 로더를 사용하는 클래스를 사용해야합니다.

예:

String.class.getPackage().getImplementationVersion();
Package.getPackage(this).getImplementationVersion();
Package.getPackage("java.lang.String").getImplementationVersion();

로그 백은이 기능을 사용하여 생성 된 스택 트레이스에서 각 클래스의 JAR 이름 / 버전을 추적하는 것으로 알려져 있습니다.

또한 참조 http://docs.oracle.com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp90779


이 간단한 프로그램은 즉, jar 버전의 모든 경우를 나열합니다.

  • 매니페스트 파일에서 찾은 버전
  • Manifest 및 jar 이름에서도 버전을 찾을 수 없습니다.
  • 매니페스트 파일을 찾을 수 없습니다.

    Map<String, String> jarsWithVersionFound   = new LinkedHashMap<String, String>();
    List<String> jarsWithNoManifest     = new LinkedList<String>();
    List<String> jarsWithNoVersionFound = new LinkedList<String>();
    
    //loop through the files in lib folder
    //pick a jar one by one and getVersion()
    //print in console..save to file(?)..maybe later
    
    File[] files = new File("path_to_jar_folder").listFiles();
    
    for(File file : files)
    {
        String fileName = file.getName();
    
    
        try
        {
            String jarVersion = new Jar(file).getVersion();
    
            if(jarVersion == null)
                jarsWithNoVersionFound.add(fileName);
            else
                jarsWithVersionFound.put(fileName, jarVersion);
    
        }
        catch(Exception ex)
        {
            jarsWithNoManifest.add(fileName);
        }
    }
    
    System.out.println("******* JARs with versions found *******");
    for(Entry<String, String> jarName : jarsWithVersionFound.entrySet())
        System.out.println(jarName.getKey() + " : " + jarName.getValue());
    
    System.out.println("\n \n ******* JARs with no versions found *******");
    for(String jarName : jarsWithNoVersionFound)
        System.out.println(jarName);
    
    System.out.println("\n \n ******* JARs with no manifest found *******");
    for(String jarName : jarsWithNoManifest)
        System.out.println(jarName);
    

http://www.javaxt.com/downloads/ 에서 다운로드 할 수있는 javaxt-core jar를 사용합니다 .


늦었지만 다음 두 가지 방법을 시도해 볼 수 있습니다

이 필요한 수업 사용

import java.util.jar.Attributes;
import java.util.jar.Manifest;

These methods let me access the jar attributes. I like being backwards compatible and use the latest. So I used this

public Attributes detectClassBuildInfoAttributes(Class sourceClass) throws MalformedURLException, IOException {
    String className = sourceClass.getSimpleName() + ".class";
    String classPath = sourceClass.getResource(className).toString();
    if (!classPath.startsWith("jar")) {
      // Class not from JAR
      return null;
    }
    String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + 
        "/META-INF/MANIFEST.MF";
    Manifest manifest = new Manifest(new URL(manifestPath).openStream());
    return manifest.getEntries().get("Build-Info");
}

public String retrieveClassInfoAttribute(Class sourceClass, String attributeName) throws MalformedURLException, IOException {
    Attributes version_attr = detectClassBuildInfoAttributes(sourceClass);

    String attribute = version_attr.getValue(attributeName);

    return attribute;
}

This works well when you are using maven and need pom details for known classes. Hope this helps.


You can filter version from the MANIFEST file using

unzip -p my.jar META-INF/MANIFEST.MF | grep 'Bundle-Version'


For Linux, try following:

find . -name "YOUR_JAR_FILE.jar" -exec zipgrep "Implementation-Version:" '{}' \;|awk -F ': ' '{print $2}'


If you have winrar, open the jar with winrar, double-click to open folder META-INF. Extract MANIFEST.MF file to any location (say desktop). Open the extracted file in a text editor: You will see Implementation-Version.


It can be checked with a command java -jar jarname

참고URL : https://stackoverflow.com/questions/5834794/how-to-check-the-version-of-jar-file

반응형