43

I am getting the following error in RAD:

java.net.URISyntaxException: Illegal character in path at index 16: file:/E:/Program Files/IBM/SDP/runtimes/base......

Could you please let me know what is the error and how to resolve it?

Donal Fellows
  • 126,337
  • 18
  • 137
  • 204
Srinivasan
  • 11,248
  • 26
  • 61
  • 88

8 Answers8

62

There's an illegal character at index 16. I'd say it doesn't like the space in the path. You can percent encode special characters like spaces. Replace it with a %20 in this case.

The question I linked to above suggests using URLEncoder:

String thePath = "file://E:/Program Files/IBM/SDP/runtimes/base";
thePath = URLEncoder.encode(thePath, "UTF-8"); 
Jonathon Faust
  • 12,206
  • 4
  • 48
  • 62
  • 1
    file:/// is files. Note the three – Cole Tobin May 11 '12 at 16:04
  • 14
    Hm... it doesn't really work: space is replaced with plus sign, not with %20, and also all the slashes are destroyed too... See here: http://stackoverflow.com/questions/4737841/urlencoder-not-able-to-translate-space-character – Tom Burger Sep 03 '12 at 21:01
  • 2
    Do not use URLEncoder as written below by John it only make things worse. – Tal.Bary Apr 18 '17 at 07:34
  • I encountered this error [ java.net.URISyntaxException: Illegal character in path at index 14: file:/20160703 Tour de France 2016 - stages.csv ] when I was using "cycli" (a Neo4j command line utility) to import data from CSV files via a Cypher script. My CSV file names contained spaces; I replaced those with underscores, and the error went away. – Victoria Stuart Jul 05 '17 at 18:56
27

I ran into the same thing with the Bing Map API. URLEncoder just made things worse, but a replaceAll(" ","%20"); did the trick.

Makram Saleh
  • 8,484
  • 4
  • 25
  • 43
John
  • 693
  • 6
  • 10
12

Did you try this?

new File("<PATH OF YOUR FILE>").toURI().toString();
ceklock
  • 5,851
  • 10
  • 53
  • 76
2

I had a similar problem for xml. Just passing the error and solution (edited Jonathon version).

Code:

HttpGet xmlGet = new HttpGet( xmlContent );

Xml format:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <code>CA</code>
    <name>Cath</name>
    <salary>300</salary>
</employee>

Error:

java.lang.IllegalArgumentException: Illegal character in path at index 0: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contents>
    <portalarea>CA</portalarea>
    <portalsubarea>Cath</portalsubarea>
    <direction>Navigator</direction>
</contents>
    at java.net.URI.create(URI.java:859)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:69)
    at de.vogella.jersey.first.Hello.validate(Hello.java:56)

Not Exactly perfect Solution: ( error vanished for that instance )

String theXml = URLEncoder.encode( xmlContent, "UTF-8" );
HttpGet xmlGet = new HttpGet( theXml );

Any idea What i should be doing ? It just cleared passed but had problem while doing this

HttpResponse response = httpclient.execute( xmlGet );
Mad-D
  • 4,289
  • 16
  • 47
  • 89
2

Had the same problem with spaces. Combination of URL and URI solved it:

URL url = new URL("file:/E:/Program Files/IBM/SDP/runtimes/base");
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());

* Source: https://stackoverflow.com/a/749829/435605

Community
  • 1
  • 1
AlikElzin-kilaka
  • 31,903
  • 30
  • 182
  • 264
  • My problem was that I was using `file://c:/..`, but as you posted here it is `file:/c:/...` (just one slash) – ST7 May 09 '17 at 14:48
0

If this error occurs with the jdk use this :

progra~1 instead of program files in the path example :

 c:/progra~1/java instead of c:/program files/java

It will be ok always avoid space in java code.....

it can be used for every thing in program files, otherwise put quotes at the beginning and the en of path

"c:/..../"

cyril
  • 693
  • 5
  • 22
0

I got this error today and unlike all the above answers my error was due to a new reason.

In my Japanese translation strings.xml file, I had removed a required string.

Some how android mixed up all the other string and this caused an error.

The solution was to include all the strings from my normal, English strings.xml

Including those strings which weren't translated to Japanese.

Bilbo Baggins
  • 3,584
  • 7
  • 38
  • 62
0

the install directory can't have space. reinstall the software will correct it

deskmore
  • 488
  • 4
  • 3
  • 2
    that's one way to solve it but I think you were being sarcastic, right? – Pierre Jul 12 '17 at 15:07
  • This is the sort of backward thinking that let us drag cumbersome remainders of the 70s and 80s in current day software - and waste unnecessary amounts of time and money. Current day software must be capable to handle spaces in paths and non-latin characters, using UTF-8. – Jan Nov 03 '19 at 12:26