应用程序用作库时访问资源文件的Java问题

时间:2015-07-17 10:17:23

标签: java resources

我有两个Java应用程序。一个应用程序将包含资源文件,并将用作其他Java应用程序的库。

第一个应用程序com.test.resourceusing.MainClass.java,其中包含res / base.xml资源文件。

package com.test.resourceusing;

import java.io.BufferedInputStream;
import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.Scanner;

public class MainClass {
public MainClass() {
    super();
}

public static void main(String[] args) {
    MainClass main = new MainClass();
    try {
        main.start();
    } catch (MalformedURLException e) {
    }
}

public void start() throws MalformedURLException {
    URL url = getClass().getResource("res/base.xml");
    System.out.println(url.getPath());
    System.out.println(url.getFile());
    File f = new File(url.getFile());
    if (f.exists()) {
        System.out.println("File exist!");

        BufferedInputStream result = (BufferedInputStream) 
              getClass().getResourceAsStream("res/base.xml");
        Scanner scn = new Scanner(result);
        while(scn.hasNext()){
            System.out.println(scn.next());
        }
    } else {
        System.out.println("Not working! :(");    
    }
}

}

结果是:

/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
File exist!
<?xml
version='1.0'
encoding='utf-8'?>
<schema>
</schema>

然后我创建包含所有资源文件的.jar文件,并尝试将其用作其他应用程序中的库。

第二个应用: resourcetest.MainClassTest.java

package resourcetest;

import com.test.resourceusing.MainClass;

import java.net.MalformedURLException;

public class MainClassTest {
  public MainClassTest() {
    super();
  }

  public static void main(String[] args) {
    MainClass main = new MainClass();
    try {
        main.start();
    } catch (MalformedURLException e) {
    }
  }
}

结果是:

file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
Not working! :(

我不明白为什么它不能正常工作,我的代码中是否存在问题?或者这个解决方案在Java中是不可能的?

2 个答案:

答案 0 :(得分:1)

您是否看到这些文件的位置不同?

/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml

您无法使用File API访问位于JAR文件中的资源。

您的代码已经开始了。一个简单的编辑应该有效:

public void start() throws IOException {
    URL url = getClass().getResource("res/base.xml");
    if (url != null) {
        System.out.println(url.getPath());
        System.out.println(url.getFile());
        System.out.println("File exist!");

        try(InputStream result = url.openStream()) {
            try(Scanner scn = new Scanner(result)) {
                while(scn.hasNext()) {
                    System.out.println(scn.next());
                }
            }
        }
    } else {
        System.out.println("Not working! :(");    
    }
}

答案 1 :(得分:0)

经过深入搜索后,我发现了一个原因 - https://stackoverflow.com/a/10605316/1117515。 在这种情况下,我需要使用getResourceAsStream()。