Macos 通过双击文件将参数传递给JavaFx应用程序
我创建了一个JavaFX应用程序,部署了.app文件,它运行良好。然后,我将操作系统设置为使用我的应用程序打开具有特定扩展名的所有文件。我的问题是,当我双击一个文件时,我打开了我的应用程序,但我不知道是哪个文件打开了它 我试图用函数Macos 通过双击文件将参数传递给JavaFx应用程序,macos,javafx,Macos,Javafx,我创建了一个JavaFX应用程序,部署了.app文件,它运行良好。然后,我将操作系统设置为使用我的应用程序打开具有特定扩展名的所有文件。我的问题是,当我双击一个文件时,我打开了我的应用程序,但我不知道是哪个文件打开了它 我试图用函数getParameters().getRaw()检查应用程序参数,但它总是返回一个空列表 有人知道如何检索打开应用程序的文件的路径吗?我终于找到了解决这个问题的方法 为了回答这个问题,我创建了一个由三个类组成的示例应用程序: Launcher MyApp Handle
getParameters().getRaw()
检查应用程序参数,但它总是返回一个空列表
有人知道如何检索打开应用程序的文件的路径吗?我终于找到了解决这个问题的方法 为了回答这个问题,我创建了一个由三个类组成的示例应用程序:
Launcher
MyApp
Handler_OpenFile
MyApp是扩展javafx.application.application类的类,Handler_OpenFile用于处理双击事件,最后启动程序是包含main的启动程序
Launcher.java:此类必须存在,因为如果在扩展javafx.application.application的类中定义了main,则OpenFileEvent将无法正常工作(更准确地说,只有在应用程序已打开时才会触发OpenFileEvent)
Handler_OpenFile.java:这个类定义了一个静态变量,用于存储打开应用程序的文件的值。这可能不是最好的解决方案,但这是我目前唯一能让它工作的方法
public class Handler_OpenFile implements OpenFilesHandler {
public static File file = null;
@Override
public void openFiles(OpenFilesEvent e) {
for (File file : e.getFiles()){
this.file = file;
}
}
}
java:该类将能够访问Handler_OpenFile类中分配的静态值,并检索打开文件的绝对路径
public class MyApp extends Application {
@Override
public void start(Stage primaryStage) {
Logger logger = Logger.getLogger("log");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler("../Logfile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info("Application launched from: " + Handler_OpenFile.file.getAbsolutePath());
} catch (SecurityException | IOException exception) {
exception.printStackTrace();
}
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
最后,在创建捆绑包的build.xml文件中,您必须添加文件关联(在本例中为扩展名为.zzz的文件):
这只适用于Java的最后一次更新(8u40):。对于以前的版本,您必须手动更改包中的info.plist,就像在中所说的那样。我在OS X上遇到同样的问题已经有很长一段时间了,而公认的答案对我来说不起作用。通过谷歌搜索,我终于找到了一个解决方案 总之,我需要使用
com.sun
API才能让它工作。我在下面提供了一些示例代码:
public class MyApp extends Application {
public MyApp() {
com.sun.glass.ui.Application glassApp = com.sun.glass.ui.Application.GetApplication();
glassApp.setEventHandler(new com.sun.glass.ui.Application.EventHandler() {
@Override
public void handleOpenFilesAction(com.sun.glass.ui.Application app, long time, String[] filenames) {
super.handleOpenFilesAction(app, time, filenames);
// Do what ever you need to open your files here
}
});
}
// Standard JavaFX initialisation follows
}
在实现适当的API来处理文件打开之前,这应该被视为临时解决方案。希望这个答案能帮助那些无法得到公认答案的人工作
注意:Java不是我经常使用的语言(我的应用程序使用Scala和ScalaFX),因此我为代码中的任何语法错误道歉。getParameters().getRaw()给出了什么?很抱歉,我实际使用了该语言,我将编辑我的问题,您可能想用它来解决这些问题。近5年后,这仍然是一条路要走。这还不够!将
com.apple.eawt
类替换为java.awt.desktop
等效类。
<fx:info title="Sample" vendor="me">
<fx:association extension="zzz" description="Sample Source"/>
</fx:info>
public class MyApp extends Application {
public MyApp() {
com.sun.glass.ui.Application glassApp = com.sun.glass.ui.Application.GetApplication();
glassApp.setEventHandler(new com.sun.glass.ui.Application.EventHandler() {
@Override
public void handleOpenFilesAction(com.sun.glass.ui.Application app, long time, String[] filenames) {
super.handleOpenFilesAction(app, time, filenames);
// Do what ever you need to open your files here
}
});
}
// Standard JavaFX initialisation follows
}