0

I'm new to VScode and tomcat-for-java plugin, I want to run my application with the plugin, but I got a permission denied error when starting up. When starting up, the code will create a temp file and a FileOutputStream, the code snippet which throws exception looks like this, please note that the code is in third party jar file so I can't change it.

File myFile = new File("a.txt");
out = new FileOutputStream(myFile);

I tried using the original tomcat to start up the application with command line ./startup.sh under tomcat/bin directory, I see the myFile's absolute path is path/to/tomcat/bin, and when using the VScode tomcat plugin, the myFile's absolute path is /a.txt which definitely no permission to write to.

I searched a lot about this issue and awared that when creating a relative path file in tomcat, the path will be the user.dir(system properties) + filename, and the user.dir will be determined by the path to start up the tomcat, which is path/to/tomcat/bin since the startup.sh is under this directory.

So I tried to add env parameters to VM by passing user.dir to customize JVM options in VScode, the file path was changed successfully, but still got permission denied exception. I even tried to change the file path to the same path with the original tomcat, but still no luck.

So I wonder what's the user.dir system property when running a war package by right click on the war package then select run on tomcat server? How can I solve this problem?

N.Zan
  • 1
  • 1

1 Answers1

0

With tomcat it is mostly better to work with absolute paths (besides any readonly file within the war)

Check the link below

How is user.dir configured for Tomcat servlets?

"user.dir" property is the working folder where Java was executed (example shown here) and you cannot change it not even with -D property when executing the JVM. If you want to change the value of "user.dir", you would have to change the folder where Java executes to start tomcat

inquirymind
  • 135
  • 1
  • 11
  • Thanks for your answer. About the folder where java executes to start tomcat, the question is, I don't really know where it is. I checked the VScode workspace I assigned, under that directory, there are several tomcat directories which should be corresponding to the tomcat instances I created in VScode, but there's no `bin` directory under these tomcats dirs, so how can I know the folder path? Another question about you answer, with the -D property when executing the JVM, the dir path does changed, so I don't quite sure about the answer in your reference. – N.Zan Feb 07 '20 at 23:55
  • Absolute paths actually means you don't need to know where tomcat is running. What do you mean you do not know where is java executing? With System.getProperty("user.dir") you do not have enough information? – inquirymind Feb 08 '20 at 15:41
  • - I can't use absolute path, the code is within a third party jar. - System.getProperty("user.dir") will return `/` path which the application don't have the permission to write any file. - I can change the `user.dir` with JVM parameters but the application still don't have permission to write file on the new path(which seems to be a valid path to write). – N.Zan Feb 10 '20 at 14:21
  • "seems to be" means: 1.- it is not; 2.- a bug. Make sure which one is the problem, the solutions are absolutely different. Gather as much information as possible (error messages, paths, try to list files in the directory, try to read one of them... you name it) and log it so you can find out what's the actual problem – inquirymind Feb 10 '20 at 17:20