Re: [Java-gnome-developer] Problems generating .jar
Brought to you by:
afcowie
From: Emmanuel R. <emm...@gm...> - 2006-09-19 18:13:08
|
Pablo Martín wrote: > Hi all, > > I'm developing an application under eclipse with an interface designed > on glade, when I generate the .jar file into workspace project, I can > run it without problems, but, when I generate the .jar file out of the > workspace and I run it, I obtain a FileNotFounException when program > try to obtain the .glade file: > > ... > glade = new LibGlade("./gui/project.glade"), this) > ... > > > > It looks like java can`t read the file location when it's into a jar > File, how should I define this file location to read it correctly? > Hi Pablo, If you want to include the project files into a Jar file, you must load the files through a class loader. The trick is to use a class which you know to be bundle into the jar, basically any one of the classes in your project. Then use the very same class loader that loaded your class to find the glade file, or any other resource in the jar. Here's what you can do in order to fix your problem: // Find the resource pointing to the glade file through the same class loader used for this project's code // NOTE the folder 'gui' was removed see below for a reason InputStream stream = this.getClass().getClassLoader().getResourceAsStream("project.glade"); // Load the glade file through a resource glade = new LibGlade(stream, this, null); Now, if you look carefully the path of the resource doesn't include the name of the folder 'gui' anymore. I did this in order to have eclipse and the jar play nicely with each other. The problem is that if I want to run the code without any modifications from Eclipse I have to tell him where to find the project.glade file. This can be done by adding the gui folder into the classpath of the project. There are two things that you must do now: 1) Pack the file project.glade into the jar without the folder gui. 2) Add the folder gui to the classpath of Eclipse: Go into Eclipse Menu -> Project -> Properties -> Java Build Path -> Libraries -> Add Class Folder and choose 'gui' as a folder. Now you should be able to run the code both from Eclipse and a jar without any modification. Emmanuel Rodriguez |