Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.URISyntaxException

I have get this exception. but this exception is not reproduced again. I want to get the cause of this

Exception Caught while Checking tag in XMLjava.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Documents and Settings\All Users\.SF\config\sd.xml stacktrace net.sf.saxon.trans.XPathException. 

Why this exception occured. How to deal with so it will not reproduce.

like image 596
aayushi soni Avatar asked May 18 '10 09:05

aayushi soni


2 Answers

A valid URI does not contain backslashes, and if it contains a : then the characters before the first : must be a "protocol".

Basically "C:\Documents and Settings\All Users\.SF\config\sd.xml" is a pathname, and not a valid URI.

If you want to turn a pathname into a "file:" URI, then do the following:

File f = new File("C:\Documents and Settings\All Users\.SF\config\sd.xml"); URI u = f.toURI(); 

This is the simplest, and most reliable and portable way to turn a pathname into a valid URI in Java. It should work on Windows, Mac, Linux and any other platform that supports Java. (Other solutions that involve using string bashing on a pathname are not portable.)

But you need to realize that "file:" URIs have a number of caveats, as described in the javadocs for the File.toURI() method. For example, a "file:" URI created on one machine usually denotes a different resource (or no resource at all) on another machine.

like image 91
Stephen C Avatar answered Oct 07 '22 18:10

Stephen C


The root cause for this is file path contains the forward slashes instead of backward slashes in windows.

Try like this to resolve the problem:

"file:" + string.replace("\\", "/");   
like image 30
Ankireddy Polu Avatar answered Oct 07 '22 18:10

Ankireddy Polu