2017-06-20 34 views
2

我試圖訪問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"); 
    } 
} 
} 

Inside jar file 在此先感謝。

+1

你不能從一般的URL創建一個文件:'ClassLoader.getResource(...)'返回一個URL,表示一個資源在加載類的地方的位置:在這種情況下,在jar文件。請注意,在這種情況下文件系統上沒有'Geolite2-City.mmdb'文件:只有原始文件的內容被捆綁到jar文件中。如果您的數據庫只能從實際文件中讀取,那麼您需要解壓縮並將其放置在某個文件系統(根據需要存在臨時文件夾或用戶目錄)。 –

+0

我已添加屏幕截圖,以顯示它在jar文件中 – user2070739

+1

是的,很抱歉;在我原來的(現在刪除的)評論中,我誤解了這個問題。如果你的數據庫需要一個實際的文件(不是一般的流),那麼你需要從jar文件中提取它,並將其放在文件系統上(你可以很容易地通過編程來實現)。 –

回答

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(); 

如果您每次運行應用程序時需要一個新的數據庫,你可能會複製到一個臨時文件,而不是,刪除文件的應用程序退出時。

+0

謝謝,我正在尋找這樣的東西,有沒有你學到這些的文檔? – user2070739

+2

@ user2070739我想我只是從理解一個jar文件是什麼(它是在「我不記得學習這個」我的大腦的一部分......)中丟失的信息類型)。 https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html有幫助嗎? –