0

I have a simple code

String staticDir = f.getCanonicalPath() + "/src/main/webapp/static/";

On Windows it will return for me "C:\Temp/src/main/webapp/static/".

How to force Java to use "/" instead of "\"?

I have tried

System.setProperty("file.separator", "/");
String staticDir = f.getCanonicalPath() + "/src/main/webapp/static/";

but it doesn't solved issue for me.

Thank you!

5
  • 4
    Sounds like an XY Problem Commented Jul 18, 2017 at 12:59
  • whys is this a problem for you? does it break something? Commented Jul 18, 2017 at 13:01
  • Yeah, I have another lib, that will process that path. And that function only accepts "/" separators. Commented Jul 18, 2017 at 13:02
  • 1
    I think you should try to get file:///c:/ URI instead. stackoverflow.com/a/2466408/2308683 Commented Jul 18, 2017 at 13:07
  • Are you in fact trying to create a URI? Because if you are, changing the file separator to / is not the correct way to do it. Commented Jul 18, 2017 at 14:42

2 Answers 2

2

Just replace the path separator with the expected one:

String dir = f.getCanonicalPath().replace(System.getProperty("file.separator"), "/")
             + "/src/main/webapp/static/";

If you can, favor Java 7+'s java.nio.file.Path. There, the notion of path separator disappears until you call toString(). But very few libraries use this and still use File or even, worse, String.

If the library you're using still does it by the mean of strings, it probably means they're splitting manually on /. Maybe it's time to tell them to upgrade?

Sign up to request clarification or add additional context in comments.

2 Comments

That's YUICompressor ( github.com/yui/yuicompressor ) - it is not updated for a long time...
That bug is not fixed from 2013 ))) github.com/yui/yuicompressor/issues/111
0

In my case it was a bug in the library, that I use - https://github.com/yui/yuicompressor/issues/111 .

That's why all suggested solution didn't solved issue for me.

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.