Okay so I am trying to execute a class inside my jar
How would I go about making it run the class inside the jar? If that's even possible.
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("java -Dfile.encoding=Cp1252 -classpath MiddleEarth728/src/bin;MiddleEarth728/src/lib/libs.jar;MiddleEarth728/src/lib/graphics.jar; MiddleEarth728.src.Loader");
dispose();
} catch (Exception err) {
err.printStackTrace();
}
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("Java -Xmx500m client 0 0 highmem members 0");
dispose();
} catch (Exception err) {
err.printStackTrace();
}
}
});
Why not including the jar in your class path and just call the class??
Anyway if you really needs to load a jar during the runtime, you have to use a class loader :
URL[] urls = { new URL("jar:file:" + path + "!/") };
cl = URLClassLoader.newInstance(urls);
cl.loadClass(className);
You don't want to 'run a class', you want to execute the main method of that class. You can do so using reflection.
URL[] urls = { new URL("jar:file:" + path + "!/") };
cl = URLClassLoader.newInstance(urls);
Class clazz = cl.loadClass(className);
Method mainMethod = clazz.getDeclaredMethod("main", String[].class);
mainMethod.invoke(null, new String[]{}); //can add args here if needed
The only advantage of spawning a different JVM would be security.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.