0

I have created a music application in java swings and I want to set it as my default music player for all mp3 format file. Do any anyone have an idea how to do it? Reference screenshot

Just like in the screenshot all pdf files open on adobe reader. My application just works fine and I am running it with no problem. Currently, i open the files from my application but now I want that whenever I double-click on mp3 file it opens in my application.

NOTE: I have created an executable file but I need window should suggest my application open when I click on open with.

Kindle Q
  • 944
  • 2
  • 19
  • 28
sahib
  • 85
  • 1
  • 13
  • 3
    This isn't a Java question; it is about how to make Windows use a particular application as a handler for a given extension. http://windows.microsoft.com/en-gb/windows/change-default-programs#1TC=windows-7 – Andy Turner Mar 19 '16 at 11:32
  • thanx for the comment but i could not find any specific way to do it .it just says that i can do it but the question is how – sahib Mar 19 '16 at 11:37
  • @AndyTurner This **is** a java-question. The link you provided renders useless, with a bit of further reading. Jar-files aren't executables on their own and can't be selected as default-application. –  Mar 19 '16 at 11:52

2 Answers2

1

Pretty simple: .jar-Files aren't executable by themselves. The JVM is a runnable. A workaround would be to write a wrapper-application in for example C or C++ and compile it to an executable. Or use a wrapper-library like launch4j. You can find more info about those wrappers here: wrapper-libraries for java

If you absolutely want to edit the file-associations from within your java-app, you can use JDIC to do that task.

boolean editDefaultApplication(){
    AssociationService serv = new AssociationService();

    //try to remove old association
    Association logassoc = serv.getFileExtensionAssociation(".mp3");       

    try {
        serv.unregisterUserAssociation(logassoc);
    } catch (java.lang.IllegalArgumentException |  AssociationNotRegisteredException | RegisterFailedException e) {
         e.printStackTrace();
         return false;
    }

    //add your own application as default
    logassoc.addFileExtension(".mp3");
    logassoc.addAction(new org.jdesktop.jdic.filetypes.Action("open", "<path to wrapper executable>"));
    try {
        serv.registerUserAssociation(logassoc);
    } catch (java.lang.IllegalArgumentException |  AssociationNotRegisteredException | RegisterFailedException e) {
         e.printStackTrace();
         return false;
    }

    return true;
}
Community
  • 1
  • 1
  • i have made it executable but it should be something in coding for eg. when you click on html file and select open with it automatically suggest for browsers. – sahib Mar 19 '16 at 11:51
  • @sahib I remember (answer got deleted). You'll still need a wrapper-application, for the same reason I mentioned above. In fact you're making the whole problem a lot more complicated, since that'll make the entire program even more system-dependent. E.g. in windows you'll have to edit the registry, for linux and mac the whole process will work differently. –  Mar 19 '16 at 11:56
  • @sahib If it's actually a `.exe`-file (or the equivalent for your OS), I'd still suggest you simply use the open-with option and set the program as default, instead of setting the default-application from within your application. That'd be a **lot** more complex. If you absolutely want to include that feature, you'll need the JDIC-libarary (http://www.oracle.com/technetwork/articles/javase/jdic-assoc-136531.html). –  Mar 19 '16 at 12:04
-1

Shift + right click on an mp3 file, select "Open with". Now in the dialog that opens select your executable and check "Always open with"

With jar files you can execute the with java.exe passing as parameter java -jar yourjarname.jar

xmorera
  • 1,933
  • 3
  • 20
  • 35
  • jar-files aren't executables on their own and windows won't allow you to select a jar as default-program. –  Mar 19 '16 at 11:46
  • What about java -jar your.jar? – xmorera Mar 20 '16 at 19:23
  • Why a downvote if you can achieve what you want using java -jar to get it started? – xmorera Mar 20 '16 at 19:24
  • well, "java -jar" isn't an executable either, but a terminal-command. "Open with" only accepts `.exe`-files and a few other filetypes. The downvote is because you **can't** achieve it that way. Only option that would come close to an answer would be to write a batch-file containing the command you mentioned. –  Mar 20 '16 at 19:34
  • That's where you are wrong again, @Paul . It is "java.exe" so it does work. I've done it that way before and it works fine. Did you try? Because it does work for me. – xmorera Mar 22 '16 at 00:38
  • java.exe is an executable, but it won't open mp3-files on its own. For that purpose you'll need to specify the jar-file. But that'd require a full command-line, which is only achievable with a batch script. Just as I said. You can specify java as defaultprogram to open jar-files. But that's neither the point of the question nor useful since that's the default anyways –  Mar 22 '16 at 00:55
  • And what is wrong with a batch file? It can be set to start the program and pass the parameter. java -jar mymp3playing.jar file.mp3. So there you go, it is a working solution :) – xmorera Mar 27 '16 at 20:57