javafilebatch-filefile-association

Pass the path of an opened file to a batch file


If I link a batch file as a file association for a certain file extension, then I can use it to pass arguments to the main function of the Java program. How do I get the path of the opened file as argument to use it then in Java? I want to make it possible to directly open files created and used by my Java program.

MyFile.extension > Open with > MyBatch.bat

MyBat.bat

java -jar test.jar thisIsWhereThePathBelongs

test.jar main function

public static void main(String[] args) {
    System.out.println(args[0]); // thisIsWhereThePathBelongs
}

Thanks in advance.


Solution

  • In order for your batch (or .cmd) file to pass on the arguments it receives when called via Open with ... or as file extension association, you need to ensure the argument %1 is passed to Java:

    MyBat.bat

    @echo off
    java -jar your.jar your.className %1