简体   繁体   English

Java在Eclipse中运行但在Netbeans中运行时无法成功打开文件

[英]Java opens file successfully when run in Eclipse but not in Netbeans

I have been working on a project in Eclipse that requires me to open a file. 我一直在Eclipse中进行一个项目,该项目需要我打开文件。 It's working as expected. 它按预期工作。 I imported my project into Netbeans just to check it out but when run, the file can't be opened. 我将项目导入Netbeans只是为了将其签出,但是在运行时无法打开该文件。 I think a screenshot would be most helpful in this case: 我认为屏幕截图在这种情况下最有用:

Netbeans中问题的屏幕截图

Any idea what the issue is? 知道是什么问题吗? I've even explicitly specified the run directory in the project properties and that still doesn't work. 我什至在项目属性中明确指定了运行目录,但仍然无法正常运行。

Additional Info: 附加信息:

  • I just remembered that I'm using the JDK 1.6 in Eclipse but Netbeans is using the JDK 1.7 -- is file path resolution handled differently between the two platforms? 我只是记得我在Eclipse中使用JDK 1.6,但是Netbeans使用的是JDK 1.7-在两个平台之间,文件路径解析的处理方式是否不同?

Also, as suggested by a few users, I added in my catch block: 另外,根据一些用户的建议,我在catch块中添加了:

System.err.println(new File(inFileName).getAbsolutePath();

Which, as it should, returns: 它应该返回:

C:\\Users\\David\\Dev\\projects\\Autocuration\\pearltrees_export_02-07-2012.rdf

After attempting to construct the URI manually, 尝试手动构建URI之后,

System.out.println(new File(System.getProperty("user.dir")).toURI().resolve(inFileName));

I got the following exception: 我有以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in path at index 32: pearltrees_export_02-07-2012.rdf

Considering there is no 32nd character, I assume the printStackTrace method must trim the string obtained from the exception message before printing. 考虑到没有第32个字符,我假设printStackTrace方法必须在打印之前修剪从异常消息获得的字符串。

Whatever the case, it appears that Netbeans parses Run arguments using the ' ' (space) character as a separator. 无论如何,Netbeans似乎都使用''(空格)字符作为分隔符来解析Run参数。 However, it also includes that space when it passes the arguments to the main method. 但是,当将参数传递给main方法时,它也包含该空间。 In other words, I was trying to open: 换句话说,我正在尝试打开:

"pearltrees_export_02-07-2012.rdf "

rather than: 而不是:

"pearltrees_export_02-07-2012.rdf"

Simply trimming argument fixes the problem: 简单地修剪参数即可解决问题:

inStream = new FileInputStream(new File(inFileName.trim()));

Anyway, I cannot find any documentation stating this is the intended behavior as it is certainly different from how the java binary behaves (and any other editors I know of). 无论如何,我找不到任何文档说明这是预期的行为,因为它与java二进制文件(以及我所知的任何其他编辑器)的行为肯定不同。 For this reason I consider this a bug, and I submitted a bug report: 因此,我认为这是一个错误,并提交了错误报告:

http://netbeans.org/bugzilla/show_bug.cgi?id=215429 http://netbeans.org/bugzilla/show_bug.cgi?id=215429

I guess we'll see what the Netbeans fellows have to say.. 我猜我们将看到Netbeans同事怎么说。

Is the same OS? 是相同的操作系统吗? Same file name? 相同的文件名? Acording to the message given, it looks like there's some problem in the file name. 根据给出的消息,文件名似乎有问题。

在catch块中,应添加以下内容:

System.err.println(new File(inFileName).getAbsolutePath());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM