2012-03-26 2 views
1

외부 .jar 파일에서 클래스를 가져 오는 데 문제가 있습니다. 여기 외부 .jar를로드하는 방법은 this이지만, 실행할 때마다 ClassNotFoundException이 발생합니다. 아래 코드는 내가 사용하고있는 코드이며 here은 내가 테스트 할 .jar입니다. 누구든지이 문제를 해결하는 방법에 대해 알고 있다면 나에게 말해주십시오. 감사!Java : 외부 .jar에서 클래스 가져 오기

@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) 
public void loadJar() throws MalformedURLException { 
    URL[] classes = {new URL("jar:file://test.jar!/")}; 
    URLClassLoader child = new URLClassLoader (classes, this.getClass().getClassLoader()); 
    try { 
     Class classToLoad = Class.forName ("test.PackageTest", true, child); 
     Method method = classToLoad.getDeclaredMethod ("test"); 
     Object instance = classToLoad.newInstance(); 
     Object result = method.invoke(instance); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (SecurityException e) { 
     e.printStackTrace(); 
    } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
    } catch (InstantiationException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
} 
+0

이 부분은 무엇인가요? http://stackoverflow.com/questions/9880385/java-build-path –

답변

1

하면 로거 또는 콘솔에() myFile.getAbsolutePath를 전송하고 클래스 로더가 test.jar을 찾으려는 위치를 확인할 수있는 몇 가지 문

File myFile = new File("test.jar"); 
URL myJarFileURL = new URL("jar", "", "file:" + myFile.getAbsolutePath() + "!/"); 
URL[] classes = {myJarFileURL}; 

URL[] classes = {new URL("jar:file://test.jar!/")};을 깨는 시도하십시오. 그런 다음 test.jar이 그 위치에 있는지 확인하십시오.

+0

오우 그 덕분에 백만 달러를 받았습니다! – MrDrProfessorTyler

1

이 항아리없이 그것을 시도 : 및/URL에!

여기 내 코드입니다.

+0

작동하지 않습니다 : ( – MrDrProfessorTyler

+0

변경된 코드를 게시 할 수 있습니까? –