I am writing application, that create .mg files. These files contain just text about level as normal .txt files. I just want to know, how can I set to run these .mg files with my application as default.
Is there any way to do it in java?

- 85,076
- 16
- 154
- 332

- 11
- 1
-
3You can't. There'll be no way to make it work with any OS, any DE and any file explorer. – tilpner May 18 '14 at 18:09
-
1On windows, you need to run the appropriate regedit commands to add this type with a default application. – Peter Lawrey May 18 '14 at 18:10
1 Answers
U should do this with registry modification.
steps to know what key u should modify :
Extensions are associated in HKEY_CLASSES_ROOT key.
Follow these steps to create a your own association:
1.Open HKEY_CLASSES_ROOT and create a key for extension (if it doesn't exist)
2.Open that key and write into DefaultValue name of your "handler" - somthing like MyApp_ext
3.Close current key and create another key inside HKEY_CLASSES_ROOT with the name of your handler (MyApp_ext)
4.Open that key and write into DefaultValue description of this extension.
5.Create a key DefaultIcon inside the handler key.
6.Open that key and write into DefaultValue full path to the file containing an icon.
7.Close the current key and create \shell\Open\Command keys inside the handler key
8.Open that key and write into DefaultValue full path to your application including parameter.
8.If you face troubles creating the file extension association, open a regedit and take a look at the structure inside H**KEY_CLASSES_ROOT** key.
now to do above modification in java try modify below code to ur desire key (its a code for
modification in windows registry) :
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
public class WindowsRegistrySnippet {
public static void main(String[] args) {
// Read a string
String productName = Advapi32Util.registryGetStringValue(
WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName");
System.out.printf("Product Name: %s\n", productName);
// Read an int (& 0xFFFFFFFFL for large unsigned int)
int timeout = Advapi32Util.registryGetIntValue(
WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows", "ShutdownWarningDialogTimeout");
System.out.printf("Shutdown Warning Dialog Timeout: %d (%d as unsigned long)\n", timeout, timeout & 0xFFFFFFFFL);
// Create a key and write a string
Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "SOFTWARE\\StackOverflow");
Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER, "SOFTWARE\\StackOverflow", "url", "http://stackoverflow.com/a/6287763/277307");
// Delete a key
Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "SOFTWARE\\StackOverflow");
}
}

- 1,666
- 2
- 33
- 68