将Java SE应用程序打包到可执行Jar中

时间:2017-01-12 18:11:55

标签: java maven cdi executable-jar weld-se

尝试从命令行运行JAVA SE可执行jar时出现以下异常:

Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: java.lang.Internand
    at org.jboss.weld.executor.AbstractExecutorServices.checkForExceptions(AbstractExec
    at org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions()
    at org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions()
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer.addClasses(ConcurrentBeanDeploye
    at org.jboss.weld.bootstrap.BeanDeployment.createClasses(BeanDeployment.java:203)
    at org.jboss.weld.bootstrap.WeldStartup.startInitialization(WeldStartup.java:374)
    at org.jboss.weld.bootstrap.WeldBootstrap.startInitialization(WeldBootstrap.java:76
    at org.jboss.weld.environment.se.Weld.initialize(Weld.java:556)
    at org.jboss.weld.environment.se.StartMain.go(StartMain.java:44)
    at org.jboss.weld.environment.se.StartMain.main(StartMain.java:53)
Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.InternalError: Enclo at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197) at com.google.common.cache.LocalCache.get(LocalCache.java:3932) at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3936) at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4806) at org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue(LoadingCacheUtils.java at org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue(LoadingCacheUtils. at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransforme at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransforme at org.jboss.weld.bootstrap.AnnotatedTypeLoader.loadAnnotatedType(AnnotatedTypeLoad at org.jboss.weld.bootstrap.AnnotatedTypeLoader.loadAnnotatedType(AnnotatedTypeLoad at org.jboss.weld.bootstrap.BeanDeployer.addClass(BeanDeployer.java:97) at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer. at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer. at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFac at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFac at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.InternalError: Enclosing method not found at java.lang.Class.getEnclosingMethod(Class.java:1065) at sun.reflect.generics.scope.ClassScope.computeEnclosingScope(ClassScope.java:50) at sun.reflect.generics.scope.AbstractScope.getEnclosingScope(AbstractScope.java:78 at sun.reflect.generics.scope.AbstractScope.lookup(AbstractScope.java:96) at sun.reflect.generics.scope.AbstractScope.lookup(AbstractScope.java:96) at sun.reflect.generics.scope.AbstractScope.lookup(AbstractScope.java:96) at sun.reflect.generics.scope.AbstractScope.lookup(AbstractScope.java:96) at sun.reflect.generics.factory.CoreReflectionFactory.findTypeVariable(CoreReflecti at sun.reflect.generics.visitor.Reifier.visitTypeVariableSignature(Reifier.java:165 at sun.reflect.generics.tree.TypeVariableSignature.accept(TypeVariableSignature.jav at sun.reflect.generics.repository.MethodRepository.getReturnType(MethodRepository. at java.lang.reflect.Method.getGenericReturnType(Method.java:255) at org.jboss.weld.annotated.slim.backed.BackedAnnotatedMethod.(BackedAnnotate at org.jboss.weld.annotated.slim.backed.BackedAnnotatedMethod.of(BackedAnnotatedMet at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods..java:195) at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods..java:188) at org.jboss.weld.util.LazyValueHolder.get(LazyValueHolder.java:35) at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$EagerlyInitializedLazyVdType.java:156) at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods.188) at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods.188) at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.(BackedAnnotatedT at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.of(BackedAnnotatedType. at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.lo at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.lo at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.j at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2315) at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2278) at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2193) ... 18 more

然而,我能够在eclipse中成功运行程序,但是一旦打包到jar中,我总是会遇到这个错误。

这是我的maven包插件:

     <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
 <!-- Build an executable JAR -->
    <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>org.jboss.weld.environment.se.StartMain</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    </plugins>

以下是我引导Weld的方式:

public  static void main(String[] args) {
    //Start CDI container
    WeldContainer weldContainer = WeldFactory.INSTANCE.getWeldContainer();

    try {
        MainThread mainExecutor = weldContainer.instance().select(MainThread.class).get(); 
        mainExecutor.run();
    } catch (Exception e) {
        LOG.error("Fatal error",e);
    }finally {
        //Shutdwon CDI container
        weldContainer.shutdown();   
    }

}

//  @Inject
//  MainThread mainExecutor;
//  
//    public void main(@Observes ContainerInitialized event){
//      try {
//          //MainThread mainExecutor = weldContainer.instance().select(MainThread.class).get(); 
//          mainExecutor.run();
//      } catch (Exception e) {
//          LOG.error("Fatal error",e);
//      }finally {
//          //Shutdwon CDI container
//          //weldContainer.shutdown(); 
//      }
//
//    }

(我已经尝试了注释和未注释的代码但仍然得到相同的错误)。 任何帮助都会很棒,我真的尝试过所有的事情。

1 个答案:

答案 0 :(得分:1)

用户Siliarus帮助了你,为他赢得了赞誉。

现在您遇到了另一个问题。应用程序立即关闭。
这并不奇怪 - 因为您没有使用ContainerInitialized事件而发生这种情况。如果您正在使用内置的org.jboss.weld.environment.se.StartMain类,那么您应该有一个观察ContainerInitialized事件的简单bean。

或者,您可以使用Main类,您上面粘贴的内容。要执行此操作,请指示maven-jar-plugin使用main方法将闪亮的类用于以下内容:

<mainClass>com.your.own.class.with.main.method</mainClass>

如果出现其他问题,我建议您查看Weld使用的工作示例:https://github.com/weld/core/tree/master/examples

希望它有所帮助。