Java 从jar文件访问文件和图像

Java 从jar文件访问文件和图像,java,javafx,jar,java-8,javafx-2,Java,Javafx,Jar,Java 8,Javafx 2,我在尝试访问jar文件中的文件和图像时遇到了一些问题。该程序在生成jar文件之前按预期工作。我已经用ClassLoader创建了一个Resources文件夹,但是在试图运行jar文件时,命令行仍然出现了一个错误,它可以正常工作,但是没有显示所有的信息 类型必须是文件,以便databaseReader可以读取它 错误消息 java.io.FileNotFoundException: file:\C:\Users\Nicholas\IdeaProjects\MirrorMe\out\artifact

我在尝试访问jar文件中的文件和图像时遇到了一些问题。该程序在生成jar文件之前按预期工作。我已经用ClassLoader创建了一个Resources文件夹,但是在试图运行jar文件时,命令行仍然出现了一个错误,它可以正常工作,但是没有显示所有的信息

类型必须是文件,以便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");
    }
}
}

提前感谢。

当您的应用程序捆绑为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(); 

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

您无法使用常规URL创建文件:
ClassLoader.getResource(…)
返回一个URL,表示资源在类加载位置的位置:在本例中是在jar文件中。请注意,在此场景中,文件系统上没有
Geolite2 City.mmdb
文件:只有绑定到jar归档中的原始文件的内容。如果您的数据库只能读取实际文件,那么您需要提取该文件并将其放在文件系统的某个位置(临时文件夹或用户目录,视需要而定);在我最初(现已删除)的评论中,我误解了这个问题。如果您的数据库需要一个实际的文件(不是一般的流),那么您需要从jar文件中提取它并将其放在文件系统中(您可以很容易地通过编程实现)。谢谢,我在四处寻找类似这样的东西,你是在哪里学会的?@user2070739我想我是通过理解jar文件(它的信息在我大脑的“我不记得学过这个”部分中丢失了…)才明白这一点的。有帮助吗?