Muneeb Mirza
Muneeb Mirza

Reputation: 920

Tomcat returns wrong path for AppData

I am using 3rd party tool built in node for my project build in java. That 3rd party tool is by default installed in

C:\Users\ZXC\AppData\Roaming\npm\xyz

When I run my code in Eclipse it runs fine and gives me correct path of AppData using

System.getenv("AppData")

but this same code when run on tomcat gives me this path

C:\Windows\System32\config\AppData\Roaming

This path is incorrect, there is no AppData folder inside this config folder either. Is there something I am missing, or System.getenv won't work on tomcat?

Upvotes: 0

Views: 801

Answers (1)

ailav
ailav

Reputation: 186

That is because when you're running the application in Eclipse, the owner of the process is your Windows user - ZXC, whereas running Tomcat as a service, it most probably runs it as LocalSystem, and %AppData% default location is different for each user.

Correct way to pass environment variable to Tomcat, without being dependant on user-specific paths would be to create a file CATALINA_BASE/bin/setenv.bat or CATALINA_HOME/bin/setenv.bat for Windows (setenv.sh for *nix environment) and inside of it, set any variable that you might need to use inside of your application. In this case, the content should look something like this:

set “PATH_TO_TOOL=C:\Users\ZXC\AppData\Roaming\npm\xyz"

and then use in your application PATH_TO_TOOL variable (or choose better naming) to avoid any possible further problems because of the specifics of AppData user variable. You can see more details about the usage of setenv script in Tomcat documentation.

Upvotes: 1

Related Questions