When I add a stylesheet to the scene, it seems that the stylesheet file is opened for every node in the scene and not closed properly. If there area many nodes in the scene, this causes a "Too many open files" error on the next I/O action. The following sample throws the error for me on Linux. To make it work, also create an empty "style.css" file next to the source file.
import java.nio.file.Paths;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class AddStylesheetTest extends Application {
private static String SHEET = AddStylesheetTest.class.getResource("style.css").toExternalForm();
public static void main(String[] args) {
launch(args);
}
private StackPane pane;
@Override
public void start(Stage stage) {
pane = new StackPane();
Scene scene = new Scene(pane, 200, 200);
scene.getStylesheets().add(SHEET);
stage.setScene(scene);
stage.show();
pane.getChildren().add(group());
replaceGroup(5);
}
private Group group() {
Group g = new Group();
for(int i = 0; i < 100; ++i) {
for(int j = 0; j < 100; ++j) {
g.getChildren().add(new Rectangle(2*i, 2*j, 1, 1));
}
}
return g;
}
private void replaceGroup(int n) {
if(n > 0) {
Platform.runLater(() -> {
pane.getChildren().clear();
pane.getChildren().add(group());
replaceGroup(n - 1);
});
} else {
// throws an exception due to too many open files
Paths.get("/any/path");
}
}
}
This is the error I get:
Exception in thread "JavaFX Application Thread" java.lang.UnsatisfiedLinkError: /home/tomas/jdk8u20-b05/jre/lib/amd64/libnio.so: /home/tomas/jdk8u20-b05/jre/lib/amd64/libnio.so: cannot open shared object file: Too many open files
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1929)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1835)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at sun.nio.fs.UnixNativeDispatcher$1.run(UnixNativeDispatcher.java:573)
at sun.nio.fs.UnixNativeDispatcher$1.run(UnixNativeDispatcher.java:571)
at java.security.AccessController.doPrivileged(Native Method)
at sun.nio.fs.UnixNativeDispatcher.<clinit>(UnixNativeDispatcher.java:571)
at sun.nio.fs.UnixFileSystem.<init>(UnixFileSystem.java:67)
at sun.nio.fs.LinuxFileSystem.<init>(LinuxFileSystem.java:39)
at sun.nio.fs.LinuxFileSystemProvider.newFileSystem(LinuxFileSystemProvider.java:46)
at sun.nio.fs.LinuxFileSystemProvider.newFileSystem(LinuxFileSystemProvider.java:39)
at sun.nio.fs.UnixFileSystemProvider.<init>(UnixFileSystemProvider.java:56)
at sun.nio.fs.LinuxFileSystemProvider.<init>(LinuxFileSystemProvider.java:41)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at sun.nio.fs.DefaultFileSystemProvider.createProvider(DefaultFileSystemProvider.java:48)
at sun.nio.fs.DefaultFileSystemProvider.create(DefaultFileSystemProvider.java:63)
at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:108)
at java.nio.file.FileSystems$DefaultFileSystemHolder.access$000(FileSystems.java:89)
at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:98)
at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:96)
at java.security.AccessController.doPrivileged(Native Method)
at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(FileSystems.java:96)
at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(FileSystems.java:90)
at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
at java.nio.file.Paths.get(Paths.java:84)
at AddStylesheetTest.replaceGroup(AddStylesheetTest.java:53)
at AddStylesheetTest.lambda$0(AddStylesheetTest.java:49)
at AddStylesheetTest$$Lambda$63/527321197.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.access$200(GtkApplication.java:48)
at com.sun.glass.ui.gtk.GtkApplication$6$1.run(GtkApplication.java:149)
at java.lang.Thread.run(Thread.java:744)
import java.nio.file.Paths;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class AddStylesheetTest extends Application {
private static String SHEET = AddStylesheetTest.class.getResource("style.css").toExternalForm();
public static void main(String[] args) {
launch(args);
}
private StackPane pane;
@Override
public void start(Stage stage) {
pane = new StackPane();
Scene scene = new Scene(pane, 200, 200);
scene.getStylesheets().add(SHEET);
stage.setScene(scene);
stage.show();
pane.getChildren().add(group());
replaceGroup(5);
}
private Group group() {
Group g = new Group();
for(int i = 0; i < 100; ++i) {
for(int j = 0; j < 100; ++j) {
g.getChildren().add(new Rectangle(2*i, 2*j, 1, 1));
}
}
return g;
}
private void replaceGroup(int n) {
if(n > 0) {
Platform.runLater(() -> {
pane.getChildren().clear();
pane.getChildren().add(group());
replaceGroup(n - 1);
});
} else {
// throws an exception due to too many open files
Paths.get("/any/path");
}
}
}
This is the error I get:
Exception in thread "JavaFX Application Thread" java.lang.UnsatisfiedLinkError: /home/tomas/jdk8u20-b05/jre/lib/amd64/libnio.so: /home/tomas/jdk8u20-b05/jre/lib/amd64/libnio.so: cannot open shared object file: Too many open files
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1929)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1835)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at sun.nio.fs.UnixNativeDispatcher$1.run(UnixNativeDispatcher.java:573)
at sun.nio.fs.UnixNativeDispatcher$1.run(UnixNativeDispatcher.java:571)
at java.security.AccessController.doPrivileged(Native Method)
at sun.nio.fs.UnixNativeDispatcher.<clinit>(UnixNativeDispatcher.java:571)
at sun.nio.fs.UnixFileSystem.<init>(UnixFileSystem.java:67)
at sun.nio.fs.LinuxFileSystem.<init>(LinuxFileSystem.java:39)
at sun.nio.fs.LinuxFileSystemProvider.newFileSystem(LinuxFileSystemProvider.java:46)
at sun.nio.fs.LinuxFileSystemProvider.newFileSystem(LinuxFileSystemProvider.java:39)
at sun.nio.fs.UnixFileSystemProvider.<init>(UnixFileSystemProvider.java:56)
at sun.nio.fs.LinuxFileSystemProvider.<init>(LinuxFileSystemProvider.java:41)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at sun.nio.fs.DefaultFileSystemProvider.createProvider(DefaultFileSystemProvider.java:48)
at sun.nio.fs.DefaultFileSystemProvider.create(DefaultFileSystemProvider.java:63)
at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:108)
at java.nio.file.FileSystems$DefaultFileSystemHolder.access$000(FileSystems.java:89)
at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:98)
at java.nio.file.FileSystems$DefaultFileSystemHolder$1.run(FileSystems.java:96)
at java.security.AccessController.doPrivileged(Native Method)
at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(FileSystems.java:96)
at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(FileSystems.java:90)
at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
at java.nio.file.Paths.get(Paths.java:84)
at AddStylesheetTest.replaceGroup(AddStylesheetTest.java:53)
at AddStylesheetTest.lambda$0(AddStylesheetTest.java:49)
at AddStylesheetTest$$Lambda$63/527321197.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.access$200(GtkApplication.java:48)
at com.sun.glass.ui.gtk.GtkApplication$6$1.run(GtkApplication.java:149)
at java.lang.Thread.run(Thread.java:744)