4

How to get a File java object from its path string with environments vars? For example:

new File("%SYSTEM_ROOT%/file.txt")

or

new File("${SYSTEM_ROOT}/file.txt")
4
  • 1
    Shouldn't the OP be using System.getenv? Commented May 20, 2013 at 15:38
  • 1
    I would like to know if exists a way to use enviroments variables in Files without replacing it manually! Commented May 20, 2013 at 15:38
  • @fge: System.getProperty() won't return an environment variable value. Commented May 20, 2013 at 15:44
  • @JBNizet Oops, yep, I mixed with .getenv() Commented May 20, 2013 at 16:01

2 Answers 2

2

Syntax that allows getting value of environment variable using % sign is a feature of windows shell. In java you should use System.getenv("SYSTEM_ROOT") instead, so your line should look like: new File(System.getenv("SYSTEM_ROOT") + "/file.txt") or even better new File(new File(System.getenv("SYSTEM_ROOT"), "file.txt")

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

1 Comment

Ok, the trouble is that the path is stored in a DB VAR, so i have this String "%SYSTEM_ROOT%/file.txt" (or "${SYSTEM_ROOT}/file.txt") and i need the relative object... however I understand there is no way, I may provide a regex replacement.
1

I know this Question is a little bit old, but no Answer yet.

In another Question about environment variables there is a simliar Problem and they have a sample code for regex:
System Variable Regex

This code replaces all system vars that are in the format ${ENV_VAR}.

Hope that helps,
Kalasch

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.