Search code examples
javafile-type

Java user start program vs file start program


I'm making a program that will be run in Windows and to open a custom file type with it. I know how to associate the file type with the program, but is there a way to tell whether the program was opened by the user (blank screen when opened, no content visible) or whether the program was started by opening a file associated with the program (double click file and it start the program so the content in the file is visible)?


Solution

  • When you define your static void main(String[] args) method, the args is actually the command-line arguments to your program.

    If there are no arguments, it was opened by itself, but if there are any arguments (specifically, you are interested in just the first), then it was most likely started by double-clicking on a file associated with your program, and that first argument will then be the full path to the file.