• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set my Java application as the default program to open a file extension  RSS feed

 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers ,

I have created my Swing Java application, and i allow the user to save a file through the application with a special extension (*.amdli) on his computer. Now when the user double clicks that file in Windows/Linux, i want my program to be opened by default to load the data inside that file.

How can i do this in Windows / Linux ?

Thanks ,
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is so strange i did not find a solution in the Saloon !!
Anyway, i have found the Sun project JDIC and this article explains how to use it to do the file type association job.

Thanks
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope i could find an answer for this, this time.

After i have defined a new extension (*.amdli) successfully in the Registry, i created a file (*.amdli), but when i double click this file to be opened with my program, the splash screen appears but then it gives the error : "could not find the main class : com.classes.MainClass. Program will exit".

My program is a jar wrapped inside an exe file, and if I double click the program exe directly it opens normally. Is there a problem of using the file type association with that kind of exe program ?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javaw is a terrible tool that catches any exception the main method throws and displays it as "could not find the main class". What happens if you change the javaw to java in your default program? That will show a console that should let you see the exception.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob ... I have got something new here.
The problem is in the path of the (*.amdli) file. When i wrote the complete path of the file in the console, it worked fine, so both of those worked fine :

If the (*.amdli) file was in the same folder of the exe & i double click it, it opens fine.
So the only problem now if the (*.amdli) file was in another path other than the exe, when i double click it, then it shows the same error message.

Here is what i write in windows file type command to open that file :

It seems the "%1" passes only the file name, and not the file path, i have searched for a command to pass the full file path but i couldn't find it yet.

 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were right Rob ... When i tried to open a file outside the path of my program using java command in console, i got this exception :

My bundle file is located in "languages/english.properties" outside the exe, and here is the code i use to load it :

This code works fine if i directly double click the application to open it, but it causes that exception when i double click the file type association that i have mentioned (*.amdli) if that file was outside the path of the exe ... Any suggestion ?

 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The File does not exist because the starting path is probably different. You are using a ResourceBundle; why not treat languages as a proper resource? I believe the resource name will then be /languages/english if the languages folder is located in the root of your JAR file.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bundle file is outside the jar file.
Here is my hierarchy :
app_folder -> pdf.jar
app_folder -> pdf.exe (I don't use the jar, instead i use the exe file)
app_folder -> languages -> english.properties

I want to keep the packages like this to allow users to edit the bundle file.
The strange thing is if i double click (pdf.exe) directly it opens fine without the error.
And if i double click a (*.amdli) file that is in the same path of (pdf.exe) it opens fine too.

But if i double click a (*.amdli) file that is in another path it will cause the error !!
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is that the languages folder is located relative to the JAR file (or EXE file in this case), but Java does not provide any direct mechanism to retrieve the so-called application path.

You could do a search to find two tricks that do allow you to retrieve that path, but neither is 100% guaranteed to work. One uses the resource URL of the .class file, the other the protection domain. A quick example of the latter can be found here.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob ... This worked perfect
This file type association is getting me mad ... After fixing that problem, i've got a new strange Exception, also at the application startup :

I am using Substance for the Look & Feel ... People are saying that this Exception appears if some images are not found.
 
Vivek Sagar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Hesham,
you can create an association in Windows to open a file of a particular extension.
use assoc [.extension]=[application name] without the brackets to create an association between an extension and an application. Or you can use the Folder Options utility to create association between a file type and your application..
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hesham Gneady wrote:I am using Substance for the Look & Feel ... People are saying that this Exception appears if some images are not found.

These people are right. If you investigate the stack trace, you will see this line:
This tells me that Substance is trying to get a blank image. This method creates a new BufferedImage:
Combining this with the exception message "Width (-1) and height (-1) cannot be <= 0" this tells me that the getBlankImage method is passing -1 as both the width and the height for the new BufferedImage. -1 is also the width and height if you try to create an ImageIcon from something that isn't an image. I don't think that's a coincidence.

Check the source code of org.jvnet.substance.utils.SubstanceCoreUtilities.getBlankImage (line 883 and just before it) and see where it tries to get that "blank image" from.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First i have tried to use the default Look & Feel instead of Substance and things worked fine this time. The file is loaded successfully.
Now for Substance i've traced the code here and they're doing a strange thing in the getBlankImage() method, they're throwing an exception to test its result which causes that problem.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's only happening if the width or height is 100 or larger. In your case they are both -1. In this case it is inner class SubstanceMenuBar of SubstanceTitlePane; this uses the Image returned by frame.getIconImage(). Apparently this method returns a non-null Image but with a not-yet known width and height.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to overcome this ?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you set the frame's icon image? Because it seems the image you set there is already incorrect.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am setting all the images in my program the same way.
All images are located outside the jar file, in a folder called "images", so for example i set the frame icon like this :


Ok .... This is good, the same problem as you were saying. JVM looks for the logo image in the relative path, so i've changed the images' paths to be in the ClassPath ... And this worked fine
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!