2

I have created a notepad application which is in a jar file, using Java.

Using this I created a text file and saved it with the file extension .rtx.

Now I want to right-click file.rtx in Windows, or any other platform, and in the popup I want to show my notepad application. If it is selected I want to open that file to show its contents in my notepad environment.

Alternatively, double-clicking the file should result in opening the file in my notepad environment.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
Rizwan
  • 21
  • 1

3 Answers3

0

This isn't specifically a Java problem. You need to associate the .rtx extension with your app.

This is often done by the user, in their OS preferences. Depending on the OS (and the app type) an app may be able to set the file association itself, but this depends on a wide variety of factors. I'm not sure if it's possible to do at all with a plain old JAR.

See this answer for a Windows-specific solution.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

This depends a lot on the OS you're targeting. If you're trying to go for Windwos, for example, you might check out this article, which describes JDIC, and has sample code (which I've included below) that should help you do this.

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/

Create a file association:

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);

}

Remove an association:

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);

}
jefflunt
  • 33,527
  • 7
  • 88
  • 126
0

Launch the app. using Java Web Start, and declare an interest in the .rtx file type within the JNLP launch file. When the user double clicks a .rtx file, your application's main will be called with arguments -open path/to/the/file.rtx.

Here is my demo. of the file services of the JNLP API that declares a file type of .zzz.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433