2013-06-16 5 views
2

현재 활성 파일을 실행하는 Eclipse 플러그인을 개발 중입니다. 나는이 방법플러그인 설치시 동적 실행이 작동하지 않습니다.

public static void runIt(String fileToCompile,String packageName) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, SecurityException, NoSuchMethodException 
    { 

     File file = new File(fileToCompile); 

     try 
     { 
      // Convert File to a URL 
      URL url = file.toURL(); // file:/classes/demo 
      URL[] urls = new URL[] { url }; 

      // Create a new class loader with the directory 
      ClassLoader loader = new URLClassLoader(urls); 

      ClassLoader classLoader = Thread.currentThread() 
        .getContextClassLoader(); 

      Class<?> thisClass = classLoader.loadClass("NewFile"); 
      Object newClassAInstance = thisClass.newInstance(); 
      Class params[] = new Class[1]; 
      params[0]=String[].class; 
      Object paramsObj[] = {}; 
      String m=null; 
      Object instance = thisClass.newInstance(); 
      Method thisMethod = thisClass.getDeclaredMethod("main", params); 
      String methodParameter = "a quick brown fox"; 
      // run the testAdd() method on the instance: 
      System.out.println((String)thisMethod.invoke(instance,(Object)m)); 



     } 
     catch (MalformedURLException e) 
     { 
     } 

    } 

을 사용하고하지만 "시작 이클립스 응용 프로그램"[다른 이클립스 창에서 플러그인 실행]을 때 작동하지만 이클립스의 플러그인을 설치할 때 그것은 더 이상 작동하지 않습니다. 문제는이 줄에 있습니다.

클래스 thisClass = classLoader.loadClass ("NewFile"); 실행할 클래스를 찾을 수 없습니다.

+0

'packageName'은 무엇을 의미 않습니다

당신이 URLClassLoader (코드에서 포기 된 것으로 나타나는), 당신처럼, 그것을 부모를 줄 필요가 사용할 경우

? –

답변

0

플러그인으로 실행할 때 컨텍스트 클래스 로더가 다른 것으로 가정합니다. 귀하의 라인에서 얻은 클래스 로더 계층 구조를 살펴보십시오.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 

두 런타임 컨텍스트가 다른지 확인하십시오. 플러그인을 단계별로 디버깅 할 수 있다면 사용 가능한 객체를 둘러보고 동적 클래스에서 작동하는 클래스 로더를 찾을 수 있어야합니다.

new URLClassLoader(urls, this.getClass().getClassLoader())