我使用Java在jar文件中创建了一个记事本应用程序。
使用它,我创建了一个文本文件,并以文件扩展名.rtx保存它。
现在,我想在Windows或任何其他平台上右击file.rtx,并在弹出窗口中显示我的记事本应用程序。如果选择了该选项,我希望打开该文件以在我的记事本环境中显示其内容。
或者,双击该文件将在我的记事本环境中打开该文件。
发布于 2011-05-16 08:10:11
这并不是一个特别的Java问题。您需要将.rtx扩展与您的应用程序关联。
这通常是由用户在其操作系统首选项中完成的。根据操作系统(和应用程序类型),应用程序可能能够自行设置文件关联,但这取决于各种因素。我不确定是否可以使用一个普通的旧JAR。
有关特定于this answer的解决方案,请参阅Windows。
发布于 2011-05-16 08:15:25
这在很大程度上取决于你的目标操作系统。例如,如果您正在尝试使用Windwos,您可以查看这篇文章,其中描述了JDIC,并提供了示例代码(我已经包含在下面),这些代码应该可以帮助您做到这一点。
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/
创建文件关联:
import org.jdesktop.jdic.desktop.*;
import org.jdesktop.jdic.filetypes.*;
AssociationService serv = new AssociationService();
Association logassoc = new Association();
// Adds the .log type to the Association object.
logassoc.addFileExtension("LOG"); 
// Adds an Action to the Association object that will 
// open a .log file with Windows Notepad. 
logassoc.addAction(
  new org.jdesktop.jdic.filetypes.Action(
    "open",
    "C:\\WINDOWS\\system32\\NOTEPAD.EXE %1"));
try {
   // Adds the .log Association to the file types' table 
   // at the user level using an AssociationService object.
   serv.registerUserAssociation(logassoc);
} 
catch (java.lang.IllegalArgumentException e) {
   // This exception will be caught if the given Association is not valid 
   // to be added to the table of file types.
   System.err.println(e);
}
catch (AssociationAlreadyRegisteredException e) {
   // This exception will be caught if the Association already
   // exists in the table of file types.
   System.err.println(e);
}
catch (RegisterFailedException e) {
   // This exception will be caught if the Association was
   // unable to be added to the table of file types.
   System.err.println(e);
}删除关联:
import org.jdesktop.jdic.desktop.*;
import org.jdesktop.jdic.filetypes.*;
AssociationService serv = new AssociationService();
// This uses an AssociationService to search the table of file 
// types for the .log extension. If the .log file is found, 
// an Association object representing the .log file type 
// will be returned. Otherwise, null is returned. 
Association logassoc = serv.getFileExtensionAssociation("LOG");
try {
   // The AssociationService will remove the .log file type from 
   // the table of file types.
   serv.unregisterUserAssociation(logassoc);
} catch (java.lang.IllegalArgumentException e) {
   // This exception will be caught if the given Association is not valid 
   // to be removed from the table of file types.
   System.err.println(e);
} catch (AssociationNotRegisteredException e) {
   // This exception will be caught if the Association does not already  
   // exist in the table of file types.
   System.err.println(e);
} catch (RegisterFailedException e) {
   // This exception will be caughtif the association was unable to be 
   // removed from the table of file types.
   System.err.println(e);
}发布于 2011-05-16 08:28:51
启动应用程序。使用Java Web Start,并在JNLP启动文件中声明对.rtx文件类型的兴趣。当用户双击.rtx文件时,将使用参数-open path/to/the/file.rtx调用应用程序的main。
下面是我的demo. of the file services of the JNLP API,它声明了一个.zzz文件类型。
https://stackoverflow.com/questions/6012121
复制相似问题