accessing files in a jar (I do not undrestand any more!!!)
Dear All,
Some days ago I have posted a thread asking for a solution to have access to my images which are located in different folders in the same level of my .class files, I followed the instruction and my problem has been solved! This time I ma trying to open files by :
completeFileName = createFilePath(folderName, fileNo,lesionExtention);
...
...
FileInputStream flotCommunicationIn =new FileInputStream(completeFileName);
ObjectInputStream flotTraitementIn =new ObjectInputStream(flotCommunicationIn);
Lesion aLesion = (Lesion) flotTraitementIn.readObject();
the code for createFilePath() is:
privatestatic String createFilePath(String folderName,String fileName, String extention){
String filePath =null;
String fileLocation =null;
if(extention ==null)
extention ="";
if(fileName ==null || fileName ==""){
fileName ="";
extention ="";
fileLocation = folderName;
}else{
fileLocation = folderName +"/"+ fileName + extention;
}
URL url = DiaphanoscopeExercicesUI.class.getResource(fileLocation);
//System.out.println(url);
filePath = url.getFile();
//filePath = url.toString();
if (filePath ==null)
System.err.println("The file: " + fileLocation +" does not exist !");
System.out.println(url);
return filePath;
}
when I run my programme from my IDE I have no problem but when I create the jar file and I run it from the cmd I receive the following error:
java.io.FileNotFoundException: file:\D:\TravailDiplome\Programmation\CodesSources_ProjetDiaphanoscope\DiaphanoscopieProject\dist\DiaphanoscopieProject.jar!\ch\h
eigvd\comem\tddiaphan\ui\exercices\easy.hmt (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at ch.heigvd.comem.tddiaphan.ui.DiaphanoscopeExercicesUI$ToolPanel$Exerc
iceRadioButtonListener.actionPerformed(DiaphanoscopeExercicesUI.java:2045)
....
the easy.hmt file is exactly here: ch\heigvd\comem\tddiaphan\ui\exercices\easy.hmt and my ".class" file is in ui folder...
I have the same problem with a .pdf file which is in:
ch\heigvd\comem\tddiaphan\ui\docs\theory.pdf
can any body help.. I do not undrestand any more!!! My head is just going to explose... :(
Please Help...
[3410 byte] By [
pishi13a] at [2007-10-3 10:32:02]

Look at your error message you are trying to load the file:
ch\heigvd\comem\tddiaphan\ui\exercices\easy.hmt
You are trying to load the file:
file:\D:\TravailDiplome\Programmation\CodesSources_ProjetDiaphanoscope\DiaphanoscopieProject\dist\DiaphanoscopieProject.jar!\ch\h
eigvd\comem\tddiaphan\ui\exercices\easy.hmt
I know exactly what is written in the error message but when but this file path is given by the MyClass.class.getResource(fileLocation);
If you look at the createFilePath method I have a:
System.out.println(filePath); this prints the path of my file.. when I run the .jar it prints:
jar:file:/D:/TravailDiplome/Programmation/CodesSources_ProjetDiaphanoscope/DiaphanoscopieProject/dist/DiaphanoscopieProject.jar!/ch/heigvd/comem/tddiaphan/ui/exercices/easy.hmt
I think the first part (D:/TravailDiplome/Programmation/CodesSources_ProjetDiaphanoscope/Diaph
anoscopieProject/dist/DiaphanoscopieProject.jar) is the location of my jar file and the second is the location of my file in the jar file.. I think the problem comes from the "/" before D and I do not know how can I take it off!
If that is not the problem then I do not know what is!!!!
So now can anyone help :(?
I think your problem is that you are trying to get the full file name. Don't do that. Just use the URL you create to access the file, don't return the filename, return the URL. And then access the file with that.
When I return the string format of the URL the FileInputStream() do not find the URL so thats why I am trying to return the file! I am just getting mad! I can not undrestand why I can have these files when I run the programme from my IDE (netBeans) and why I can not have access to these files when I execute the .jar file which has been created by the same IDE :(
> When I return the string format of the URL the
> FileInputStream() do not find the URL so thats why I
> am trying to return the file! I am just getting mad!
> I can not undrestand why I can have these files when
> I run the programme from my IDE (netBeans) and why I
> can not have access to these files when I execute the
> .jar file which has been created by the same IDE :(
Accessing a file in a jar is different then accessing it outside of a jar.
Like I said don't return the "complete File path". This will not help you. Use the URL.
I am really sorry to bother you...
I have tried your solution and in my code instead of filePath I am sending the url
But as I said before, the FileInputStream(URL) does not exist. If I get the toString or getFile of the url, then its the same problem!
I undrestand that the way a jar reads a file and outside of it is different but its a bit strange if I have to programme everything with a method and to change the method when I want to create my jar!!
Anyway I am ought to use the FileInputStream as I am trying to read a saved object and in an other place I am using :
Desktop.open(new File(createFilePath("docs","theory", ".pdf")));
by using JDIC packages to open a pdf file on any platform... this open method accept only File objects. In my place what would u do!!!
once again I am sorry to bother u this much :(
you make me laugh.. as I said neither File(url) nor FileInputStream(url) constructor exist!!
> you make me laugh.. as I said neither File(url) nor> FileInputStream(url) constructor exist!!Sorry, I am thinking uri.Anyway, get the resource as a stream not as a file then.
Oh my god.. I guess I am running out of memory :)
Do u have any example?
You mean for my FileInputStream(String) I should send a string of a stream?
I saw an example from getSourceToStream() and creating a StringBuffer from this stream but is it the real solution!!?
I am just wondering...
in the name of my package ch\heigvd\comem\tddiaphan maybe the \t is making all these problems!! if this is the case, it means that I should change the name of the whole package?!!!
[url http://forum.java.sun.com/thread.jspa?threadID=788381&tstart=0]Continued here.[/url]
The length of the file name is not an issue.Look at this for an example: http://forum.java.sun.com/thread.jspa?forumID=31&threadID=654173
I have changes my codes to:
InputStream input = DiaphanoscopeExercicesUI.class.getResourceAsStream("exercices/easy.hmt");
ObjectInputStream flotTraitementIn = new ObjectInputStream(input);
Lesion aLesion = (Lesion) flotTraitementIn.readObject();
...
...
does not help.. got the same error :(
java.io.FileNotFoundException: D:\TravailDiplome\Programmation\CodesSources_Proj
etDiaphanoscope\DiaphanoscopieProject\dist\DiaphanoscopieProject.jar!\ch\heigvd\
comem\tddiaphan\ui\exercices\middle.hmt (The system cannot find the path specifi
ed)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
I do not know whats going on..
I have just created another principal programme:
and tried this:
try {
InputStream input = LesionMakerPrincipalProgramme.class.getResourceAsStream("ch/heigvd/comem/tddiaphan/ui/exercices/easy.hmt");
ObjectInputStream flotTraitementIn = new ObjectInputStream(input);
Lesion aLesion = (Lesion) flotTraitementIn.readObject();
flotTraitementIn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
I made a jar from it and it there was no error in cmd so I guess it works there but do not undrestand whats happening!!!
For those who become after me here.. thats what I did and thats works fine:
private InputStream createInputStream(String folderName,String fileName, String extention) {
String filePath = null;
String fileLocation = null;
if(extention == null)
extention = "";
if(fileName == null || fileName == ""){
fileName = "";
extention ="";
fileLocation = folderName;
} else {
fileLocation = folderName + "/"+ fileName + extention;
}
//this will return the inputstream of your file
return getClass().getClassLoader().getResourceAsStream(fileLocation);
}
then I used this method to open my file...
fileName = "myFileName";
folderName = "resources"; //this folder should be in the same route as the package of your classes
extention = ".hmt";
fileInputStream = createInputStream(folderName, fileName,lesionExtention);
try {
if (fileInputStream != null) {
ObjectInputStream flotTraitementIn = new ObjectInputStream(fileInputStream);
lotTraitementIn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
I made this folder in
ProjectFolder
|_myClassPackage
|_resources
in other place it did not fonction...
Good luck
