2017-06-20 77 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有帮助吗? –