2017-07-16 220 views
0

我有以下代码file.exists()在java/mac电脑上错误地返回false?

File f = new File(fileSystemPath); 
    if(!f.exists()) 
     throw new IllegalArgumentException("Static File="+getCanonicalPath(f)+" does not exist. fileSysPath="+this.fileSystemPath); 

这将导致以下异常

WEBPIECESxPACKAGE.TestLesson1Json > testSynchronousController FAILED 
java.lang.IllegalArgumentException: Static File=/Library/Workflow/webpieces/webserver/webpiecesServerBuilder/templateProject/WEBPIECESxAPPNAME/src/dist/public/favicon.ico does not exist. fileSysPath=public/favicon.ico 
    at org.webpieces.router.impl.StaticRoute.<init>(StaticRoute.java:44) 
    at org.webpieces.router.impl.model.R1RouterBuilder.addStaticRoute(R1RouterBuilder.java:89) 
    at org.webpieces.router.impl.model.R1RouterBuilder.addStaticFile(R1RouterBuilder.java:82) 
    at org.webpieces.router.api.routing.AbstractRoutes.addStaticFile(AbstractRoutes.java:38) 
    at WEBPIECESxPACKAGE.base.AppRoutes.configure(AppRoutes.java:44) 
    at org.webpieces.router.api.routing.AbstractRoutes.configure(AbstractRoutes.java:12) 
    at org.webpieces.router.impl.RouteLoader.loadAllRoutes(RouteLoader.java:171) 
    at org.webpieces.router.impl.RouteLoader.loadImpl(RouteLoader.java:114) 
    at org.webpieces.router.impl.RouteLoader.load(RouteLoader.java:73) 
    at org.webpieces.router.impl.ProdRouterService.start(ProdRouterService.java:49) 
    at org.webpieces.webserver.impl.WebServerImpl.startAsync(WebServerImpl.java:68) 
    at org.webpieces.webserver.impl.WebServerImpl.startSync(WebServerImpl.java:54) 
    at WEBPIECESxPACKAGE.Server.start(Server.java:232) 
    at WEBPIECESxPACKAGE.TestLesson1Json.setUp(TestLesson1Json.java:73) 

然而,当我把一个断点在我的节目就在if语句,我做的LS -la命令这个结果前后存在检查(即它的存在只是罚款)

[08:22][ruby-1.9.3][dhiller:/Library/Workflow/webpieces(master)]$ ls -la /Library/Workflow/webpieces/webserver/webpiecesServerBuilder/templateProject/WEBPIECESxAPPNAME/src/dist/public/favicon.ico 
-rwxr-xr-x 1 dhiller wheel 1150 Jul 4 09:31 /Library/Workflow/webpieces/webserver/webpiecesServerBuilder/templateProject/WEBPIECESxAPPNAME/src/dist/public/favicon.ico 
[08:22][ruby-1.9.3][dhiller:/Library/Workflow/webpieces(master)]$ 

而不是新的文件,我想这一点这并没有帮助eithe r

private File createFile(String fileSystemPath2) { 
    String[] split = fileSystemPath2.split("/"); 
    File tmp = new File(split[0]); 
    for(int i = 1; i < split.length; i++) { 
     tmp = new File(tmp, split[i]); 
    } 
    return tmp; 
} 

我在jdk 1.8.0_111和mac 10.11.6上。有一两件事,已被工作user.dir来附加到文件的开头,以便像

String userDir = System.getProperty("user.dir") 
File f = new File(userDir, systemPath) 

我为什么需要,虽然这样做呢?这看起来很奇怪,因为我可以发誓这之前工作?

+2

你怎么有'getCanonicalPath(F)的''而不是f.getCanonicalPath()' ? –

+0

我有一个尝试..catch将其转换为getCanonicalPath()方法中的runtimeexception,而不是传播无用的检查异常,用户无法对固定到最高级别进行任何操作。 –

回答

0

好吧,我想通了,这让我有另一个问题。

我修改了user.dir系统属性,但这显然打破了实际工作中的相关文件(至少在jdk1.8.0_111中)。即。只要文件仅与原始user.dir相关,它就会工作。不幸的是,intellij选择的user.dir,gradle picks和eclipse选择都是不一致的:(所以我想我会对齐它们并修改user.dir,所有的都会工作.... ooops,错误的,那是行不通的。

我说对我看到的是下调版的bug,因此接下来的这个问题。

user.dir property broken on OSX jdk 1.8.0_111? how about other OS, versions?

0

嗯,你需要用户家目录附加到路径,否则它不会是一个绝对路径且仅当它位于一个文件夹中,这条路就可以您的应用程序将能够找到它从(在您家中的用户家中)解决。

+0

th不是真的。事实上,我发现另一个测试工作得很好,记录了“相对”文件的绝对路径和规范路径,并记录了整个路径。 –

+0

在帖子后添加了测试https://stackoverflow.com/questions/45130661/user-dir-property-broken-on-osx-jdk-1-8-0-111-how-about-other-os-versions演示我认为必须是一个错误。 –

相关问题