urlclassloaders の他のスレッドでいくつかの助けがあった後、urlclassloaderを理解し、ロードされた jar のクラスにアクセスする方法について、 私は問題に正しく取り組んでいるとは思わないので、質問を続けます。
myPackageA.start
urlclassloader の呼び出しには、次のコードで
フォームmyPackageB.comms
myPackageB.comms
への依存関係がありますimport org.jgroups.JChannel
/home/myJars/jgroups-3.4.2.Final.jar
package myPackageB;
import org.jgroups.JChannel;
public class SimpleChat {
JChannel channel;
String user_name=System.getProperty("user.name", "n/a");
private void start() throws Exception {
channel=new JChannel();
channel.connect("ChatCluster");
channel.getState(null, 10000);
channel.close();
}
public static void main(String[] args) throws Exception {
new SimpleChat().start();
}
}
通常、上記のコードを呼び出して、java -cp /home/myJars/jgroups-3.4.2.Final.jar:myPackageB myPackageB.SimpleChat
期待どおりに実行します。
私の質問は、スクリプト内で -cp を設定して、以下のコードを使用して呼び出すときにインポートが機能する方法ですmyPackageB.SimpleChat
。java -cp myPackageA.jar myPackageA.start
package myPackageA;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class start
{
Class<?> clazz;
private void start() throws Exception
{
if (this.clazz == null)
throw new Exception("The class was not loaded properly");
Object mySc = this.clazz.newInstance();
Method sC = this.clazz.getDeclaredMethod("main", String[].class);
String[] params = null;
sC.invoke(mySc, (Object) params);
}
public void loadSc() throws Exception
{
URL classUrl;
classUrl = new URL("file:///home/myJars/myPackageB.jar");
URL[] classUrls = { classUrl };
URLClassLoader ucl = new URLClassLoader(classUrls);
Class<?> c = ucl.loadClass("myPackageB.SimpleChat");
this.clazz = c;
}
public static void main(String[] args) throws Exception
{
start startnow = new start();
startnow.loadSc();
startnow.start();
}
}
ありがとう
アート