Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37363 > unrolled thread
| Started by | Oliver Ruebenacker <curoli@gmail.com> |
|---|---|
| First post | 2017-08-31 02:48 -0700 |
| Last post | 2017-08-31 05:50 -0700 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
From java.nio.Path to Unix-style String on Windows Oliver Ruebenacker <curoli@gmail.com> - 2017-08-31 02:48 -0700
Re: From java.nio.Path to Unix-style String on Windows Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-31 08:10 -0400
Re: From java.nio.Path to Unix-style String on Windows Sebastian <nowhere@nirvana.org> - 2017-09-01 22:35 +0200
Re: From java.nio.Path to Unix-style String on Windows Sebastian <nowhere@nirvana.org> - 2017-09-02 00:12 +0200
Re: From java.nio.Path to Unix-style String on Windows Mike Amling <mamling@chaff.us> - 2017-09-02 22:05 -0500
Re: From java.nio.Path to Unix-style String on Windows Eric Douglas <e.d.programmer@gmail.com> - 2017-08-31 05:50 -0700
| From | Oliver Ruebenacker <curoli@gmail.com> |
|---|---|
| Date | 2017-08-31 02:48 -0700 |
| Subject | From java.nio.Path to Unix-style String on Windows |
| Message-ID | <123f72bc-6240-488c-a007-0048d4468bee@googlegroups.com> |
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?
Thanks!
Best, Oliver
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2017-08-31 08:10 -0400 |
| Message-ID | <oo8u3e$3is$1@dont-email.me> |
| In reply to | #37363 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Sebastian <nowhere@nirvana.org> |
|---|---|
| Date | 2017-09-01 22:35 +0200 |
| Message-ID | <oocgag$api$1@news.albasani.net> |
| In reply to | #37364 |
Am 31.08.2017 um 14:10 schrieb Eric Sosman:
> 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.
>
I believe that under Windows the Path implementation simply stores a
string, which is returned by the toString() method. As there is no logic
involved for deriving the string representation, at least at that point
it would be too late to try and provide a different path separator. So
Eric's suggestion would seem the best you can do.
-- Sebastian
[toc] | [prev] | [next] | [standalone]
| From | Sebastian <nowhere@nirvana.org> |
|---|---|
| Date | 2017-09-02 00:12 +0200 |
| Message-ID | <oocm0n$ra5$1@news.albasani.net> |
| In reply to | #37364 |
Am 31.08.2017 um 14:10 schrieb Eric Sosman:
> 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.
>
Apache Commons IO has a method FilenameUtils#separatorsToUnix, see
https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FilenameUtils.html#separatorsToUnix(java.lang.String)
I don't get the part about stripping away the drive letters. Paths like
C:/blabla/... are perfectly acceptable to my bash (GitBash on Windows).
-- Sebastian
[toc] | [prev] | [next] | [standalone]
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Date | 2017-09-02 22:05 -0500 |
| Message-ID | <oofr89$c24$1@dont-email.me> |
| In reply to | #37375 |
On 9/1/17 5:12 PM, Sebastian wrote: > Am 31.08.2017 um 14:10 schrieb Eric Sosman: >> 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). >>> > > > I don't get the part about stripping away the drive letters. Paths like > C:/blabla/... are perfectly acceptable to my bash (GitBash on Windows). My dim recollection is that the Cygwin-supplied utilities that take file names on the command line (e.g. ls, cat, diff, ...) understand the drive letter just fine but backslashes need to be converted to slashes. --Mike Amling QnV0IHRoZSBub24tQ3lnd2luIHByb2dyYW1zLCBlLmcuIGphdmEuZXhlLCBkb24ndCByZWNvZ25p emVkIC9jeWdkcml2ZS9jL2RpcmVjdG9yeS9maWxlIGV2ZW4gd2hlbiBpbnZva2VkIGJ5IEJhc2gg YW5kIG5lZWQgQzovZGlyZWN0b3J5L2ZpbGUgb3IgQzpcZGlyZWN0b3J5XGZpbGUu
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2017-08-31 05:50 -0700 |
| Message-ID | <13c44e3a-dd90-47a7-adf5-264ca8074df5@googlegroups.com> |
| In reply to | #37363 |
On Thursday, August 31, 2017 at 5:48:36 AM UTC-4, 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? > > Thanks! > > Best, Oliver What's the code look like, at what point exactly is it converting?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web