从jar文件访问文件和图像

时间:2017-06-20 12:07:27

标签: java javafx jar java-8 javafx-2

尝试访问jar文件中的文件和图像时,我遇到了问题。程序在被制作成jar文件之前按预期工作。我创建了一个使用ClassLoader的Resources文件夹,但在尝试运行它工作的jar文件时仍然在命令行上出错,但并不是所有的信息显示。

类型必须是File,以便databaseReader可以读取它。

错误消息

java.io.FileNotFoundException: file:\C:\Users\Nicholas\IdeaProjects\MirrorMe\out\artifacts\MirrorMe_jar\MirrorMe.jar!\GeoLite2-City.mmdb (The filename, directory name, or volume label syntax is incorrect)
    at java.io.RandomAccessFile.open0(Native Method)
    at java.io.RandomAccessFile.open(Unknown Source)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at com.maxmind.db.BufferHolder.<init>(BufferHolder.java:19)
    at com.maxmind.db.Reader.<init>(Reader.java:116)
    at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:35)
    at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:23)
    at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:129)
    at sample.LocateMyCity.<init>(LocateMyCity.java:60)
    at sample.WeatherToday.getPersonLocationId(WeatherToday.java:102)
    at sample.WeatherToday.<init>(WeatherToday.java:126)
    at sample.Main.start(Main.java:37)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)

完整代码

public class LocateMyCity {

private String myCityLocation;

private String country;

public String getCountry() {
    return country;
}

public String getmyCityLocation(){
    return myCityLocation;
}

public LocateMyCity() {
    try {


        ClassLoader classLoader = getClass().getClassLoader();
        File database = new File(classLoader.getResource("GeoLite2-City.mmdb").getFile());

        URL whatismyip = new URL("http://checkip.amazonaws.com");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                whatismyip.openStream()));

        String ip = in.readLine(); //you get the IP as a String
        System.out.println(ip);

        // This creates the DatabaseReader object, which should be reused across
        // lookups.
        DatabaseReader reader = new DatabaseReader.Builder(database).build();

        InetAddress ipAddress = InetAddress.getByName(ip);

        // Replace "city" with the appropriate method for your database, e.g.,
        // "country".
        CityResponse response = reader.city(ipAddress);

        City city = response.getCity();
        System.out.println(city.getName()); // 'Minneapolis'
        this.myCityLocation = city.getName();

        Country country = response.getCountry();
        System.out.println(country.getIsoCode());            // 'GB'
        this.country = country.getIsoCode();

        System.out.println(country.getName());               // 'United Kindom'

    }catch (Exception e){
        e.printStackTrace();
        System.out.println("Tracing IP E");
    }
}
}

Inside jar file 提前谢谢。

1 个答案:

答案 0 :(得分:3)

当您的应用程序捆绑为jar文件时,资源不再是文件,而是归档中的元素(jar文件)。对于桌面应用程序,应用程序通常无需从存档中提取这些元素即可运行。

如果您的数据库需要一个实际的文件,而不仅仅是一个可以读取的流(如果您需要写入它,则尤其如此),那么您就不能在存档中使用资源而必须使用文件系统上的文件。

您可以轻松地从存档中提取资源并将其内容写入本地文件系统。有关如何执行此操作的具体细节取决于您需要的功能。例如,如果您作为应用程序功能的一部分写入数据库,并希望下次运行应用程序时这些更改仍然存在,那么您只需要在第一次运行时从存档中提取资源(或者如果用户在稍后阶段删除了文件)。通常,您可以通过将文件放在用户的主目录中来完成此操作。您可以这样做,例如:

Path appDirectory = Paths.get(System.getProperty("user.home"), ".application-name");
Path databaseFile = appDirectory.resolve("GeoList2-City.mmdb");

if (! Files.exists(databaseFile)) {
    try {
        // create the app directory if it doesn't already exist:
        Files.createDirectories(appDirectory);

        InputSteam defaultDatabase = getClass().getClassLoader().getResourceAsStream("GeoLite2-City.mmdb");
        Files.copy(defaultDatabase, databaseFile);
    } catch (IOException exc) {
        // handle exception here, e.g. if application can run without db,
        // set flag indicating it must run in non-db mode
        // otherwise this is probably a fatal exception, show message and exit...
        exc.printStackTrace();
    }
}

// ...

DatabaseReader reader = new DatabaseReader.Builder(databaseFile.toFile()).build(); 

如果您每次运行应用程序时都想要一个新数据库,则可能会复制到临时文件,而不是在应用程序退出时删除该文件。