可以直接调用B.jar对应的包中的Main类的main方法,这是没有问题的。
但是这和exec方式调用是有区别的。
直接调用main方法是在当前进程,并且也是当前线程中调用的。两个jar是在同一个java运行环境中运行,共享内存。
而用exec调用是另开一个进程来调用的。两个jar是在不同的java运行环境中运行,不共享内存。建议采用的方式
在A.jar中开启一个线程,然后在线程中调用B.jar中的main方法,这样a.jar和b.jar两个在同一个运行环境中运行,但是基于不同的线程。等于将你现在的进程级改成了线程级,效率将大大提高。因为对于操作系统来说,启动进程的开销是非常大的。

解决方案 »

  1.   

    可以将B.jar加入到当前的classLoader中,然后用反射调用。
      

  2.   

    如何写呢?没怎么用过JAVA。
    xxx.com.project.B.Main main ?
    要package B么?
      

  3.   

    如何写呢?没怎么用过JAVA。
    xxx.com.project.B.Main main ?
    要package B么? /**
     * create the ClassLoader with specified jar file
     * 
     * @param directory
     *            the directory including the jar file
     * @param parent
     *            the parent classLoader
     * @return the ClassLoader with specified jar file
     * @throws Exception
     */
    public static ClassLoader createClassLoader(final File directory,
    final ClassLoader parent) throws Exception {
    // Construct the "class path" for this class loader
    Set<URL> set = new LinkedHashSet<URL>(); // Add packed directory JAR files
    if (directory != null) {
    if (!directory.isDirectory() || !directory.exists()
    || !directory.canRead())
    return parent;
    String filenames[] = directory.list();
    for (int j = 0; j < filenames.length; j++) {
    String filename = filenames[j].toLowerCase(Locale.ENGLISH);
    if (!filename.endsWith(".jar"))
    continue;
    File file = new File(directory, filenames[j]);
    if (log.isDebugEnabled())
    log.debug("  Including jar file " + file.getAbsolutePath());
    URL url = file.toURI().toURL();
    set.add(url);
    } } // Construct the class loader itself
    final URL[] array = set.toArray(new URL[set.size()]);
    final ClassLoader cl = AccessController
    .doPrivileged(new PrivilegedAction<URLClassLoader>() {
    @Override
    public URLClassLoader run() {
    if (parent == null)
    return new URLClassLoader(array);
    else
    return new URLClassLoader(array, parent);
    }
    }); modifyPropertiesField(cl);
    return cl;
    }     // 加入到当前线程的classLoader
          try {
            final ClassLoader cl = Thread.currentThread()
                .getContextClassLoader();
            this.classLoader = ClassLoaderFactory.createClassLoader(
                new File(baseDir + DEFRAGMENT_LIB_PATH), cl);
          } catch (Exception e) {
            logger.error("exception occurred while init the"
                + " defragment classloader", e);
          }   //调用
            final Class<?> clazz = cl.loadClass(CLASS_NAME_ENTRANCE);
          final Object[] args = { new String[] { PARA_DEFRAGMENY } };
          final Object o = clazz.newInstance();
         Reflections.invokeMethodByName(o, METHOD_DEFRAGMENT, args); /**
     * 
     * call the method, ignore the private and protected
     */
    public static Object invokeMethodByName(final Object obj,
    final String methodName, final Object[] args) {
    Method method = getAccessibleMethodByName(obj, methodName);
    if (method == null) {
    throw new IllegalArgumentException("Could not find method ["
    + methodName + "] on target [" + obj + "]");
    } try {
    return method.invoke(obj, args);
    } catch (Exception e) {
    throw convertReflectionExceptionToUnchecked(e);
    }
    }