0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Java リフレクションでJAR実行

Posted at

JavaでJARリフレクションを実行した際の備忘録
サンプルソースのみ

  // JarFile のパス
	String strPathJar = "D:\\pleiades\\zz_orgBatch\\test\\testReflect.jar";
	// 実行するクラスとメソッド
	String strLoadClass = "src.batch.test.TestBatch";
	String strMethod = "startBatch";
	// 対象JARファイルを指定
	File jarFile = new File(strPathJar);
	URLClassLoader loader =
			URLClassLoader.newInstance(new URL[] {jarFile.toURI().toURL()});
	// クラスをロード
	Class<?> c = loader.loadClass(strLoadClass);
    //インスタンス化
    Object obj = c.getDeclaredConstructor().newInstance();
	// 実行するメソッドと引数を定義する
	Method m1 = c.getMethod(strMethod,String[].class);
    //引数をオブジェクト化する必要がある
	String[] strParam = {};
    Object[] objParam = new Object[] {strParam};
    // 実行
    m1.invoke(obj,objParam);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?