48

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
  • 133,037
  • 18
  • 149
  • 215
Srinivasan
  • 11,718
  • 30
  • 64
  • 92

7 Answers7

66

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,396
  • 4
  • 50
  • 63
  • 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
  • 3
    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,613
  • 4
  • 27
  • 44
John
  • 701
  • 6
  • 10
14

Did you try this?

new File("<PATH OF YOUR FILE>").toURI().toString();
ceklock
  • 6,143
  • 10
  • 56
  • 78
  • 1
    When the 'correct' answer doesn't work. This one is simpler and works...and probably works on multiple platforms too. Thank you. – Scott Hather Aug 01 '23 at 13:42
3

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
  • 34,335
  • 35
  • 194
  • 277
  • 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
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,479
  • 18
  • 52
  • 93
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
  • 872
  • 6
  • 29
0

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

deskmore
  • 518
  • 4
  • 3
  • 3
    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