java.io.FileNotFoundException although the file is present
Asked Answered
C

4

0

i am trying to use ip2c to get country codes based on the ip of the user. Now the problem is it cannot find the binary file that it has to search. It throws the following ecxepion

java.io.FileNotFoundException: ip-to-country.bin (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at net.firefang.ip2c.input.RandomAccessBuffer.<init>(Unknown Source)
    at net.firefang.ip2c.IP2Country.<init>(Unknown Source)
    at net.firefang.ip2c.IP2Country.<init>(Unknown Source)
    at com.em.ss.controllers.CalendarController.postReminder(CalendarController.java:96)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.em.ss.filter.GzipFilter.doFilter(GzipFilter.java:86)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

The file is in the base folder of the project and if I make a main function and create a file descriptor, it correctly finds the file and everything is good.

public static void main(String[] args) throws IOException
    {

        File file=new File("ip-to-country.bin");
        System.out.println(file.getCanonicalPath());
    }

But when i run my application, which is a spring mvc application and then ip2c cannot find the file. Here is how ip2c tries to look for it.

m_input = new MemoryMappedRandoeAccessFile(file, "r");

where file is simply the name of the bin file i.e ip-to-country.bin . I am totally stuck and have no idea whats the problem.

Capparidaceous answered 1/3, 2012 at 13:17 Comment(0)
E
0

You are clearly trying to access the file using a relative pathname. When you do this, the Java runtime system will attempt to find the file relative to the application's current directory. And the most likely reason it works in one scenario and not the other is the current directory is different in the two scenarios.

Try using an absolute pathname instead.

Editheditha answered 1/3, 2012 at 13:29 Comment(3)
i cant change the code, its in a jar that i am using. It needs this binary file and i have to put it somewhere in my project where it can be accessed as is, and i am not sure where :(Capparidaceous
Simple. The short term fix is to work out what the current directory and put the file there. The long term fix is to get hold of the source code for this JAR, and change it to get rid of the hard-wired pathname. (And if you can't, look for a better vendor. Hard-wired pathnames is a clear sign of poor quality product.)Editheditha
ok thanx alot, i was hoping to get a solution that did not involve this but well, i guess this is the only solution, so i'll accept this answer :)Capparidaceous
E
5

In cases of "the server can't find the file, but it's there!" problems, this will solve it (always has for me):

File file = new File("somefilename");
System.out.println(file.getAbsolutePath());    

You will quickly discover that the directory where the server is looking isn't what you thought it was. Usually because the home directory isn't what you think it is.

Eudy answered 1/3, 2012 at 13:27 Comment(1)
thanx a lot mate, but its strange that its looking in the eclipse directory C:\development\eclipse\ip-to-country.bin. Given that its a web project, i thought it would look in WebContent folder or something. Now i cant change the code that the jar is using to provide an absolute path, so is there a way to change the home directory some how so that it finds the file?Capparidaceous
H
1

Spring MVC application probably run is some server - on contrary with main-function in class.

You root path would be different. You should put this file right next to WEB-INF folder. Should be located in a war folder. (If you have started from some good template)

Hartsell answered 1/3, 2012 at 13:27 Comment(0)
E
0

You are clearly trying to access the file using a relative pathname. When you do this, the Java runtime system will attempt to find the file relative to the application's current directory. And the most likely reason it works in one scenario and not the other is the current directory is different in the two scenarios.

Try using an absolute pathname instead.

Editheditha answered 1/3, 2012 at 13:29 Comment(3)
i cant change the code, its in a jar that i am using. It needs this binary file and i have to put it somewhere in my project where it can be accessed as is, and i am not sure where :(Capparidaceous
Simple. The short term fix is to work out what the current directory and put the file there. The long term fix is to get hold of the source code for this JAR, and change it to get rid of the hard-wired pathname. (And if you can't, look for a better vendor. Hard-wired pathnames is a clear sign of poor quality product.)Editheditha
ok thanx alot, i was hoping to get a solution that did not involve this but well, i guess this is the only solution, so i'll accept this answer :)Capparidaceous
L
0

The File class is just wrapper for manipulating abstract filenames. This code

File file=new File("ip-to-country.bin");
System.out.println(file.getCanonicalPath());

not requres the file exist and executes normally. Use File.exists() method to check existence of file. As you trying to access file with relative path, make sure it located in current directory of running application. Probably it is not same folder as project root.

Lael answered 1/3, 2012 at 13:29 Comment(1)
i did try that, i tried the canonical path to see if its in the folder that i think it is.Capparidaceous

© 2022 - 2024 — McMap. All rights reserved.