在Windows中,一切正常,我只是使用JavaFX和Java 1.8.0_131从操作系统中select一个文件。
# # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00007fa5f45cde90, pid=4843, tid=0x00007fa59b31c700 # # JRE version: Java(TM) SE Runtime Environment (8.0_131-b11) (build 1.8.0_131-b11) # Java VM: Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode linux-amd64 compressed oops) # Problematic frame: # C [libpthread.so.0+0x9e90] pthread_mutex_lock+0x0 # # Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again # # An error report file with more information is saved as: # /home/alexander/Desktop/Roots/hs_err_pid4843.log # # If you would like to submit a bug report, please visit: # http://bugreport.java.com/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. # Aborted (core dumped)
您可以使用的代码:
import java.awt.Desktop; import java.io.File; import java.io.IOException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; import javafx.stage.Stage; public final class FileChooserSample extends Application { private Desktop desktop = Desktop.getDesktop(); @Override public void start(final Stage stage) { stage.setTitle("File Chooser Sample"); final FileChooser fileChooser = new FileChooser(); final Button openButton = new Button("Open a Picture..."); final Button openMultipleButton = new Button("Open Pictures..."); openButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(final ActionEvent e) { configureFileChooser(fileChooser); File file = fileChooser.showOpenDialog(stage); if (file != null) { openFile(file); } } }); openMultipleButton.setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(final ActionEvent e) { configureFileChooser(fileChooser); List<File> list = fileChooser.showOpenMultipleDialog(stage); if (list != null) { for (File file : list) { openFile(file); } } } }); final GridPane inputGridPane = new GridPane(); GridPane.setConstraints(openButton, 0, 0); GridPane.setConstraints(openMultipleButton, 1, 0); inputGridPane.setHgap(6); inputGridPane.setVgap(6); inputGridPane.getChildren().addAll(openButton, openMultipleButton); final Pane rootGroup = new VBox(12); rootGroup.getChildren().addAll(inputGridPane); rootGroup.setPadding(new Insets(12, 12, 12, 12)); stage.setScene(new Scene(rootGroup)); stage.show(); } public static void main(String[] args) { Application.launch(args); } private static void configureFileChooser(final FileChooser fileChooser){ fileChooser.setTitle("View Pictures"); fileChooser.setInitialDirectory( new File(System.getProperty("user.home")) ); } private void openFile(File file) { try { desktop.open(file); } catch (IOException ex) { Logger.getLogger( FileChooserSample.class.getName()).log( Level.SEVERE, null, ex ); } } }
以上代码取自Oracle教程 – > !链接