2

I want to know if it's possible to open a file in my program that is written in java just by doing a double click on the file?

For example: On my desktop is a file "test.dat" that was built by my program. If I try to open this file my program shows up and asks me what I want to do with that file.

Is it possible to implement that feature using java?

Markus
  • 1,649
  • 1
  • 22
  • 41
  • 2
    Depends on your IDE, but you want to create a jar file: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm – Lojko Apr 23 '13 at 07:52
  • 2
    I don't think that is what he means. He wants to save data with his own built program, and then later, read it in with his own program by double-click. – Eric Smekens Apr 23 '13 at 07:53
  • yes Eric, that's exactly what I want to implement – Markus Apr 23 '13 at 08:18
  • i guess you should rephrase the question. You want to have a programmatically method to set the OS preferencies for a file extension to be opened with your program eg file.myformat -> Windows/linux distro/MacOS methods for setting Myprogram as myformat file opener. Thus the way to do.. is this what you want? – LMG Sep 15 '13 at 20:14
  • yes, that's what I want to achieve – Markus Sep 17 '13 at 07:29

3 Answers3

4

It is the operating system that decides which applications are associated with a given extension. You may configure your OS to open all .dat files with you program if that works for you. Alternatively create a shortcut or a launcher telling what to use.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • 1
    okay thanks, but how to handle this inside my program? I mean if the user "opens" the file my program must be notified with a method like "openFile(String path)" or things like that. How to set which method should be called? – Markus Apr 23 '13 at 08:25
2

I understand what you are asking. You want to know how to get the parameter passed from the OS to your application and then call your open file method.

Your java program has a class that has a main method that is getting called to start your application. This class is listed in your manifest file as Main-Class: com.your.package.MainClass. The method signature looks something like:

public static void main(final String args[]) {

The String array args[] contains any parameters passed to your program from the command line. When you tell the OS to associate a file with an executable and you then double click on the file, the OS passes the filename (full path) to the executable as the first parameter in this String array. The tricky part is that you can't just associate a file extension with your jar file because the jar file isn't an executable. The jar file is actually associated with java.exe or javaw.exe. So to make this work you need to create a batch file (or a shell script depending on your OS) that calls java.exe or javaw.exe, sets the class path to your jar file, runs the main class and then passes the parameter to your program. This is how it would be done in a batch file on windows.

"C:\Program Files\Java\jre1.8.0_25\bin\javaw.exe" -cp C:\Path\To\Your\Jar\File.jar com.your.package.MainClass %1

Then, instead of associating your .dat file with your jar file, you would associate it with this batch file. The %1 will cause the filename to be passed to your MainClass as args[0] which you can then pass to your openFile(arg[0]) method and voila, the file is open. You aren't limited to just %1 either. You can have %1 %2 %3, etc. if the OS is passing multiple files to your program, for example if you have selected multiple .dat files. This would be done in a similar manner in a Unix shell script.

/usr/bin/javac -cp /Path/To/Your/Jar/File.jar com.your.package.MainClass %1
Lehrian
  • 347
  • 1
  • 2
  • 9
  • What if I have a jar wrapped as an exe? I tried opening a custom file with my program, however the args[] were only printed as null – Dahlin Jul 10 '22 at 11:23
  • If your exe was created with launch4j, which I am assuming it was, then a quick read of http://launch4j.sourceforge.net/docs.html seems to indicate that the command line is constant. I am not an expert on this, but based on reading the docs it doesn't seem possible to double click a .dat file associated with your .exe and have it passed as a command line parameter. – Lehrian Jul 11 '22 at 14:59
  • I fixed it, I discovered something weird. Since I use JavaFX there is a common problem that it will not start unless the main method is started from another classes main method. The args are passed to that method however they are null at the point where I need them, but not before, so I made an arraycopy and now it works fine :3 – Dahlin Jul 11 '22 at 15:42
0

"Opening" files by double clicking is a feature of windows OS that is controlled by mapping file extension to specific program.

If you want to run java program packed in jar file you have to create so called "runnable" jar and map jar extension to program named java or javaw.

AlexR
  • 114,158
  • 16
  • 130
  • 208