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 csv file in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource(ApplicationConstants.ALIASESFILE).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, it gives a path as shown below
D:\javaProjects\AutomodeGS_Prachi\lib\internal\RESTWSGS.jar!\aliases\aliases.csv
I am getting a file notfound exception.What could be wrong?
The
getResource()method is returning a “jar:” URL. The path component of that URL is not a normal filesystem pathname, and can’t be opened directly using Java’s file classes.The simple way to do this is to use
Class.getResourceAsStream(...)to open the stream. If you need an “identifier” for the JAR entry, useClass.getResource(...), but then open the stream usingURL.openStream().