Twinscode
Twinscode

Reputation: 455

File association using my executable jar file

I have assigned an extension name for my files called *.XSCA. I would like to read the content of this file when the user click on it using my executable jar file to open it. How can I do that?

Upvotes: 1

Views: 1302

Answers (1)

Thomas Uhrig
Thomas Uhrig

Reputation: 31603

assuming you work under windows:

  • create a batch-file (*.bat) that calls your jar (it should just be something like "java -jar yourJar.jar")
  • click right on a file of your type, go to "Properties" and change the program that opens your file to that batch-file
  • let the batch-file pass the file-location as an argument to your jar

You can write a batch-file with any text-editor. Use google to find out how. It will just be a wrapper for your jar that you can associate in windows with your file-extension-type.

EDIT:

OK, step by step:

That's your Java program (export it with Eclipse as a runnable jar):

public class ExtensionOpener {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("args:");

        for(String arg: args) 
            System.out.println(arg);

        Thread.sleep(3000);
    }
}

That's the batch-file (just save it as e.g. your-starter.bat):

@echo run bat
java -jar C:\Users\Thomas\Desktop\FileOpener.jar -file -%1

Now click right on your file (your.fileextension) and go to properties and associate it with the batch-file. That's it.

Upvotes: 1

Related Questions