Bug 39295 - white spaces in path
Summary: white spaces in path
Status: RESOLVED FIXED
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.6.5
Hardware: PC Windows XP
: P2 major (vote)
Target Milestone: 1.7.0
Assignee: Ant Notifications List
URL:
Keywords: PatchAvailable
: 39037 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-04-13 10:18 UTC by Waldek Herka
Modified: 2008-02-22 12:18 UTC (History)
1 user (show)



Attachments
Launcher.java - fixed the "white spaces in path" problem (9.40 KB, patch)
2006-04-13 10:21 UTC, Waldek Herka
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Waldek Herka 2006-04-13 10:18:00 UTC
A couple of weeks ago I found that sometimes there is problem with launching ant
when the path which is utilized to lookup the "jars" contains some blanks. I
investigated it and changed the code of ant-launcher a little bit. Now the path
is converted to URI and then back to URL, so we can forget about white spaces
and any other error-prone characters. It works perfectly for me - I think it can
be useful for others too.

*************************************************************
Changes in file org.apache.tools.ant.launch.Launcher.java:
1)
import java.net.URLEncoder;
2)
    private void run(String[] args) throws LaunchException, MalformedURLException {
//..
        // now update the class.path property
        StringBuffer baseClassPath
            = new StringBuffer(System.getProperty("java.class.path"));
        if (baseClassPath.charAt(baseClassPath.length() - 1)
                == File.pathSeparatorChar) {
            baseClassPath.setLength(baseClassPath.length() - 1);
        }
//--------------- my modifications -------------------
        URL[] tempJars = new URL[numJars] ;
        int count = 0;
        for(URL _url: jars)
        {        	         	
        	File _file = null;        	
        	_file = new File(_url.getFile());
            tempJars[count++] = _file.toURI().toURL();        	
        }                
        
        
        System.arraycopy(tempJars, 0, jars, 0, tempJars.length);        
//----------------- end of modifications ------------------------        
        for (int i = 0; i < jars.length; ++i) {
            baseClassPath.append(File.pathSeparatorChar);
            baseClassPath.append(Locator.fromURI(jars[i].toString()));
        }

//..
}
Comment 1 Waldek Herka 2006-04-13 10:21:27 UTC
Created attachment 18091 [details]
Launcher.java - fixed the "white spaces in path" problem

Changes from line #235 to #246
Comment 2 Kev Jackson 2006-04-13 11:18:26 UTC
It looks like you've used Java5 (indeed Java 1.4+) code here.  This means that
Ant cannot be compiled on Java 1.2 compilers.

toURI() is the easiest way of encoding the space character as %20, but it's only
available in Java 1.4+.  For the 'enhanced' for loop, that limits the code even
further to Java 1.5+

I'll look into the underlying problem (spaces in path), but if you'd like to
submit code that will compile under JDK 1.2 that would be great

Thanks for the bug report and the code.
Kev
Comment 3 J.M. (Martijn) Kruithof 2006-04-13 19:14:15 UTC
Have you verfied against ant HEAD, I think we fixed this some time ago.
Comment 4 Waldek Herka 2006-04-14 20:13:34 UTC
(In reply to comment #2 and #3)
> Have you verfied against ant HEAD, I think we fixed this some time ago.

Kev: Indeed I'm working on Java 1.5 - if this problem still exist I will change 
it according to 1.2 spec, but first I will check the HEAD since Martijn 
reported that it has been already fixed.

Martijn: I suppose you're right, but I did not use dev code. I will check it 
soon - just after Easter.

BTW: I must say that ANT is my favourite batch-processing tool - it's SIMPLY 
THE BEST!

Comment 5 Matt Benson 2006-04-14 20:38:18 UTC
batch processing tool?  Ant is a build tool... ;) (I abuse it this way too!)
Comment 6 Waldek Herka 2006-04-24 09:20:59 UTC
I saw the last changes related to this bug - unfortunately they didn't fix it at
all :-(
I've made the test and it looked the same as previously... but they give us one
advantage - now we need to change one line only to make this working properly.

Launcher.java[line#272("baseClassPath.append(Locator.fromURI(jars[i].toString()));)"]
to:
************ FIX ********************
"baseClassPath.append(jars[i].toString());"
************ FIX ********************
_______________________________________________________________________________
Since the paths of "jars" have been already encoded in addPath(..) we cannot do
this once again here...
_______________________________________________________________________________

Result of the test with the version from repository:
//-----------------------------------------------------
C:\Program Files\tools test 2\ant\bin>ant
java.lang.IllegalArgumentException
        at java.net.URI.create(URI.java:842)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.tools.ant.launch.Locator.fromURI(Locator.java:160)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:272)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:100)
Caused by: java.net.URISyntaxException: Illegal character in path at index 16:
file:/C:/Program Files/Java/jdk1.5.0_05/lib/tools.jar
        at java.net.URI$Parser.fail(URI.java:2809)
        at java.net.URI$Parser.checkChars(URI.java:2982)
        at java.net.URI$Parser.parseHierarchical(URI.java:3066)
        at java.net.URI$Parser.parse(URI.java:3014)
        at java.net.URI.<init>(URI.java:578)
        at java.net.URI.create(URI.java:840)
//-----------------------------------------------------
.. and after my change:
//-----------------------------------------------------
C:\Program Files\tools test 2\ant\bin>ant
Buildfile: build.xml
     [echo] [windir]    : C:\WINDOWS
     [echo] [temp.dir]  : C:\WINDOWS\temp

test:
     [echo] splash:  file:///C:\Program Files/tools test 2/ant/images/winzip.jpg
     [echo] 10:57:27 AM
     [exec] Result: 1

BUILD SUCCESSFUL
Total time: 7 seconds
//-----------------------------------------------------

I hope it will not harm the processing on the other OSes.
Comment 7 Peter Reilly 2006-04-24 11:31:31 UTC
Can you try the current SVN ant.
I implemently something similar to your request.
It uses Locator.encodeURI implemented in
ant 1.7, instead of File.toURI

I have tested on linux.
export CLASSPATH="/home/preilly/a space"
preilly@backpack:~/tmp/a > ant -f x.xml

Where there is a jar in "a space" that is
needed by x.xml.
Comment 8 Waldek Herka 2006-04-24 12:44:07 UTC
(In reply to comment #7)
> Can you try the current SVN ant.
> I implemently something similar to your request.
> It uses Locator.encodeURI implemented in
> ant 1.7, instead of File.toURI
> 
> I have tested on linux.
> export CLASSPATH="/home/preilly/a space"
> preilly@backpack:~/tmp/a > ant -f x.xml
> 
> Where there is a jar in "a space" that is
> needed by x.xml.
> 

Indeed it was implemented this way, but please try to put JAVA environment in
Windows' default location (C:\Program Files\Java\..) and you'll get this error
for sure.
I have just investigated it once again - in fact I gave the workaround not the
real solution... it works but I can give you a better one.
The real problem was encoding of "tools.jar" - it hasn't been encoded at all, so
if you've installed JDK somewhere in "spacing" location you get into trouble as
I did.


New refined fix:
Launcher.java - line#258 "jars[jars.length - 1] = toolsJar.toURL();" should be
changed to:
**************** FIX ***********************
 if (toolsJar != null) {
        	try {
        		jars[jars.length - 1] = new
URL(Locator.encodeURI(toolsJar.toURL().toString()));
            } catch (UnsupportedEncodingException ex) {
                throw new MalformedURLException(ex.toString());
            }
        }
**************** FIX ***********************
Comment 9 Peter Reilly 2006-04-24 14:17:10 UTC
Opps, Sorry, I did not read our previous message correctly.
I have made the change now (after extracting the fileToURL code
to a method). Thanks for reporting the problem and showing
the solution!

Peter
Comment 10 Waldek Herka 2006-04-24 14:25:40 UTC
(In reply to comment #9)
> Opps, Sorry, I did not read our previous message correctly.
> I have made the change now (after extracting the fileToURL code
> to a method). Thanks for reporting the problem and showing
> the solution!
> 
> Peter

You're welcome :-)

BTW: I'm gonna play around ANT since I found it very useful and powerfull tool,
so I suppose I will be sending some code (fixes or extensions) from time to time..
Comment 11 Peter Reilly 2006-05-03 16:54:40 UTC
*** Bug 39037 has been marked as a duplicate of this bug. ***