2014-03-12 43 views
0

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

的VectorSync_1.0.0.201403121100.jar具有如下的結構:

VectorSync:

  • 圖標/ VectorSync.gif
  • 模型/ VectorSync.xml
  • 程序/ VectorSync.java
  • META-INF/MANIFEST.MF

其中i進口.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> 

我用這個方法來嘗試ACCES一個.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

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

謝謝

+0

檢查這更多的相關信息,如果你的作品給予好評;) 的http://計算器.com/questions/22203162/images-not-loading-when-running-jar-file/22203605#22203605 – robermann

回答

0

如果你看看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))); 
0

你試過這種方法嗎?

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

感謝您的回答,但它不適用於我的項目 – user3410517