I have a project(project source) with .txt file which I want to access from other project (project caller). caller has dependecy over source. So caller should see source as .jar. Well, the question, I have to access to this .jar to obtenin the .txt file but I cannot. I have tried thinks like: getClass().getResourceAsStream("classpath:/cc.txt"); with InsputStream and getClass().getResource("cc.txt"); with URL object but I always got a null. All forums I ve read speaks about this way to access. How do I suposse to access to a .jar file to get the .txt file? thanks all!!
Asked
Active
Viewed 79 times
0
-
A text file is not supposed to be in a JAR I think... – MarioDS Apr 23 '12 at 10:12
-
Looks like closest duplicate. http://stackoverflow.com/questions/6963410/trying-to-get-a-properties-file-from-a-jar-file-in-another-jar-file – Phani Apr 23 '12 at 10:15
-
@colditz: If u have got ur answer please tick mark it.... – abhi Apr 23 '12 at 10:51
1 Answers
0
Extract the Contents of ZIP/JAR Files Programmatically. Suppose jarFile is the jar/zip file to be extracted. destDir is the path where it will be extracted:
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enum = jar.entries();
while (enum.hasMoreElements())
{
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enum.nextElement();
java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
if (file.isDirectory()) // if its a directory, create it
{
f.mkdir();
continue;
}
java.io.InputStream is = jar.getInputStream(file); // get the input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
while (is.available() > 0) // write contents of 'is' to 'fos'
{
fos.write(is.read());
}
fos.close();
is.close();
}
-
But, If I want to use getResource() or getResourceAsStream() from the getClass is not possible? – Colditz prez Apr 23 '12 at 12:00