- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用以下方法调用 jar 文件中的类:
invokeClass("path.to.classfile", new String[] {});
public static void invokeClass(String name, String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, MalformedURLException {
File f = new File(System.getProperty("user.home") + File.separator + ".myapplication"+File.separator+"myjar.jar");
URLClassLoader u = new URLClassLoader(new URL[]{f.toURI().toURL()});
Class c = u.loadClass(name);
Method m = c.getMethod("main", new Class[] { args.getClass() });
m.setAccessible(true);
int mods = m.getModifiers();
if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
throw new NoSuchMethodException("main");
}
try {
m.invoke(null, new Object[] { args });
} catch (IllegalAccessException e) {
}
}
是否可以在单独的进程中调用它?那么正在运行的应用程序和新调用的应用程序没有任何共同点吗?
情况:您启动程序 a(客户端更新程序)。从客户端 a 启动程序 b(客户端)
使用当前代码,项目 a 和项目 b 的所有实例共享相同的堆空间。我正在尝试实现一种状态,其中项目 b 的所有实例都是独立的,并且不关心项目 A 是否终止。
最佳答案
是的,实际上这使您免于完全执行该反射过程
您需要使用 ProcessBuilder在单独的虚拟机中启动新进程。
类似于:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", f.getAbsolutePath());
Process p = pb.start();
编辑
- Will that work if the program that does pb.start() terminates?
- Will that work if the java environmental variable isn't set (e.g. Mac OS X?)[can't test on mac os x]
确实如此。看看这个视频:
http://img33.imageshack.us/img33/8380/capturadepantalla201001s.png
源代码(省略导入):
// MainApp.java
public class MainApp {
public static void main( String [] args ) throws IOException {
JFrame frame = new JFrame("MainApp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JLabel("<html><font size='48'>Main App Running</font><html>") );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
launchSeparateProcess();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ){
System.out.println("MainAppp finished");
}
});
}
private static void launchSeparateProcess() throws IOException {
File f = new File("./yourjar.jar");
ProcessBuilder pb = new ProcessBuilder("java", "-jar", f.getAbsolutePath() );
Process p = pb.start();
}
}
//-- Updater.jar
public class Updater {
public static void main( String [] args ) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JLabel("<html><font size='78'>Updating....</font></html>"));
frame.pack();
frame.setVisible(true);
}
}
//--manifest.mf
Main-Class: Updater
关于java - URLClassLoader + loadClass + 在独立进程上调用 main 方法? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2024893/
loadClass(String name) 之间有什么区别?和 loadClass(String name, boolean resolve) ? 我知道的唯一区别是如果 resolve 参数为真,
如何使用 loadClass 方法加载数组? String className = "Customer[]"; Thread.currentThread().getContextClassLoader
我已经编写了我想使用 ClassLoader 加载的小型 java 类。 public class ClassLoadingObj { public ClassLoadingObj(){
String pathToJar = "C:\\Users\\dtn\\jb_workspace\\helloworld-mdb\\target\\jboss-as-helloworld-mdb.wa
有谁知道: Class clazz = getClass().getClassLoader().loadClass(className); 和 Class clazz = Class.forName(
我想创建自定义类加载器。但是我对loadClass()感到困惑,这个方法是否将指定的类加载到内存中? 如果是那么为什么不调用指定类的静态 block ? //main class package cu
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadd00d #00 pc 0x50f8e /system/lib/libdvm
Mongoose 接受 ES6 class作为架构的基础。 该链接中的示例: class PersonClass { get fullName() { return `${this.fir
使用以下代码,我得到由 ClassNotFoundException 引起的 NoClassDefFoundError: File file = new File( "C:\\prototype.co
我已经检测了 JDK 和应用程序。记录入口点和导出点以用于构建调用图。 调用图看起来像 sun.misc.Launcher$AppClassLoader.loadClass->com.example.
我正在使用 apache felix osgi。当我使用 classLoader.loadClass(..) 加载类时,它给出了 ClassNotFoundException但是当我使用 Class.
我想以编程方式创建一个 OSGi 框架,用它加载一个 bundle 并从该 bundle 加载一个类。当我调用 Bundle.loadClass() 时,我得到一个 Class 实例,其中所有 fie
我正在利用 Mongoose class schemas . 并在我的 Node 项目中使用 TypeScript。 我关注了Mongoose the Typescript way...?确保我的模型
我在 android 9 设备上面临这个崩溃,但找不到任何解决方案。谁能帮我解决这个崩溃? 这是崩溃报告。 java.lang.Error: FATAL EXCEPTION [Thread-86] U
class testMe{ void show(){ System.out.println("Hello"); } } public class Classloader
我使用以下代码从外部包动态加载类: Object plugin = null; String packageName = "unipd.elia.deltapluginacceleromete
我在 Google Play 上有一个活跃的应用程序。我的用户经常遇到崩溃,我不知道在哪里调试。 java.lang.NoClassDefFoundError: at jl.b (jl.java:
我注意到我们可以随意设置一个线程的上下文类加载器。这是否意味着每个 new 都会导致上下文类加载器的 loadClass 被调用? 最佳答案 不,loadClass() 只会调用一次,即第一次访问该类
我要在现有的 C++ 应用程序中嵌入 JVM,并且需要使用类注册 native Java 函数的实现。 考虑这个带有原生函数的简单类: class Native { static { Sy
我正在尝试测试实用程序方法,该方法检查特定类是否在类路径上,如果是,则 return true 否则 return false。 我为什么这样做:我必须独立类扩展同一个类,并且只有其中一个在类路径上。
我是一名优秀的程序员,十分优秀!