[Java] What to do if a lot of "File is opened too much" is displayed due to FileNotFoundException

environment

Event

Many FileNotFoundExceptions saying "File is opened too much" are displayed in the error log.

Caused by: java.io.FileNotFoundException: /user/local/apache-tomcat-8/**/sample/WEB-INF/**/sample.jar (The file is open too much)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:225)
        at java.util.zip.ZipFile.<init>(ZipFile.java:155)
        at java.util.jar.JarFile.<init>(JarFile.java:166)
        at java.util.jar.JarFile.<init>(JarFile.java:103)
・
・
 (abridgement)
・
・
Caused by: java.io.FileNotFoundException: /user/local/apache-tomcat-8/**/sample/WEB-INF/**/sample.jar (The file is open too much)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:225)
        at java.util.zip.ZipFile.<init>(ZipFile.java:155)
        at java.util.jar.JarFile.<init>(JarFile.java:166)
        at java.util.jar.JarFile.<init>(JarFile.java:103)

Cause

If you use ProcessBuilder, InputStream, OutputStream, and ErrorStream will be opened without permission in the background, so it seems that you will run out of resources unless you close all streams.

package sample;

import java.io.IOException;

public class ProcessExecutor {
  
 	public void execute(String... command) throws IOException {
  		ProcessBuilder processBuilder = new ProcessBuilder(command);
  		Process process = processBuilder.start();
  		process.waitFor();
  		process.destroy();
 	}
}

Workaround

The error was resolved by firmly adding the close process to all streams as shown below.

package sample;

import java.io.IOException;

public class ProcessExecutor {
  
 	public void execute(String... command) throws IOException {
  		ProcessBuilder processBuilder = new ProcessBuilder(command);
  		Process process = processBuilder.start();
  		process.waitFor();
  		
  		//Added later
  		process.getInputStream().close();
  		process.getOutputStream().close();
  		process.getErrorStream().close();
  		process.destroy();
 	}
}

Reference summary

Recommended Posts

[Java] What to do if a lot of "File is opened too much" is displayed due to FileNotFoundException
What to do if FacesMessage is set but not displayed
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do if Command line is too long appears when building a gradle project in Intellij IDEA
What to do if you get a wrong number of arguments error in binding.pry
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What to do when rails creates a 〇〇 2.rb file
[Rails] What to do when the view collapses when a message is displayed with the errors method
What to do if you get a java.io.IOException in GlassFish
What to do if audio is not available on discordrb
Road to Java Engineer Part2 What kind of language is Java?
What to do if mysql2 gets a bundle install error
What is a jar file?
What is a Java collection?
What to do about the "cannot be read or is not a valid ZIP file" error
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
[Ubuntu 20.04] What to do if the external monitor is not recognized
What to do if you get a gcc error in Docker
What to do if Cloud9 is full in the Rails tutorial
What to do if the Eclipse Maven dependency Jar is wrong
What to do if you get a DISPLAY error in gym.render ()
What to do if zip dies if there is a pom when making an executable jar with gradle
What is a lambda expression (Java)
[Rails / Docker] What to do if access is denied by the browser (localhost: 3000) after adding a gem
[Gradle] [checkstyle] What to do if the active setting of Checkstyle is removed by "Refresh Gradle project" from Eclipse
What to do if you are told "there is no main manifest attribute" when creating a jar file containing dependencies in a maven project
What to do if you get a groovy warning in Thymeleaf Layout
What to do if the background image is not applied after deployment
[Composer] [Laravel] What to do if you cannot install due to insufficient memory
The story of forgetting to close a file in Java and failing
What to do when "npm ERR! Code ENOSELF" is displayed after npm install
Memo: [Java] If a file is in the monitored directory, process it.
How to find out the Java version of a compiled class file
What to do if tomcat process remains when tomcat is stopped in eclipse
What to do if you get a MiniMagick vulnerability alert on GitHub
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
What to do if you get Could not save master table to file after importing a project in Eclipse
An amateur programmer who doesn't like Java very much tried to solve "10 questions to tell if he is a Java programmer"
What is a Spring Boot .original file?
What to do when a javax.el.PropertyNotWritableException occurs
What is the best file reading (Java)
What is a class in Java language (2 /?)
What to do if you install Ubuntu
What to do if the changes are not reflected in the jar manifest file
Volume that wants to use a lot of logical operators in if statements
[Swift] What to do if the app icon is set but not reflected
What to do if you have installed Java for OS X on macOS
wildflly10 java8 ERROR [org.jboss.jca.core.tx.jbossts.XAResourceRecoveryImpl] (Periodic Recovery) IJ000906 What to do if an error occurs
[Spring Boot] If you use Spring Boot, it was convenient to use a lot of util.
Androd: What to do about "The Realm is already in a write transaction in"
What to do when is invalid because it does not start with a'-'
[Ubuntu] What to do when MongoDB cannot be started due to a SocketException error
What to do if you hit the wall of Too many symbol files when uploading with App Store Connect
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
[Solution] What to do if you get a Docker error'ERROR: Cannot connect to the Docker daemon at unix: ///var/run/docker.sock. Is the docker daemon running?'