Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: From java.nio.Path to Unix-style String on Windows Date: Thu, 31 Aug 2017 08:10:28 -0400 Organization: A noiseless patient Spider Lines: 28 Message-ID: References: <123f72bc-6240-488c-a007-0048d4468bee@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 31 Aug 2017 12:06:06 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="4ffc29eb47fd5939a298e0eee6d13e71"; logging-data="3676"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tePZWhG/EYpegHbQISqP6" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 In-Reply-To: <123f72bc-6240-488c-a007-0048d4468bee@googlegroups.com> Content-Language: en-US Cancel-Lock: sha1:08I2mgPG5wzCQ8BjzD3XkqgC/p4= Xref: csiph.com comp.lang.java.programmer:37364 On 8/31/2017 5:48 AM, Oliver Ruebenacker wrote: > > Hello, > > I have an app that generates command lines to be executed by Bash (the popular Unix shell). The command lines contain file names that are provided as java.nio.Path which are converted to String and then inserted. > > When run on Unix or like, a Path is converted to String Unix-style (e.g. /a/b/c), and that is just as Bash expects it. > > If run on Windows, however, a Path is converted to String Windows-style (e.g. C:a\b\c), and that fails, because Bash expects Unix-style file paths, even if it is run on Windows (e.g. Cygwin Bash, or Bash on Windows). > > How can I make sure a Path is always converted to String Unix-style, even if run on Windows? The "clean" (but probably impractical) way would be to install a Unix-style FileSystemProvider on the Windows machine, and use it to create and string-ize the Path. There are only two FSP's in the standard Windows JRE (one for Windows files, one for ZIP archives), so I'm not sure how you would manage this ... The "dirty" (but probably feasible) way would be to string-ize the Path, split the String on System.getProperty("file.separator"), and reassemble the pieces using "/" between them. You would still need to strip away the leading "C:" (or "D:" or "//share"), which is more than a little bit hackish, but I can't see any way to avoid the hackery. -- esosman@comcast-dot-net.invalid Twelve hundred thirty-eight days to go.