簡體   English   中英

從classpath的jar文件內的文件夾中讀取文件

[英]Read files from a folder inside a jar file on classpath

我有一個帶有圖形編輯器的項目(例如GMF技術),我需要讀取放置在項目類路徑上的.Jar庫中的文件。

VectorSync_1.0.0.201403121100.jar的結構類似於:

VectorSync:

  • icons / VectorSync.gif
  • 型號/VectorSync.xml
  • 程序/VectorSync.java
  • META-INF / MANIFEST.MF

我導入.Jar文件的項目的.classpath是:

<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="lib" path="/home/jperezmedina/git/arom/arom-core/target/scala-2.9.0/aromslave_2.9.0-0.1.jar"/>
    <classpathentry kind="lib" path="/home/jperezmedina/runtime-EclipseApplication/DefaultProject.arom/lib/VectorSync_1.0.0.201403121100.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

我使用此方法嘗試訪問.xml文件:

JarFile file = new JarFile("VectorSync_1.0.0.201403121100.jar");

BufferedReader input1 = 
    new BufferedReader(new InputStreamReader(file.getClass().
        getClassLoader().getResourceAsStream("model/VectorSync.xml")));

但是使用的方法始終返回null

您能幫我解決這個問題嗎?

謝謝

您嘗試過這種方法嗎?

Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);

如果查看JarFile您會發現它為您提供了執行此操作的api

JarFile file = new JarFile("VectorSync_1.0.0.201403121100.jar");
ZipEntry entry = file.getEntry("model/VectorSync.xml");
BufferedReader input1 = new BufferedReader(
                        new InputStreamReader(file.getInputStream(entry)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM