Issue
I have a Java program that previously run in Tomcat 8.
Now, I already upgrade my Tomcat to Tomcat 9 and I find out that the program giving me errors at getAbsoluteFile
.
I suspect the error is because of the limitation of access right for the folder.
However, I already change the permission to chown root, chmod 777, but it still give the same error-
java.io.FileOutputStream.open(FileOutputStream.java:270) Jun 1 16:48:29 te tomcat9[7844]: #011at java.io.FileOutputStream.(FileOutputStream.java:213) Jun 1 16:48:29 te tomcat9[7844]: #011at java.io.FileOutputStream.(FileOutputStream.java:162) Jun 1 16:48:29 te tomcat9[7844]: #011at java.io.FileWriter.(FileWriter.java:90) Jun 1 16:48:29 te tomcat9[7844]: #011at auap.AdaptiveAuthService.logWriter(AdaptiveAuthService.java:1134) Jun 1 16:48:29 te tomcat9[7844]: #011at auap.AdaptiveAuthService.logWriter(AdaptiveAuthService.java:1151)
The same piece of code is working fine in Tomcat 8 but not in Tomcat 9.
Is there any limitation of security enhancement of Tomcat 9 that cause my code cannot run?
Above is the code that I use to create a file in both Tomcat8 and Tomcat9.
private static int logWriter(String data) {
Calendar now = Calendar.getInstance();
int year_file = now.get(Calendar.YEAR);
int month_file= (now.get(Calendar.MONTH) + 1);
int date_file = now.get(Calendar.DATE);
String filename = "LogAdaptive"+year_file+month_file+date_file;
File logFile = new File("/var/logs/tomcat9/"+filename+".txt");
boolean blnExists = logFile.exists();
FileWriter fw = null;
FileWriter fstream = null;
BufferedWriter bw = null;
BufferedWriter fbw = null;
try {
if (!blnExists) // if not exist, create file
{
fw = new FileWriter(logFile.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(data);
String newLine = System.getProperty("line.separator");
bw.write(newLine);
bw.close();
fw.close();
} else // if exist, amend file
{
fstream = new FileWriter(logFile, true);
fbw = new BufferedWriter(fstream);
fbw.write(data);
fbw.newLine();
fbw.close();
fstream.close();
}
} catch (Exception e) {
logWriter(EXCEPTION_STR0 + e);
} finally {
try {
if (fw != null){
fw.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
try {
if (fstream != null){
fstream.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
try {
if (bw != null){
bw.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
try {
if (fbw != null){
fbw.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
}
return 1;
} // end logWriter function
Solution
I suspect that you would have given 777 access to a directory, but not to its subdirectory. use chmod -R 777
to give access to the directory, subdirectory and files inside the directory.
But, normally we do not give 777 access. We start our application process, in your case it is the tomcat start script or service, with the appropriate user who has appropriate privilege with all the dependent directory.
if you start your application with "root" user or any user who have who have appropriate privilege to access the dependent directories, your issue will be resolved.
Answered By - Sivaraj Velayutham