What's the correct parameter value of System.getenv("OUTPUT_PATH")?

I know that this question is bit old, but maybe somebody will have benefits from this answer...

This line:

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

reads the environment variable defined in Hackerrank runtime/testing environment to determine the place where results will be stored for further analysis.

In order to use exactly the same code, you have to create this variable on your system and use it, or change to store results on different place (as it is already explained in previous answers and comments).

But, ...

Since this is used for a Hackerrank solving, I think it is better to have all outputs redirected to system.out instead of file, because it is far more useful to see results at runtime (or debug) in the IDE console rather than places it in a file.

So, this line is better to change on this way:

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));

which will stream your BufferedReader to System.out.

In that case You will have same output as it is on Hackerrank test cases, but in your IDE console instead in file.


As I said in the comments, you're not supposed to replace it. That's an environment variable in the shell that is used to run your solution.

Seeing as the value of the variable is passed to FileWriter, this means that it represents the name of a file.

You can replicate it in a terminal by running your program with the command:

env OUTPUT_PATH=/path/to/some/file java Solution

This will start a new shell which contains a variable called OUTPUT_PATH pointing to a file called /path/to/some/file and when the program starts, the file name will be used


That "OUTPUT_PATH" is an environmental variables. You have to declare that variable in your operating system to use it. Generally website like hackerrank do it because learning the path of there system is not good for security I guess. You can test your code in IDE but the environmental variables will be not there you need to declare. I hope it helped you for your confusion.