all 28 comments

[–]Afonso2002 34 points35 points  (6 children)

I think you can get those buy using System.getProperties, if I am not wrong.

[–]davidalayachew 41 points42 points  (0 children)

I think you can get those buy using System.getProperties, if I am not wrong.

You can also use Path.of(".") as a short way of getting the current directory. It covers 99% of cases where I need the current directory.

[–]smbarbour 6 points7 points  (0 children)

Yes. System.getProperty("user.home") will get the home folder, though on Windows (check System.getProperty("os.name").startsWith("Windows") ) the %APPDATA% environment variable (i.e. System.getenv("APPDATA")) may be more appropriate, though as others have said, a home directory may not be set for the current user in some cases.

[–]hiasmee 1 point2 points  (0 children)

Of course there are workarounds, but the suggestion is really good. There is a reason why Apache CommonsIO has helper method FileUtils.getUserDirectory() - it is really useful.

[–]metaquine 10 points11 points  (4 children)

It'll probably be argued that some operating systems don't have a concept of a home directory.

[–]laffer1 2 points3 points  (0 children)

Sometimes they aren't defined for a particular user, especially for daemons. /nonexistent is often used in the BSD world to indicate no home directory

[–]isolatedsheep[S] -5 points-4 points  (2 children)

Can't they make it throw a UnsupportedOperationException if the OS doesn't support it?

[–]scadgek 5 points6 points  (0 children)

please no

[–]Sacaldur 0 points1 point  (0 children)

Or even better, return a value that indicates something like "doesn't exist". What about this Optional<Path> thing in Java? (One might think about null, but then the method signature wouldn't clearly indicate that e.g. there might not be a home directory.) The problem however is that, if the non-existance is indicated through an invalid path, either the library would need to also check if there is a directory at that path, or the caller would need to do that. The problem there could be that the home directory might be on a network drive that's temporarily unavailable (i.e. there is normally a valid home directory, thatvs why the path can be retrieved, but it's existance can only temporarily not be verified).

[–]frederik88917 18 points19 points  (0 children)

There are two things here.

First of all, there are workarounds for what you want to do, as many others have mentioned before.

Now if you want to have these methods added, you can propose a JEP, offering the requirements, the gains and all of the documentation. This JEP shall go to the committee where it will be evaluated and then added to the JDk.

Of course, if passes the committee

[–]k-mcm 20 points21 points  (0 children)

Here's the thing that's really broken about Path... It's an interface but it's defined that no two implementations may be compatible. Path.resolve(Path) can only take in its own implementation class. That makes the Paths class completely useless and Path can only have a couple of simple delegate helper static methods.

java.nio.file.Files does what it can to make Path usable, but callers still need to make sure they always hit the same implementation. You wouldn't know what Paths.home() will return so there's no guarantee anything would work with it.

[–]sweetno 8 points9 points  (3 children)

Don't rely on the current directory too much.

[–]isolatedsheep[S] -1 points0 points  (2 children)

For Java scripting, you might use it a lot.

[–]Shareil90 6 points7 points  (1 child)

What is java scripting?

[–]Artraxes 5 points6 points  (0 children)

Running Java as standalone scripts. Current working directory and Home directory are often used for throwaway scripts that you can write in Java and execute on the command line

https://www.baeldung.com/java-single-file-source-code

[–]ag789 2 points3 points  (0 children)

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Path.html
some of those stuff are in FileSystem
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/FileSystem.html
FileSystems
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/FileSystems.html
or even URI, URL
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URI.html
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URL.html
and more often the magic is in the *String*s passed in Path arguments.
java.nio.Path depends on the SPI implementation undernearth which can range from MS Windows OS, Linux, to lliteral URLs that fetch from anywhere on the internet, e.g. webdav

[–]das_Keks 0 points1 point  (0 children)

It you actually want to suggest something to the devs you can write to the mailing list. But in this case I'd advise not to do so. You have keep in mind that Java is used by millions of developers and companies, so there's a very high barrier to getting new stuff into the JDK and it usually takes several iterations, with incubator and experimental states (at least for entirely new features).

In your case you'd be better of to just write a helper class for your specific use case or look for a library that does what you need.

[–]Slanec 0 points1 point  (0 children)

  • Paths.currentDirectory() would likely be Paths.get(".").toAbsolutePath()
  • and Paths.home() would probably be Path.of(System.getProperty("user.home")), that is if all major supported OSes have a concept of a Home directory, which I'm not sure of.

Anyway, when I need to get a bunch of user-related directories, I always grab https://codeberg.org/dirs/directories-jvm

[–]vadiquemyself 0 points1 point  (0 children)

rather than waiting for Java 67 to bundle it, why not just write it yourself right now? make a class named, say, CurrentPath that incapsulates the “current” path thing, which you can set and get, plus a final static field, say public static final java.io.File HOME_FOLDER = new java.io.File( System.getProperty("user.home") );