Java在运行Eclipse时无法找到文件 [英] Java can't find file when running through Eclipse

查看:204
本文介绍了Java在运行Eclipse时无法找到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行一个应该从Eclipse中读取的Java应用程序时,我得到一个 java.io.FileNotFoundException ,即使该文件位于正确的目录。我可以从命令行编译并运行应用程序就好了;该问题只发生在Eclipse中,具有多个项目和应用程序。有没有一个设置,我需要更改运行配置或构建路径才能正确找到文件?

解决方案

问题很可能是您的应用程序正在使用相对路径名。正如@BalusC所说,相对的路径名可能是有问题的。但是,海事组织,他说,他说你应该永远不要使用java.io的相对路径。



当应用程序使用(例如) FileInputStream(File)构造函数打开文件时,相对路径名将相对于当前目录解析关于的javadoc中描述的过程 File.getAbsolutePath()


[...]否则,此路径名以系统相关的方式解决。在UNIX系统上,通过将相对路径名解析为当前用户目录,使其成为绝对路径。在Microsoft Windows系统上,相对路径名是绝对的,通过将其与由路径名命名的驱动器的当前目录(如果有的话)解决;如果没有,则会针对当前用户目录进行解析。


所以立即,我们看到当前目录的概念有所不同Windows和UNIX平台的细微差别。第二个问题是,在纯Java中,您无法确定当前目录是什么,您当然无法使用纯Java对当前JVM进行更改。 (当JVM启动时,user.dir系统属性设置为当前目录,但是没有任何东西阻止应用程序更改属性,因此您不能完全依赖它,而且,更改user.dir只会改变空路径的解决方式,而不是一般的相对路径。)



那么你应该怎么做?




  • 一个选项是使用绝对路径名来引用文件。这在几乎所有的情况下是可靠的,但是如果用户必须输入路径名,则使用绝对路径名可能会有问题,如果需要避免使用硬连线(或配置)的绝对路径名,则


  • 第二个选项是使用类路径相对路径名,并找到相对于应用程序安装目录的文件。如果这是您需要做的,这可以工作,但如果您需要将文件传递给某些库方法,则会出现问题。如果您试图找到用户的应用程序首选项,它也没有帮助。 (一般来说,将用户偏好设置到安装目录中是一个错误...)


  • 第三个选项是将文件命名为某个绝对目录,你从别的地方得到例如新文件(System.getProperty(home.dir),foo / bar);


  • 最后一个选项是使用相对路径名,并假设用户知道当前目录是什么。对于用户从命令行运行的许多应用程序,这是正确的解决方案。




在特定情况下Eclipse有一个简单的解决方案。转到您用于启动应用程序的运行配置,打开参数选项卡,然后单击其他单选按钮。然后输入绝对路径名作为启动应用程序的工作目录。当启动子JVM时,它将具有指定的工作目录作为其当前目录。


When I run a Java application that should be reading from a file in Eclipse, I get a java.io.FileNotFoundException, even though the file is in the correct directory. I can compile and run the application from the command line just fine; the problem only occurs in Eclipse, with more than one project and application. Is there a setting I need to change in the run configurations or build paths to get it to find the file correctly?

解决方案

The problem is most likely that your application is using a relative pathname. As @BalusC says, relative pathnames can be problematic. But IMO, he goes way too far when he says "[y]ou should never use relative paths in java.io stuff".

When an application opens a file using (for example) the FileInputStream(File) constructor, relative pathnames are resolved relative to the "current directory" in a process described as follows in the javadoc for File.getAbsolutePath().

[...] Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

So immediately, we see that the notion of "current directory" has different nuances on Windows and UNIX platforms. The second issue is that in pure Java you cannot definitively find out what the current directory is, and you certainly cannot change it for the current JVM using pure Java. (When the JVM starts, the "user.dir" system property is set to the current directory, but there is nothing stopping an application from changing the property so you cannot entirely rely on it. Furthermore, changing "user.dir" only changes the way that the empty path is resolved, not relative paths in general.)

So what should you do about this?

  • One option is to use absolute pathnames to refer to files. This is reliable in (almost) all cases, but using absolute pathnames can be problematic if the user has to enter the pathname, or if you need to avoid hard-wired (or configured) absolute pathnames.

  • A second option is to use classpath relative pathnames and locate files relative to the application's installation directory. This works if that is what you need to do, but presents a problem if you need to pass a File to some library method. It also doesn't help if you are trying to find the user's application preferences. (In general, putting user preferences into the installation directory is a mistake ...)

  • A third option is to name a file relative to some absolute directory that you get from somewhere else; e.g. new File(System.getProperty("home.dir"), "foo/bar");.

  • The final option is to use relative pathnames, and assume that the user knowing what the current directory. For many applications that the user runs from the command line, this is the right solution.

In the particular case of Eclipse, there is a simple solution. Go to the "run configuration" that you are using to launch your application, open the "Arguments" tab, and click the "Other" radio button. Then enter an absolute pathname as the working directory for the launched application. When the child JVM is launched, it will have the specified working directory as its current directory.

这篇关于Java在运行Eclipse时无法找到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆