我有两个jar文件(例如,可以将它们称为Updater.jar和Code.jar)。 Updater.jar启动,其主要方法,然后将其与倍美法再次启动本身:将jar文件添加到仪表路径
package Update;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class InstructionLauncher {
private List<UpdateInstruction> instructions = new ArrayList<UpdateInstruction>();
private static InstructionLauncher instance;
private Process process;
public static InstructionLauncher initialise(){
if(instance !=null) return instance;
else return new InstructionLauncher();
}
public void registerPremain(UpdateInstruction inst){
instructions.add(inst);
}
public void launchNext(){
UpdateInstruction inst = instructions.get(0);
String cls = inst.getClassName() + "." + inst.getMethodName();
String[] args = new String[]{"java", "-javaagent", "JOSUpdater.jar", "-jar", inst.getClassName() + "." + inst.getMethodName()};
ProcessBuilder builder = new ProcessBuilder(args);
try {
exportResource(cls, cls);
} catch (Exception e) {
UpdateManager.revert();
}
try {
Process p = builder.start();
process = p;
} catch (IOException e) {
e.printStackTrace();
}
while(!process.isAlive())launchNext();
}
private InstructionLauncher(){
instance = this;
}
//From http://stackoverflow.com/questions/10308221/how-to-copy-file-inside-jar-to-outside-the-jar
private String exportResource(String resourceName, String clazz) throws Exception {
InputStream stream = null;
OutputStream resStreamOut = null;
String jarFolder;
try {
stream = Class.forName(clazz).getResourceAsStream(resourceName);//note that each/is a directory down in the "jar tree" been the jar the root of the tree
if(stream == null) {
throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
}
int readBytes;
byte[] buffer = new byte[4096];
jarFolder = new File(Class.forName(clazz).getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
resStreamOut = new FileOutputStream(jarFolder + resourceName);
while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
} catch (Exception ex) {
throw ex;
} finally {
stream.close();
resStreamOut.close();
}
return jarFolder + resourceName;
}
}
的倍美力的方法是这样的时刻:
package Update;
import java.lang.instrument.Instrumentation;
public class PremainLauncher {
public static void premain(String args, Instrumentation inst){
inst.addTransformer(new Transformer(), true);
System.out.println("Registered instruction for package: " + args);
}
}
什么我不知道,是如何将整个外部JAR(本例中为Code.jar)添加到仪器的路径中?
我知道Instrumentation.retransformClasses方法,但要使用该方法,我需要获取该jar中的所有类的List>,但我一直无法完成该方法。
可以说Code.jar有三个类文件:Main.class,writer.class和display.class。有没有办法获得他们每个类对象的列表,而不是他们的名字?
只需在类路径中添加所需的jar。 – ManoDestra
仅仅是通过修改java.library.path变量? – JD9999
我的意思是简单地将所需的jar添加到类路径,例如java -cp path/to/your.jar; path/to/other.jar com.example.app.MainApp。不知道这是你的意思吗?除此之外,如果您希望获得更大的灵活性,则可以通过URLClassLoader类在运行时动态添加JAR。 – ManoDestra