将jar动态添加到正在运行的java项目(主项目)

将jar动态添加到正在运行的java项目(主项目),java,gradle,axis2,executable-jar,gradle-eclipse,Java,Gradle,Axis2,Executable Jar,Gradle Eclipse,我需要将运行应用程序生成的jar添加到相同的应用程序类路径中 Flow—我已经使用java中的axis 2从WSDL生成了一组类文件(打包为jar)。现在我需要在同一个java项目上访问生成的jar中的.class文件 我使用过构建工具Gradle 这是我的代码,用于为生成的一组java文件构建jar buildscript { ext { springBootVersion = '1.5.2.RELEASE' } repositories {

我需要将运行应用程序生成的jar添加到相同的应用程序类路径中

Flow—我已经使用java中的axis 2从WSDL生成了一组类文件(打包为jar)。现在我需要在同一个java项目上访问生成的jar中的.class文件

我使用过构建工具Gradle 这是我的代码,用于为生成的一组java文件构建jar

buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {

sourceCompatibility = 1.8
bootRepackage {
    enabled = false
}
sourceSets {
    compile fileTree(dir:'C://Users//10Decoders//Downloads//axis2-1.7.4-bin//axis2-1.7.4//lib',include: '*.jar')
    preBuildSources
    main {
        java {
            srcDirs = ['../'+customProp]
              println "sourceSets -- > DONE ---"+customProp
        }
    }
}


task copypackage(type: Copy){
  println "COpy Jar to package - > "
  into  customProp
  from  customProp+'/src/'
}
jar {
  archiveName = customProp+'.jar'
    destinationDir = projectDir
    println "JAR BUILD---- > "
 }
}
task mydatafile(dependsOn:build){
    println "COpy Jar to claspath - > "+customProp
    doLast{

    copy{
  into  "../"
  from  customProp+'.jar'
    }
  }

}
build.dependsOn copypackage
另一个用于为当前java应用程序(具有主功能)构建jar的Gradle文件 Build.script 2

    buildscript {
        ext {
            springBootVersion = '1.5.2.RELEASE'
        }
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'org.springframework.boot'

    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8

    dependencies {
     compile 'org.springframework:spring-support:2.0.8'
      compile 'org.springframework:spring-aop:4.1.2.RELEASE'
      compile 'org.springframework:spring-beans:4.1.2.RELEASE'
      compile 'org.springframework:spring-context:4.1.2.RELEASE'
      compile 'org.springframework:spring-core:4.1.2.RELEASE'
      compile 'com.squareup.okio:okio:1.11.0'
      compile 'com.squareup.okhttp3:okhttp:3.6.0'
      compile 'org.json:json:20160810'
      compile 'mysql:mysql-connector-java:5.1.38'
      compile 'org.apache.commons:commons-dbcp2:2.0.1'
      compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.1.Final'
      compile group: 'log4j', name: 'log4j', version: '1.2.17'
      compile('org.springframework.boot:spring-boot-starter-jdbc')
        compile group: 'com.predic8', name: 'soa-model-core', version: '1.5.4'

        // https://mvnrepository.com/artifact/wsdl4j/wsdl4j
        compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'


        // https://mvnrepository.com/artifact/org.reflections/reflections
        compile group: 'org.reflections', name: 'reflections', version: '0.9.5-RC2'
    }
 jar {
      archiveName = 'batch.jar'
      destinationDir = projectDir
      manifest {
        attributes (
            'Main-Class': 'com.wsdl.migration.SpringWsdlFieldIdentification',
           'Class-Path': 'testAPP.jar  mydata/'+customProp+'.jar'
        )
      }
    }

    task copyLibs(type: Copy){
      println "buildDir- > "+buildDir

    }
    build.dependsOn copyLibs
这里customProp是一个变量,用于在运行gradle构建时动态获取jar名称。(gradle构建-p customProp=XXXXtask)


ButtoLe:我不能从动态生成的JAR访问类。所以问题变成了——如何在运行时将JAR文件添加到运行的应用程序?。谢谢。

你是否使用反射来尝试?你可以考虑OSGi或java 9的JigEW项目。TLDR您必须使用反射或使用设计用于在运行时解析类路径的层。你是否尝试过使用反射?你可能会考虑OSGi或java 9的拼图项目。TLDR您必须使用反射或使用设计用于在运行时解析类路径的层。