3

Tough question here. How do you open a file in java, in the way that when you double click on the file it automatically opens in a java application.

I'm making a musicplayer (first real big Java project for me) and I have no clue how to acheive this. When you open, lets say, a .mp3 file it will open in whatever default program you have selected for it (such as VLC mediaplayer or Windows Media Player). What I want is to be able to set the .jar file of my application as the default program for .mp3 files, and then to be able to actually launch the files in the application.

When I currently try to open a file with the application I get a windows error saying "This app cannot be executed on your pc". But when I launch the .jar itself without doing it by trying to open a .mp3 file it runs just fine.

Does anyone know how to acheive what I want? Many thanks in advance!

---edit---

I do not mean that you can select a default program for the mp3 file. The problem is that windows throws the error shown above, and that I dont know how to handle the application being launched by opening a file (which does not ope due to the error).

3
  • right clock on any mp3 file. click properties and you should be able to choose the default program there. if the list looks limited, there should be a button the search for the program manually on your pc. from there, you can just find and pick your program as default. look into java ant. BTW, this is in the wrong section of stackexchange. probably should be in superuser or something
    – RisingSun
    May 5, 2016 at 22:39
  • 1
    I know that you can right click and select the default program. The problem is that windows does not want to launch the .jar when I try to laumch the jar by trying to open an mp3 file. Aside from that I dont know how to handle the launch with internal code. May 5, 2016 at 22:52
  • 1
    Distribute and install your program using Java Web Start. Web Start allows you to register your program with a file type.
    – VGR
    May 5, 2016 at 23:40

2 Answers 2

3

I think the problem is that you have to open a file with a .exe , so you sshould use an exe wrapper (I use jsmooth: download here)

BUT, before you do that, you need to accept that info. So in the main class, the "args" is a list of info about how it's being launched. If you are opening a file, the array's first argument will be the opened file's destination. SO I would accept it like this:

   if (args.length > 0) {
       File f = new File(args[0]);
       start_the_application_with_a_file(f);
   } else {
       start_the_application_without_a_file();
   }
3
  • That sounds like a very good way to do it. I knew I had to use Args, just not how ;) Im going to try this tomorrow, im off for bed now. Thanks for your help mate, i'll let you know if it works :) May 5, 2016 at 23:03
  • 1
    It worked dude! I used Launch4J as JSmooth did not work out for me very well, and Launch4j had more options to wrap the .jar. It works like a charm now. I would love to upvote you but I dont have 15 rep yet, so I can't. As soon as I have I will do so though. Thanks mate! May 6, 2016 at 7:38
  • Ok, Launch4J is a good wrapper too, I just use Jsmooth to wrap a .jar May 6, 2016 at 11:45
0

C:\ProgramData\Oracle\Java\javapath\java.exe -jar "C:\Program Files\YourApp.jar" %* within a batch-file (.cmd) might do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.