I am trying to implement Java application. Lets say i have a file "file.txt.r". Now i want to open that file in windows with my custom Java application. I set default program of "file.txt.r" to javaApp.jar. What kinda code must be writen in "javaApp.jar" to start "javaApp.jar" and execute code (lets say to read the content of "file.txt.r". Note that "file.txt.r" doesn't have to be text file).
Basically i want to start my java application with double click on different files what has ".r" extension
You will need to add a file association that will open any file with extension r with the following:
- java -jar <jarfile> "%1" for a command line application that's put into a JAR file
- java -classpath <mainClass> "%1" for a command line application that's stored as loose class files
- javaw -jar <jarfile> "%1" for a windowed application that's put into a JAR file
- javaw -classpath <mainClass> "%1" for a windowed application that's stored as loose class files
Replace <jarfile> and <mainClass> with your actual JAR file / main class name; you may need to add the full path to java / javaw
In your application, all you need to do is check the args argument to the main method. With the above file associations, the "%1" will pass the opened file as one argument to the JVM, which will then show up as args. The quotes are required to prevent the JVM receiving multiple arguments if the file contains a space; that's an OS limitation, not a Java limitation. Also, be sure to make sure that args.length > 0 before accessing args or you will get an ArrayIndexOutOfBoundsException if no file is given.
Tnx for the answer.
I also found another solution.... I wrote batch file (start JavaLauncher.jar %~f1). files ".r" start that batch file and batch file start "jar" file and pass file path as an argument.