December 20th, 2012, 06:23 PM
Opening files with a .jar (in Windows)
Hello, I wish to ask a simple question which is not exactly about programming. I have a small java application that works with text files in a way similar to the notepad. I want files with a certain extension (e.g ".tex") to be opened on double-click with such program, so the method I used was going to the corresponding registry key:
classes_root > tex_auto_file > shell > open > command
And typing the following as the default value:
java -jar "E:\Stuff\Program\dist\Program.jar" %1
That does work, but with two problems:
1) It opens a command line window every time the program is called, which stays open until the program is closed. Ugly.
2) When the program is called in this way, for some reason it fails to interpret the relative paths inside the code (e.g it looks for "images\icon.png" in "E:\images\icon.png" instead of the relative path of the resource). Almost drove me mad, thinking there was a problem with the code itself.
Are there better alternatives to do this? If not, how may I solve this problems?
Thanks in advance.
December 22nd, 2012, 07:02 PM
Re: Opening files with a .jar (in Windows)
Problem solved. For people having the same problem in the future, here are the solutions I found for each problem:
1) Java uses two different launchers, one for applications with GUI and one for those without. So, for applications with GUI, one must call:
javaw -jar "path" %1
java -jar "path" %1
That way it doesn't show the console.
2) Relative paths result to be dependant on the location from which the jar was called, not the one in which it actually is. In any case, a workaround is to get the current path of the jar file, using the following method:
*name of the current class*.class.getProtectionDomain().getCodeSource().getLocation().getPath();
And having that, one can use full paths to find resources in subfolders.
Tags for this Thread
Click Here to Expand Forum to Full Width