Search code examples
javawindowsexefile-association

How can pass my java application (as .exe) the path of a document on startup?


I use JSmooth to turn my java application (jar) into an executable. It´s a simple text editor, like notepad. I want to use the "Open With" function of windows to open certain files with the exe. For this, I only need the path of each file. How do I do this?

I thought about using a java-property and calling it with System.getProperty("VariableName"), though I don´t know if this is possible. ${EXECUTABLEPATH} just gets me the location of the MyApp.exe.


Solution

  • For short properties may use Preferences.userNodeForPackage(MyExample.class); and methods: get, put, flush.
    https://docs.oracle.com/javase/8/docs/technotes/guides/preferences/index.html

    and/or to use properties file near your exe file for save paths https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

    For open with Windows may use this schema for example:

    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.OPEN)) {
             desktop.open(file);
        }
    }
    

    https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html

    For set associations see: https://www.thewindowsclub.com/change-file-associations-windows may be usefull Have the ability to set Java application as default file opener?