gpt4 book ai didi

java - 从 jar 文件访问文件和图像

转载 作者:行者123 更新时间:2023-11-30 07:53:35 25 4
gpt4 key购买 nike

我在尝试访问 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");
}
}
}

Inside jar file提前致谢。

最佳答案

当您的应用程序被打包为 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();

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

关于java - 从 jar 文件访问文件和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44652627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com