叫我用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