我試圖訪問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文件中提取它,並將其放在文件系統上(你可以很容易地通過編程來實現)。 –