Reputation: 37
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 owl files in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource("/folderName").getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, i changed the path like:
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
ArrayList<File> filesFromCommonOntology = new ArrayList<File>();
while(str = br.readLine() != null)
{
File f = new File(line);
System.out.println(f);
filesFromCommonOntology.add(f);
}
for (int i = 0; i < filesFromCommonOntology.size(); i++)
{
file = filesFromCommonOntology.get(i);
String filePath = file.getPath();
Model timeModel = FileManager.get().loadModel(filePath,
ApplicationConstants.N3);
}
But when i run the project i get null pointer exception for line
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
How to resolve this problem?
Upvotes: 0
Views: 88
Reputation: 21778
You cannot enumerate files in a folder when the application is packaged into jar. It is even difficult to imagine how this could work at all: you open a folder as an inptu stream and read the file names (in that folder) that way?
The simplest way would be to add a text file containing entries in the folder, probably with some descriptions. MyClass.class.getResourceAsStream should open files (not folders) from the jar.
If you have very huge number of files, you can also try to reopen own jar as ZipFile and then you can search for the entries in you folder using ZipFile API. However this is more complex.
Upvotes: 1