Reputation: 3660
Works
public static void main(String[] args) throws Exception {
String PATH = "C:\\WINDOWS\\system32\\notepad.exe";
new ProcessBuilder(PATH).start();
}
Does Not Work
public static void main(String[] args) throws Exception {
String PATH = "C:\\WINDOWS\\system32\\notepad.exe";
new ProcessBuilder(PATH).start();
}
Exception in thread "main" java.io.IOException: Cannot run program "C:\WINDOWS\system32\notepad.exe": CreateProcess error=2, The system cannot find the file specified
The two snippets appear to be identical. In the first example, I manually typed the quotes around the path. In the latter, broken example, the quotes were copied and pasted from a web site. When I copied and pasted both quotes into a Unicode character lookup tool I got the same result both times.
Why does replacing a Unicode character with the exact same Unicode character suddenly fix my code? Am I missing something here?
If anyone thinks that the snippets are identical, open your favorite IDE and copy and paste the snippets above without changing a single character. I am using NetBeans IDE 7.4 on a Windows 8 machine. I also tried Eclipse and had the same results.
Upvotes: 1
Views: 309
Reputation: 234847
I'm surprised this compiles. The second piece of code has a LEFT-TO-RIGHT EMBEDDING character (U+202A) in front of the first quote mark (outside the quote; hence illegal Java syntax). (This seems to be confirmed by the comment by Bhesh Gurung.)
If you somehow got the code to compile, then I suspect that the code you ran is a bit different from what you posted here: that the U_202A ended up inside the quote marks; hence the file-not-found error.
Upvotes: 3
Reputation: 21089
The snippet you copied and pasted from a website had some non-printing characters mixed into the filename, so it looks identical to the typed one but contains extra characters. This usually results in an incorrect path, as you discovered. Some programs will strip those non-printing characters if they do not change the representation of the visible part (and others will remove leading/trailing whitespace, too) when copying from them or pasting into them, but not all do that.
You can verify this by pasting both snippets into a Unicode-capable text editor and comparing the length/byte count (saving them to files and checking the file sizes if your text editor doesn't report the non-printing characters). I obtained a length of 159 for the second snippet and only 156 for the first.
Upvotes: 2