0

Let's say I want to pass the following paths on the command line:

Path a: C:\example a\test
Path b: C:\example b\test\

java -jar myjar.jar C:\example a\test C:\example b\test\

Java splits arguments using whitespace, so we would end up with an args array like this:

arr[0] = C:\example
arr[1] = a\test
arr[2] = C:\example
arr[3] = b\test\

But if we also want to accept non-absolute paths, so supplying "\test" will cause the program to accept that as <parent directory>\test.

This gives a lot of problems and is much more complicated than it first appears. How do we tell Java that "a\test" is actually a part of "C:\example a\test" rather than "<parent directory>\a\test"?

2
  • 3
    Enclose the paths in quotes. Commented Aug 21, 2013 at 18:43
  • Did you try surrounding the path in quotes? Commented Aug 21, 2013 at 18:43

2 Answers 2

1

Use quotes:

java -jar myjar.jar "C:\example a\test" "C:\example b\test\"
Sign up to request clarification or add additional context in comments.

Comments

0

Create a canonical file and extract the absolute name from it:

try {
    File file = (new File(arr[0])).getCanonicalFile();
    arr[0] = file.getAbsolutePath();

} catch (IOException e) {
    // handle exception
}

This way you can compare them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.