簡體   English   中英

如何使用classLoader從加載的類中調用方法?

[英]How to call a method from loaded class using classLoader?

這是我使用的代碼:

File urlclasspath = new File("C:/Users/ASUS/Desktop/semantics/semantics/bin");
            URL urlarray[] = new URL[1];
            urlarray[0] = urlclasspath.toURI().toURL();

            MyClassLoader mycl = new MyClassLoader(urlarray);

            Class myclass = mycl.loadClass("USAGE");

            Object obj = myclass.newInstance();

我正在加載的類是USAGE ,我想調用的方法是main(String [] args)

您不需要調用newInstance() 做這個:

Class<?> myclass = mycl.loadClass("USAGE"); // get the class
Method m = myclass.getMethod("main", String[].class); // get the method you want to call
String[] args = new String[0]; // the arguments. Change this if you want to pass different args
m.invoke(null, args);  // invoke the method

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM