2012-09-12 53 views
3

我有以下CustomClassLoader的Java:添加類路徑中CustomClassLoader

/* 
* 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<?> 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)) > 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; 
    } 
} 

而且它的工作原理找出最班,我遇到了一個問題做。我有以下的Java類

public class IntegerComparator 
      implements Comparator<Integer> 
{ 
    public IntegerComparator(){} 

    public int compare(Integer a, Integer b) 
    { int aValue = a.intValue(); 
     int bValue = b.intValue(); 
     return (aValue - bValue); 
    } 
} 

這個類實現位於同一個項目,因爲這類的自定義比較,但這不是由類加載器和炸彈從加載類拿起了。它不會給出任何錯誤,但如果我添加以下行到類

import java.util.Comparator; 

CustomClassLoader的作品。這導致我相信我錯過了文件的類路徑或類似的東西。有人知道如何解決這個問題嗎?

編輯:這是調用類加載器

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); 
      } 
     } 

的代碼,我希望它遍歷到,如果有一個以上的文件

編輯加載多個文件: -verbose輸出

[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] 
+1

「炸彈爆炸」意味着什麼,到底是什麼?雖然很高興看到類加載器代碼,但使用它的代碼在哪裏? – parsifal

+0

當我嘗試加載類(調試時)它只是直接移出該方法,這很奇怪,因爲它不會給出錯誤消息,但如上所述,如果我添加了導入語句,它將加載類成功 – newSpringer

+0

添加了代碼調用CustomClassLoader – newSpringer

回答

1

如果添加什麼-verbose作爲參數給運行時的JVM。你會看到類加載細節。

+0

你知道如何在Netbeans中做到這一點,我知道如何在eclipse中做到這一點,但我不知道如何在Netbeans中做到這一點? – newSpringer

+0

嘗試以下操作:單擊項目 - >屬性 - >選擇運行 - >虛擬機選項是您的行 – jaksky

+0

+1爲-verbose:但我從未使用過-verbose之前,我已將輸出直接添加到從定製類加載器中移出,知道這個輸出是怎麼回事? – newSpringer