Benjamin Wunder
Benjamin Wunder

Reputation: 131

Tomcat servlet relative path referring to root

I'm using Java Spring Boot and deploying to Tomcat 8.1 webapp.

When I create a new File() with a relative path, it is referring to the absolute root of the system.

For instance,

File x = new File("./temp.txt");

System.out.println("X Path: " + x.getAbsolutePath());

The output is

/./temp.txt

Why is this? I'm getting a Java.nio.File.AccessDeniedException because Tomcat does not have permissions to write to the root.

The code is a third party library creating a cache file, so I can't feasibly just change to absolute path.

What causes Tomcat to use / as my working directory?

Upvotes: 1

Views: 724

Answers (1)

Benjamin Wunder
Benjamin Wunder

Reputation: 131

So I determined the problem. Tomcat, when executed as a service (CentOS: systemctl start tomcat) the WorkingDirectory defaults to root if it is not set.

By editing the tomcat.service file in /etc/systemd/system and adding under the [Service] section the following:

WorkingDirectory=/opt/tomcat

I was able to move the directory where the third party library was writing.

Upvotes: 1

Related Questions