Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Add classpath the CustomClassLoader
    primarykey
    data
    text
    <p>I have the following CustomClassLoader</p> <pre><code> /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Java; /** * * @author noconnor */ import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedExceptionAction; import org.apache.commons.io.FilenameUtils; public class CustomClassLoader extends ClassLoader { private static String repoLocation = "C:/TempBINfolder/bin/"; public CustomClassLoader() { } public CustomClassLoader(ClassLoader parent) { super(parent); } @Override protected Class&lt;?&gt; findClass(final String name) throws ClassNotFoundException { AccessControlContext acc = AccessController.getContext(); try { return (Class) AccessController.doPrivileged( new PrivilegedExceptionAction() { public Object run() throws ClassNotFoundException { FileInputStream fi = null; try { String fileName = FilenameUtils.getBaseName(name); String path = FilenameUtils.getFullPath(name); setRepoLocation(path); fi = new FileInputStream(getRepoLocation() + fileName + ".class"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int read; while ((read = fi.read(buffer)) &gt; 0) { baos.write(buffer, 0, read); } byte[] classBytes = baos.toByteArray(); return defineClass(fileName, classBytes, 0, classBytes.length); } catch (Exception e) { throw new ClassNotFoundException(name); } } }, acc); } catch (java.security.PrivilegedActionException pae) { return super.findClass(name); } } public String getRepoLocation() { return repoLocation; } public void setRepoLocation(String repoLocation) { this.repoLocation = repoLocation; } } </code></pre> <p>And it works find out for most classes, I ran into a problem do. I have the following java class</p> <pre><code> public class IntegerComparator implements Comparator&lt;Integer&gt; { public IntegerComparator(){} public int compare(Integer a, Integer b) { int aValue = a.intValue(); int bValue = b.intValue(); return (aValue - bValue); } } </code></pre> <p>This class implements a custom Comparator which is located in the same project as this class but this is not picked up by the class loader and bombs out from loading the class. It does not give any errors but if i add the following line to the class</p> <pre><code>import java.util.Comparator; </code></pre> <p>The CustomClassLoader works. This leads me to believe that I am missing a classpath for the file or something like that. Does anybody know how to solve this?</p> <p><strong>EDIT:</strong> This is the code that is calling the class loader</p> <pre><code>Class stringClass = null; for (String file : classFiles) { ClassLoader cls = new CustomClassLoader(ClassLoader.getSystemClassLoader()); try { stringClass = cls.loadClass(file); tempList.add(stringClass); } catch (ClassNotFoundException ex) { Logger.getLogger(CompilerForm.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <p>I want it to loop through to load more than one file if there is more than one file</p> <p><strong>EDIT:</strong> -verbose output</p> <pre><code>[Loaded java.net.MalformedURLException from C:\Program Files\Java\jdk1.7.0_05\jre\lib\rt.jar] [Loaded Java.CustomClassLoader$1 from file:/C:/projects/Compiler/Compiler/build/classes/] [Loaded java.lang.ClassFormatError from C:\Program Files\Java\jdk1.7.0_05\jre\lib\rt.jar] [Loaded org.junit.runners.model.MultipleFailureException from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar] [Loaded org.junit.runner.notification.Failure from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar] [Loaded java.io.StringWriter from C:\Program Files\Java\jdk1.7.0_05\jre\lib\rt.jar] [Loaded org.junit.runner.notification.RunNotifier$4 from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar] [Loaded org.junit.runner.notification.RunNotifier$7 from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar] [Loaded org.junit.runner.notification.RunNotifier$2 from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload