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?

5
  • 2
    Depends on your IDE, but you want to create a jar file: help.eclipse.org/juno/… Commented Apr 23, 2013 at 7: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. Commented Apr 23, 2013 at 7:53
  • yes Eric, that's exactly what I want to implement Commented Apr 23, 2013 at 8: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? Commented Sep 15, 2013 at 20:14
  • yes, that's what I want to achieve Commented Sep 17, 2013 at 7:29

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?
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

3 Comments

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
If your exe was created with launch4j, which I am assuming it was, then a quick read of 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.
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
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.

1 Comment

actually you can not be sure question is about windows.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.