0

I want to access a file inside a jar and unmarshall it. But I cannot access it with the path as we do with the files. What is the best way to do it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
kiri
  • 131
  • 8

2 Answers2

0

I assume that the file you want to access is inside the resources folder. When you generate a jar the path changes, maybe this post would help you: Access file in jar file?

  • it is a maven dependency jar. It is located in the local maven repo. I have the path. I want to access the property file inside that jar. I want to do this for all projects in the workspace.Like Iterating through the dependencies and read properties for each. So i think that won't work. – kiri Nov 09 '20 at 03:33
  • @kiri : have you tried it? It _does work_ – Jayan Nov 09 '20 at 03:40
  • In my case, i am accessing the project from outside. I am not inside the project. I am doing eclipse plugin development and i am iterating through each project as IProject and get their ClassPathEntries. From that i can get the location of it. (my interested one is in the maven repo ). I tried to do as follows but didn't work. `for (IClasspathEntry classpathEntry: classPathEntries) { InputStream is = classpathEntry.getClass().getClassLoader().getResourceAsStream("META-INF/Packages.xml");}` – kiri Nov 09 '20 at 03:47
  • This post has something similar https://stackoverflow.com/questions/16570523/getresourceasstream-returns-null – Erick Valencia Nov 09 '20 at 04:27
0

I think we can do something like this.

JarFile jarFile = new JarFile("the/path/of/jar");
InputStream inputStream =null;
ZipEntry file = jarFile.getEntry("file/i/need");
if(file != null) {
    inputStream = jarFile.getInputStream(file);
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
kiri
  • 131
  • 8