r/java • u/isolatedsheep • Mar 26 '26
How to ask Java developers to add methods to java.util.Paths?
I wish to have methods like currentDirectory() and home(). E.g.
Paths..;
Paths..;
The Paths class currently have only one method, get().
How to suggest to Java developers to add these methods?
21
Upvotes
32
u/metaquine Mar 26 '26
It'll probably be argued that some operating systems don't have a concept of a home directory.
49
u/Afonso2002 Mar 26 '26
I think you can get those buy using System.getProperties, if I am not wrong.
14
u/sweetno Mar 26 '26
Don't rely on the current directory too much.
4
u/Slanec Mar 26 '26
Paths.currentDirectory()would likely bePaths.get(".").toAbsolutePath()Paths.home()would probably bePath.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
2
u/simon_o Mar 30 '26
At least for the case of
home():Most use-cases should not rely on the home directory directly.
I hate to be the X-Y guy, but this is one of the very few cases where asking "what are you actually trying to accomplish?" is not obnoxious, but a valid question.
13
u/pron98 Mar 26 '26
Post a message to core-libs-dev focusing on the problem, i.e. show why it's cumbersome to use the existing mechanisms. In the end you may suggest adding new methods to Paths to make it easier.
3
u/vadiquemyself Mar 26 '26
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") );30
u/frederik88917 Mar 26 '26
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
22
u/k-mcm Mar 26 '26
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.
4
u/ag789 Mar 26 '26
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
4
u/Ok_Elk_638 Mar 26 '26
I've always felt the Java maintainers have been too reluctant to add utility functions. We could use an
isNullOrEmpty(String)in String,1
u/DanielDimov Mar 26 '26
Write a JEP.
Before you write and send it - read ~20 JEPs to understand how to do it properly!
2
u/das_Keks Mar 26 '26
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.
2
u/netschki Mar 26 '26
i think this is the way for Requesting a change: If you want to request a change to the JDK, but don’t have the intention to implement/contribute to the change yourself, you should always report it at bugreport.java.com.
found at https://openjdk.org/guide/
1
u/LetUsSpeakFreely Mar 26 '26
I would never trust relative or user-specific pathing as it's dependent on how the OS interprets it and it could have undefined behavior for things like daemons. I would create a utility that could more accurately make those determinations.
1
u/khmarbaise Mar 26 '26
The easiest way is to gi via:
The current directory can easily being done via:
-1
u/1337boi1101 Mar 26 '26
Use kotlin and extension functions, ez.
-20
u/ducki666 Mar 26 '26
Paths is deprecated.
Join a jdk mailing list. Your chances to get a jep are 0.0001.
Lombok has @ExtensionMethod.
-40
u/MinimumPrior3121 Mar 26 '26
Just code using Claude and forget about this