File association using my executable jar file

只愿长相守 提交于 2019-12-11 06:32:32

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15579383/file-association-using-my-executable-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!