Skip to content
← Back to Community
How do i read .env files java
Profile icon

I am trying to make a discord bot in repl-it and i want to make the token secret by putting it in an env files, Now the problem is i don't know how to use an env files inside of java code and open it

Profile icon
Profile icon

how to you get the net. imports to work?

Profile icon

Slight corrections, you can actually set environment variables with the new environment variables:


And to read keys, use System.getenv()

String myKey = System.getenv("KEY"); // assuming you set KEY to 5 in the env tab System.out.println(myKey); // this will print 5

**Note: ** This will NOT work with users that don't have access to your repl! Your secrets are safe!

Profile icon

So click on the padlock icon of the left hand side of the page (the navigation bar)
Then enter the key and value of the variable you want to put in the .env file
Then do the code: System.getenv(“key”) to get the variable from the .env file