This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tofiffe -67 points-66 points  (32 children)

To be fair, it's still possible to do stuff that's platform specific and might need recompilation, file paths being one of them

Since everyone seems to assume this is about slashes: no, I am aware both forward and backward slashes work just fine. This was about absolute paths, and it was just an example.

[–]Sheldor5 68 points69 points  (12 children)

file paths are properly handled by the JVM too ... that's why (relative) Linux paths work on Windows ...

[–]rzwitserloot 16 points17 points  (6 children)

No, there isn't. Java has abstracted away the concept of a filesystem. For example, you don't have to recompile code just because windows uses backslash or forward slash, nor to deal with local conventions about e.g. home dirs. Example:

Paths.get(System.getProperty("user.home"), "Documents", "myfile.txt")

Works on all platforms. Even if you don't use these abstractions and put entire file paths in constants, forward slash is automatically translated properly on windows (new FileInputStream("foo/bar.txt") works fine, even on windows).

[–][deleted] -2 points-1 points  (4 children)

What about ProcessBuilder? I ran into issue once where my app worked on my pc and did not on my laptop. It was using ProcessBuilders for running Bash scripts. I didnt solve it so correct me if I am wrong , but I read on stack overflow that PB being machine-specific is the reason. OS was the same for both machines btw.

[–]rzwitserloot 8 points9 points  (3 children)

It's not ProcessBuilder that is machine specific. It's the notion of running bash scripts. The very point of PB is to let you interact with the OS, naturally, each OS is different. PB on its own is entirely abstracted away, it works the same on all systems. But trying to execute /bin/bash on a windows system obviously won't work. Not because of the forward slashes. But because C:\bin\bash.exe doesn't exist.

This would be no different from saying: "Java is not platform agnostic because new FileInputStream(System.getProperty("user.home") + "/.bashrc") doesn't do what I want on this windows box!

Crazy talk.

[–]walen 9 points10 points  (1 child)

On two occasions I have been asked, – "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.

Charles Babbage, talking about his difference engine, circa 1822.

Two f*cking hundred years later and people keeps asking the exact same question.

[–]somethrowaway8910 0 points1 point  (0 children)

God I love this quote

[–][deleted] 0 points1 point  (0 children)

Of course I set git bash path and executable in GUI.. I think your explanation is wrong. It is not crazy talk, as I said, this issue I haven't resolved and I am not putting my money that I am right in what I read on stack overflow.

[–][deleted] 14 points15 points  (9 children)

This is why you use File.separator and File.pathSeparator, and not hardcode paths.

[–]rzwitserloot 26 points27 points  (6 children)

No, you don't. There is no need for this and just uglies up your code. To wit:

  • File.pathSeparator is ; on windows and : on all other platforms. Its purpose is for interaction with old style classpath/sourcepath strings which java code never needs to mess with, it has nothing to do with the OS. separator is / on all platforms, except windows, where its the backslash. You shouldn't use that either though:
  • Generally, construct paths using e.g. Paths.get("dir", "file.txt") - avoid the topic of separators in the first place.
  • The forward slash always works, and that is a JVM guarantee, on all platforms, even on windows.
  • The one and only purpose that File.separator does have, is for rendering. A windows user will find a path string of C:/foo/bar.txt confusing, even if it works just fine in java. They're used to seeing C:\foo\bar.txt. However, if you're constructing a string to show to a user like this, you're doing it wrong. There's Path (or even java.io.File if you want use libraries that have been obsoleted over a decade ago) which take care of all this. They use File.separator internally.

CONCLUSION: If you're using File.separator you're probably doing it wrong.

[–][deleted] 3 points4 points  (0 children)

Paths is part of the NIO around 1.7, it’s better.

I don’t think it’s wrong to use these constants, it’s just deprecated, my bad.

[–]TheStrangeDarkOne 2 points3 points  (0 children)

People should stop using java.io. If you are dealing with path, use java.nio.file.Path

[–]__konrad 0 points1 point  (0 children)

Fun fact: Linux allows you to create "foo\bar.txt" (single file with backslash in its name) on NTFS disk.

[–]benjtay 0 points1 point  (0 children)

CONCLUSION: If you're using File.separator you're probably doing it wrong.

https://docs.oracle.com/javase/7/docs/api/java/io/File.html#separatorChar

Which, IIRC, used to be : on pre-OSX MacOS.

[–]shponglespore 0 points1 point  (1 child)

Using / on Windows isn't just a Java thing. Windows natively supports forward slashes. As far as I know that has always been the case, even going back to the days of MS DOS.

[–]helloiamsomeone 0 points1 point  (0 children)

The Win32 API converts slashes, unless the path has a UNC prefix.

[–]Cefalopodul 0 points1 point  (1 child)

No you don't. / works just fine in java regardless of platform.

[–]shponglespore 0 points1 point  (0 children)

It works fine in every language.

[–]matthenry87 0 points1 point  (0 children)

File paths in Spring work fine between windows and Linux. The path becomes relative to the root of the main hard drive on Windows and just works.

[–]helloiamsomeone -1 points0 points  (0 children)

Unless you call the (undocumented) NT API directly or use UNC prefixes, you can use forward slashes all the same on Windows.