叫我用Java编写一些线路从资源在运行时运行jar文件JAR文件的mainclass如何加载从资源
首先我得到的罐子填满
// create FileInputStream object InputStream fileInputStream = TestCL.class.getResourceAsStream("test.jar");
创建字节数组
byte rawBytes[] = new byte[fileInputStream.available()];
为了读取在字节数组的文件的内容
fileInputStream.read(rawBytes);
如何从这个的FileInputStream加载主类
这是我的测试,以从类呼叫的方法,如何我可以用jar文件做这个例子吗?
package testcl;
import java.io.InputStream;
public class TestCL extends ClassLoader {
public static void main(String args[]) throws Exception {
TestCL javaClassLoader = new TestCL();
javaClassLoader.load();
}
public void load() throws Exception {
// create FileInputStream object
InputStream fileInputStream = TestCL.class.getResourceAsStream("ClassLoaderInput.class");
byte rawBytes[] = new byte[fileInputStream.available()];
fileInputStream.read(rawBytes);
// Load the target class
Class<?> regeneratedClass = this.defineClass(rawBytes, 0, rawBytes.length);
// Getting a method from the loaded class and invoke it
regeneratedClass.getMethod("printString", null).invoke(regeneratedClass.newInstance(), null);
}
}
什么样的 “负担”?通过'ClassLoader'加载可用类或加载字节? (一罐子是一个zip文件,你可以使用普通的旧邮政编码寻找到它) – zapl
YS兄弟,有ClassLoader的但没有在硬盘上创建文件与网址或其他 – joseph
称之为看一看https://开头的文档。 oracle.com/javase/tutorial/deployment/jar/jarclassloader.html - Url可能是TestCL.class.getResource(“test.jar”)',不确定。也可以说,你必须在'jar:' – zapl