我试图访问jar文件中的文件和图像时遇到了问题。该程序在生成jar文件之前按预期工作。我创建了一个使用ClassLoader的Resources文件夹,但在试图运行它的jar文件时仍然在命令行上出现错误,但并非所有信息都显示。从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");
}
}
}
你不能从一般的URL创建一个文件:'ClassLoader.getResource(...)'返回一个URL,表示一个资源在加载类的地方的位置:在这种情况下,在jar文件。请注意,在这种情况下文件系统上没有'Geolite2-City.mmdb'文件:只有原始文件的内容被捆绑到jar文件中。如果您的数据库只能从实际文件中读取,那么您需要解压缩并将其放置在某个文件系统(根据需要存在临时文件夹或用户目录)。 –
我已添加屏幕截图,以显示它在jar文件中 – user2070739
是的,很抱歉;在我原来的(现在删除的)评论中,我误解了这个问题。如果你的数据库需要一个实际的文件(不是一般的流),那么你需要从jar文件中提取它,并将其放在文件系统上(你可以很容易地通过编程来实现)。 –