質問
んでいるこJava?したい場合においてどのようなモジュールシステムが必要で負荷瓶入ります。ってくれていると述べたが、その書面によるご自身の ClassLoader
, そのコミュニケーションも楽しみの仕事のうべきである私の心に少なくとも、簡単に呼びかけるとともに、手法のjarファイルとして、その引数です。
あらゆる提案のための簡単なコードなのか。
解決
その理由は難しいことは,セキュリティです。たclassloaderとして変更でき;なんかできるウイリー-nilly追加の授業で行います。私は実際にとても驚いている作品は、システムクラスローダ.こちらはかんがご自身の子供のクラスローダ:
URLClassLoader child = new URLClassLoader(
new URL[] {myJar.toURI().toURL()},
this.getClass().getClassLoader()
);
Class classToLoad = Class.forName("com.MyClass", true, child);
Method method = classToLoad.getDeclaredMethod("myMethod");
Object instance = classToLoad.newInstance();
Object result = method.invoke(instance);
痛みがあります。
他のヒント
以下のソリューションはhackishで反射バイパス封止、その正:
File file = ...
URL url = file.toURI().toURL();
URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, url);
するべき OSGi, など実施して Eclipseプ.になった。できるインストール、アンインストールが開始および停止というバンドルは、効果的にJARファイルです。ではもう少しでには、例えばできるサービスを動的に発見されたJARファイルで行います。
または、仕様の Javaモジュールシステム.
どのように JCLのクラスローダの枠組み?いんでも見える。
使用例:
JarClassLoader jcl = new JarClassLoader();
jcl.add("myjar.jar"); // Load jar file
jcl.add(new URL("http://myserver.com/myjar.jar")); // Load jar from a URL
jcl.add(new FileInputStream("myotherjar.jar")); // Load jar file from stream
jcl.add("myclassfolder/"); // Load class folder
jcl.add("myjarlib/"); // Recursively load all jar files in the folder/sub-folder(s)
JclObjectFactory factory = JclObjectFactory.getInstance();
// Create object of loaded class
Object obj = factory.create(jcl, "mypackage.MyClass");
こちらはバージョンが推奨されていません。私を変更したオリジナルを削除し推奨されていません可能です。
/**************************************************************************************************
* Copyright (c) 2004, Federal University of So Carlos *
* *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted *
* provided that the following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of *
* conditions and the following disclaimer. *
* * Redistributions in binary form must reproduce the above copyright notice, this list of *
* * conditions and the following disclaimer in the documentation and/or other materials *
* * provided with the distribution. *
* * Neither the name of the Federal University of So Carlos nor the names of its *
* * contributors may be used to endorse or promote products derived from this software *
* * without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR *
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
**************************************************************************************************/
/*
* Created on Oct 6, 2004
*/
package tools;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
/**
* Useful class for dynamically changing the classpath, adding classes during runtime.
*/
public class ClasspathHacker {
/**
* Parameters of the method to add an URL to the System classes.
*/
private static final Class<?>[] parameters = new Class[]{URL.class};
/**
* Adds a file to the classpath.
* @param s a String pointing to the file
* @throws IOException
*/
public static void addFile(String s) throws IOException {
File f = new File(s);
addFile(f);
}
/**
* Adds a file to the classpath
* @param f the file to be added
* @throws IOException
*/
public static void addFile(File f) throws IOException {
addURL(f.toURI().toURL());
}
/**
* Adds the content pointed by the URL to the classpath.
* @param u the URL pointing to the content to be added
* @throws IOException
*/
public static void addURL(URL u) throws IOException {
URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Class<?> sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL",parameters);
method.setAccessible(true);
method.invoke(sysloader,new Object[]{ u });
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}
}
public static void main(String args[]) throws IOException, SecurityException, ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
addFile("C:\\dynamicloading.jar");
Constructor<?> cs = ClassLoader.getSystemClassLoader().loadClass("test.DymamicLoadingTest").getConstructor(String.class);
DymamicLoadingTest instance = (DymamicLoadingTest)cs.newInstance();
instance.test();
}
}
と Java9, に答える URLClassLoader
まエラーのように:
java.lang.ClassCastException: java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLClassLoader
このクラスローダを使用しています。代わりに、追加のシステムクラスローダを使用できます 計装 APIによります。
を代理クラス:
package ClassPathAgent;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;
public class ClassPathAgent {
public static void agentmain(String args, Instrumentation instrumentation) throws IOException {
instrumentation.appendToSystemClassLoaderSearch(new JarFile(args));
}
}
追加のMETA-INF/MANIFEST.MFに入れてJARファイルのクラス:
Manifest-Version: 1.0
Agent-Class: ClassPathAgent.ClassPathAgent
実行エージェント
この バイト-バディ-エージェント 図書館を追加エージェントのJVM:
import java.io.File;
import net.bytebuddy.agent.ByteBuddyAgent;
public class ClassPathUtil {
private static File AGENT_JAR = new File("/path/to/agent.jar");
public static void addJarToClassPath(File jarFile) {
ByteBuddyAgent.attach(AGENT_JAR, String.valueOf(ProcessHandle.current().pid()), jarFile.getPath());
}
}
エクスペディアのキャンセがその場で発音を確認することがで org.apache.xbean.classloader.JarFileClassLoader の一部である XBean プロジェクト.
こちらは短期の方法を使い、クラスローダを作成するからすべてのlibファイルを特定のディレクトリ
public void initialize(String libDir) throws Exception {
File dependencyDirectory = new File(libDir);
File[] files = dependencyDirectory.listFiles();
ArrayList<URL> urls = new ArrayList<URL>();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().endsWith(".jar")) {
urls.add(files[i].toURL());
//urls.add(files[i].toURI().toURL());
}
}
classLoader = new JarFileClassLoader("Scheduler CL" + System.currentTimeMillis(),
urls.toArray(new URL[urls.size()]),
GFClassLoader.class.getClassLoader());
}
その後のクラスローダは、い:
classLoader.loadClass(name);
で作業している場合は、Android、以下のコード:
String jarFile = "path/to/jarfile.jar";
DexClassLoader classLoader = new DexClassLoader(jarFile, "/data/data/" + context.getPackageName() + "/", null, getClass().getClassLoader());
Class<?> myClass = classLoader.loadClass("MyClass");
の溶液を提案するjodonnellはもう少し。この投稿を願いたしました。
割り当て、現在のスレッド
まずない追加
Thread.currentThread().setContextClassLoader(classLoader);
はできない荷重資源などspring/context.xml存のjar.
を含まない
お瓶の親クラスローダかまで把握することができない限りは負荷いてどのようにお考えですか。
しかし、OSGi frameworkで最も優れます。
の別バージョンhackish溶液からAllainるもJDK11:
File file = ...
URL url = file.toURI().toURL();
URLClassLoader sysLoader = new URLClassLoader(new URL[0]);
Method sysMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
sysMethod.setAccessible(true);
sysMethod.invoke(sysLoader, new Object[]{url});
にJDK11、これまでにない却の警告など入できるようにする解決策をご利用の方AllainションのJDK11.
別の作業を計測することができます。この変更を行うクラスローダの検索を避ける問題のクラスは視認性のための従属クラス
を代理クラス
この例では、同一のjarによって呼び出され、コマンドライン:
package agent;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;
public class Agent {
public static Instrumentation instrumentation;
public static void premain(String args, Instrumentation instrumentation) {
Agent.instrumentation = instrumentation;
}
public static void agentmain(String args, Instrumentation instrumentation) {
Agent.instrumentation = instrumentation;
}
public static void appendJarFile(JarFile file) throws IOException {
if (instrumentation != null) {
instrumentation.appendToSystemClassLoaderSearch(file);
}
}
}
変更のマニフェストの作成が行えます。MF
追加の参照を剤
Launcher-Agent-Class: agent.Agent
Agent-Class: agent.Agent
Premain-Class: agent.Agent
私は実際に使用Xsltproc、 このポスト をどのように変更しました。mf
走
の Launcher-Agent-Class
のみサポートされてJDK9+の責任者とし、荷重を剤なしで明示的に定義では、コマンドライン:
java -jar <your jar>
にすることがJDK6+を定義するには -javaagent
引数:
java -javaagent:<your jar> -jar <your jar>
新しJarを実行時に
きにそれがすべてのjar必要に応じて以下のコマンド:
Agent.appendJarFile(new JarFile(<your file>));
ったものは見出せなかった問題が本書です。
こちらは迅速に対処Allainの方法で対応の新しいバージョンのJava:
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try {
Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, new File(jarPath).toURI().toURL());
} catch (NoSuchMethodException e) {
Method method = classLoader.getClass()
.getDeclaredMethod("appendToClassPathForInstrumentation", String.class);
method.setAccessible(true);
method.invoke(classLoader, jarPath);
}
りますのでご注意に依存しての知識の内部実装の特定のJVMではない理想的なユニバーサル溶液とする。でに"ポンヌッフ"とは回避策ばっかりにくい標準OpenJDKやOracle JVM.ものですが、これまでのところ休憩時点で将来場JVMバージョンが発売され必要になります。
このとき、対応の遅れかできないこと(単純な例fastutil-8.2.2.jar用jhplot.ウェブクラスからDataMelt(http://jwork.org/dmelt)
import jhplot.Web;
Web.load("http://central.maven.org/maven2/it/unimi/dsi/fastutil/8.2.2/fastutil-8.2.2.jar"); // now you can start using this library
によるドキュメンテーションまで、このファイルのダウンロード内の"lib/ユーザー"として動的にロード開始することができますすぐにからクラスを使用してこのjarファイルが同じプログラム。
ご覧いただけるこのプロジェクトを開始していますの: プロキシオブジェクトlib
このlib負荷のjarファイルからシステムやその他の場所です。ま々のクラスローダのjarくありません図書館です。ユーザーが作成できるのanyオブジェクトからロードされたjarを呼び出その他の方法です。このlibのための負荷のジャパJava8からのコードベースを支援するJava7.
をオブジェクト:
File libDir = new File("path/to/jar");
ProxyCallerInterface caller = ObjectBuilder.builder()
.setClassName("net.proxy.lib.test.LibClass")
.setArtifact(DirArtifact.builder()
.withClazz(ObjectBuilderTest.class)
.withVersionInfo(newVersionInfo(libDir))
.build())
.build();
String version = caller.call("getLibVersion").asString();
ObjectBuilder支援ファクトリメソッド呼び出し、静的機能、コンタフェースの実装.なりますの投稿とりどりのreadmeファイルページです。
私個人の見る java.util.ServiceLoader なのですね。き一例 こちらの.