2

So I'm making a java program that plays MP3 files, like windows media player. What i want is for my program to automatically play mp3 files when they are double clicked, say from the desktop (I know how to set the default program to my program and everything). So the specific question is how might my java program know or find out what file was opened when my program is run? Is there any sort of argument sent anywhere when I open an mp3 file using my program?

3 Answers3

3

This is operating-system-specific. However, on most operating systems, the path to the file will be passed as the first argument to your program. That is, the first element of the array passed to main.

Note: I'm not sure how well Windows handles opening files with Java programs - since you're actually opening the file with java.exe (or javaw.exe), and passing your program's path as well as the file's path to Java. If the default configuration doesn't work, you may need to tweak it with the registry editor.

user253751
  • 57,427
  • 7
  • 48
  • 90
1

When you write a runnable program in java you should declare a main method. It's signature is like this:

public static void main(String[] args){}

The parameter take whatever you send to it in front of your java command right after the name of class file or jar file(depends on how you run your program) It's like this:

java your_main_claas.class mp3_file_address

or

java -jar your_jar.jar mp3_file_address
A.v
  • 734
  • 5
  • 26
0

On windows systems, you need to bind your program with that specific extension (e.g. mp3) using some mechanism. This information is stored in Windows registry. This way, when a file with that extension is double-clicked, windows knows which program should be executed. At this point, your program will be invoked and the filename (along with complete path) will be passed to your main() as first argument.

I don't know how to bind your program with extension on Windows. Will update my answer as soon as I find some example for this.

Below are some SO questions regarding the same thing associate a custom file extension with my java program on windows and associating a custom file extension with java app in windows

Community
  • 1
  • 1
Aakash
  • 2,029
  • 14
  • 22