java.io.FileNotFoundException:(系统找不到指定的路径)jar结果
嗨,我在Java中遇到了这个jar问题。无法识别我设置的路径。我检查了jar内部,发现路径是正确的,我在Java文件中删除并添加了“/”和路径的开头,但仍然不起作用。还有什么我可以试试的吗 代码java.io.FileNotFoundException:(系统找不到指定的路径)jar结果,java,javafx,jar,java-8,javafx-2,Java,Javafx,Jar,Java 8,Javafx 2,嗨,我在Java中遇到了这个jar问题。无法识别我设置的路径。我检查了jar内部,发现路径是正确的,我在Java文件中删除并添加了“/”和路径的开头,但仍然不起作用。还有什么我可以试试的吗 代码 package sample; public class LocateMyCity { private String myCityLocation; private String country; public String getCountry() {
package sample;
public class LocateMyCity {
private String myCityLocation;
private String country;
public String getCountry() {
return country;
}
public String getmyCityLocation(){
return myCityLocation;
}
public LocateMyCity() {
// A File object pointing to your GeoIP2 or GeoLite2 database
File database = new File("\\GeoLite2-City_20170502\\GeoLite2-City.mmdb");
try {
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");
}
}
}
来自命令提示符的错误消息
java.io.FileNotFoundException: \GeoLite2-City_20170502\GeoLite2-City.mmdb (The system cannot find the path specified)
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:44)
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)
java.io.FileNotFoundException:\GeoLite2-City\u 20170502\GeoLite2-City.mmdb(系统找不到指定的路径)
位于java.io.RandomAccessFile.open0(本机方法)
位于java.io.RandomAccessFile.open(未知源代码)
位于java.io.RandomAccessFile。(未知源)
位于com.maxmind.db.BufferHolder.(BufferHolder.java:19)
位于com.maxmind.db.Reader。(Reader.java:116)
位于com.maxmind.geoip2.DatabaseReader。(DatabaseReader.java:35)
位于com.maxmind.geoip2.DatabaseReader。(DatabaseReader.java:23)
位于com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:129)
位于sample.LocateMyCity.(LocateMyCity.java:44)
位于sample.WeatherToday.getPersonLocationId(WeatherToday.java:102)
位于sample.WeatherToday.(WeatherToday.java:126)
在sample.Main.start处(Main.java:37)
在com.sun.javafx.application.launchempl.lambda$launchApplication1$162(launchempl.java:863)
位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
位于com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
位于java.security.AccessController.doPrivileged(本机方法)
位于com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法)
位于com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
位于java.lang.Thread.run(未知源)
更换
File database = new File("\\GeoLite2-City_20170502\\GeoLite2-City.mmdb");
与
您指定的路径是相对的,并且起点可能不是您要查找的文件所在的位置。试试这样的
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("GeoLite2-City_20170502/GeoLite2-City.mmdb").getFile());
并将目录放在JAR的起点(如果在src/resources/下使用Maven)您使用的是绝对路径而不是相对路径。使用
LocateMyCity.class.getResourceAsStream(“/GeoLite2-City\u 20170502/GeoLite2-City.mmdb”)
。另外,我建议使用斜杠而不是反斜杠,因为操作系统是独立的。您必须使用ClassLoader.getResourceAsStream(“GeoLite2-City20170502/GeoLite2-City.mmdb“
而不是创建一个文件”
?不,您应该像其他人一样使用斜杠(/
)。不是反斜杠(\\)…我把代码放进去了,它是javanotfoundexception。这次我在jar“\MirrorMe.jar!\GeoLite2-City\u 20170502\GeoLite2-City.mmdb”后面的文件路径中看到了一个“!”(文件名、目录名或卷标语法不正确)
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("GeoLite2-City_20170502/GeoLite2-City.mmdb").getFile());