I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse’s export to jar functionality, I have stored a directory.To access the files the from that directory i am using
BufferdReader tempDir = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(myDirPath),"UTF-8"));
// Then i iterate on tempDir
String line;
ArrayList<File> tempDirList = new ArrayList<File>();
int c = 0;
try {
while((line = tempDir.readLine())!= null)
{
File f = new File(line);
tempDirList.add(f);
c++;
}
} catch (IOException e)
{
e.printStackTrace();
}
Now on itrating on tempDirList when i try to read the file i need file path from which i get file but I did not get file path.
So i want to know that how i get file path?
You cannot access the files in the JAR as
Fileobjects since in the web container they might not get unpacked (so there is no file). You can only access them via streams as you did.If you really need
Fileobjects (most of the times it’s quite easy to avoid that), copy the files into temporary files which you then can access.But as I said, using streams instead of file objects in the first place makes you more flexible.
If you really don’t know all the files in the directory at compile time you might be interested in this answer to list contents.