Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6379367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:07:45+00:00 2026-05-25T02:07:45+00:00

As per java.nio.file.Path : A relative path cannot be constructed if only one of

  • 0

As per java.nio.file.Path:

A relative path cannot be constructed if only one of the paths have a root component.

Why is this so? Why is it not possible to relativize like so:

Path path1 = Paths.get("/home/test");
Path path2 = Paths.get("home");

// throws an IllegalArgumentException
Path path3 = path1.relativize(path2);

I had imagined that path3 would result in the relative path ../. Why is it valid for Path to return a result that suggests that two directories sit at the same level within the file system if no root elements are defined, yet, when only one path defines a root element (as shown above), no relative path can be determined?

i.e.

Path path1 = Paths.get("home/test");
Path path2 = Paths.get("user");

// results in ../../user
Path path3 = path1.relativize(path2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:07:46+00:00Added an answer on May 25, 2026 at 2:07 am

    A non-absolute path is relative to some unspecified base directory. If you have two such paths, it will make some sense to imagine they are relative to the same (but still unspecified) base directory, and then it makes meaning to ask where one is with respect to the other.

    On the other hand, if you have two paths of which only one is absolute, such as /home/test and home there is no knowing what there relation is. For example if the base directory happens to be /home/test/blah, then home means /home/test/blah/home and it should therefore relativize to blah/home. But how would the method know how to invent blah (or to invent something else entirely)?

    The whole point of using a relative path is to say, I’m not telling you yet what the base directory for this pathname is going to be. Expecting the runtime library to guess the base path that we’re explicitly not telling it would be completely counter to that semantics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm used to the Java model where you can have one public class per
I like the Java convention of having one public class per file, even if
The one class per file rule in Java has me a bit confused. I
In Java, we have two ways to implement actionPerformed: One actionPerformed method per class:
I'm converting a Java server application which used blocking IO and thread-per-client to NIO
As per Java web start where does the jar file get downloaded on a
Per this helpful article I have confirmed I have a connection pool leak in
One of the lines in a java file I'm trying to understand is as
It seems that java.util.Properties assumes one value per propery key. That is, foo=1 foo=2
I want to use java.nio.channels.FileChannel to read from a file, but I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.