3

In my application I have to read a jar file available in the disk location and segregate .classes file from the jar.

Code is

 JarFile jarFile = new JarFile("./resources/CD.jar"); for(Enumeration<JarEntry> em = jarFile.entries(); em.hasMoreElements();) { String s= em.nextElement().toString(); ZipEntry entry = jarFile.getEntry(s); String fileName = s.substring(s.lastIndexOf("/")+1, s.length()); if(fileName.endsWith(".class")){ System.out.println(fileName); } 

But I am getting an error

java.io.FileNotFoundException: .\resources\CD.jar (The system cannot find the path specified). 
3
  • What do you mean by segregate? Do you mean that you want to list the file names inside .jar file only or you want to do something else? I can see that your code only displays the name Commented Jun 12, 2015 at 5:45
  • Try adding the full path to the file. Commented Jun 12, 2015 at 5:46
  • I want to list out the .classes files inside the jar. Commented Jun 12, 2015 at 6:03

3 Answers 3

1

I found a solution to my problem:

  1. Add the 'resource' location to the classpath
  2. Allow external service API to place cd.jar in resources location
  3. Using a child class loader add the cd.jar to the classpath code snippet is given below:

    URLClassLoader childLoader = new URLClassLoader(new URL[] { getURLOfTestclass() }, this.getClass().getClassLoader()); URL url = getClass().getClassLoader().getResource("CD.jar"); return url; 
Sign up to request clarification or add additional context in comments.

Comments

0

Did you try adding the CD.jar file in your classpath? I would try doing that once.

2 Comments

I don't think this would make any difference.
This jar file is not in my class path.External service placed that file in this location
0

Change ".\resources\CD.jar" to a fully qualified URI. Eg:C:\Users\Whoever\Documents\resources\CD.jar.

2 Comments

But then it wont work for general purpose, that is if I want to run my application in linux machine I have to change the code. Isn't it?
@ANP Precisely. A solution to that would be keeping cd.jar in the same folder as your application.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.