简体   繁体   中英

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. 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. 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?

Also, as suggested by a few users, I added in my catch block:

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,

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.

Whatever the case, it appears that Netbeans parses Run arguments using the ' ' (space) character as a separator. However, it also includes that space when it passes the arguments to the main method. 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). For this reason I consider this a bug, and I submitted a bug report:

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

I guess we'll see what the Netbeans fellows have to say..

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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