一个jar中的类可以在不同的jar中使用一个类 [英] can a class in a jar use a class in a different jar

查看:221
本文介绍了一个jar中的类可以在不同的jar中使用一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢java,所以不知道我完全理解jar文件。



我想把一些常见的代码放在一个库jar中,然后我从应用程序中使用在不同的罐子里。我已经搜索过,但只想出人们说的是,然后在类文件中显示类的例子,这些类在jar中调用类,这与在不同jar中调用类的jar中的类不同。



我已经尝试这样做,并认为我有它的工作,直到我尝试在命令行上运行应用程序,此时我找到类没有找到错误。然而,代码在eclipse中工作,从我所看到的似乎是一个常见的问题,代码在开发中的ide中运行,但有时候有人试图在eclipse之外运行代码失败。



那么可以将一个构建为jar的应用程序调用/使用不同的jar文件中的类,如果是这样呢?



这是我试图测试问题



FoobarInterface.java

  org.myorg.mylibrary; 

public interface FoobarInterface
{
String echo(final String val);
}

Foobar.java

  package org.myorg.mylibrary; 

public class Foobar实现FoobarInterface
{

public String echo(final String val)
{
StringBuilder sb = new StringBuilder(回应:);
sb.append(val);
return(sb.toString());
}

}

库jar的pom.xml

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi = http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven -4.0.0.xsd> 
< modelVersion> 4.0.0< / modelVersion>
< groupId> org.myorg< / groupId>
< artifactId> mylibrary< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< name> mylibraryname< / name>

< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-jar-plugin< / artifactId>
< configuration>
< archive>
< index> true< / index>
< manifest>
< / manifest>
< / archive>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / project>

产生mylibrary-0.0.1-SNAPSHOT.jar



客户端代码是



Main.java

  package org.myorg.myapp; 

import org.myorg.mylibrary.Foobar;

public class Main
{

public Main(String [] args)
{
Foobar foobar = new Foobar();
StringBuilder sb = new StringBuilder(RESULT:);
sb.append(foobar.echo(args [0]));
System.out.println(sb.toString());
}

public static void main(String [] args)
{
主foo = new Main(args);
}

}

应用程序jar的pom.xml

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi = http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven -4.0.0.xsd> 
< modelVersion> 4.0.0< / modelVersion>
< groupId> org.myorg< / groupId>
< artifactId> myapp< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< name> myappname< / name>
<依赖关系>
<依赖关系>
< groupId> org.myorg< / groupId>
< artifactId> mylibrary< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< / dependency>
< / dependencies>

< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-jar-plugin< / artifactId>
< configuration>
< archive>
< index> true< / index>
< manifest>
< mainClass> org.myorg.myapp.Main< / mainClass>
< / manifest>
< manifestEntries>
< Class-Path>。< / Class-Path>
< / manifestEntries>
< / archive>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / project>

我也尝试过Class-Path元素的所有这些设置

 。 ./mylibrary-0.0.1-SNAPSHOT.jar mylibrary-0.0.1-SNAPSHOT.jar 

所以理论上说,我可以使用这个命令运行应用程序$ /

  java -jar myapp-0.0.1-SNAPSHOT.jar 

但我总是收到这个错误

 线程main中的异常java.lang.NoClassDefFoundError:org / myorg / mylibrary / Foobar 

无论我如何在应用程序pom.xml中指定Class-Path元素,无论我是否在命令行中添加classpath,例如

  java -classpath mylibrary-0.0.1-SNAPSHOT.jar -jar myapp-0.0.1-SNAPSHOT.jar 

是的,两个jar我都在同一个目录,我在linux上运行这个。



编辑:



JB Nizet请求运行命令的结果为 java -jar ,但不幸的是,我已经删除了测试代码,因为他让我过去了这个问题。但是,如果有什么好处,这里是来源的输出,首先为我提出问题,仍然表现出同样的例外,但现在正在使用他建议的第二个命令。

  java -jar FoobarGUI-0.0.1-SNAPSHOT.jar org.myorg.foobar.gui.Main 
线程JavaFX Application Threadjava.lang中的异常。 RuntimeException:java.lang.reflect.InvocationTargetException javafx.fxml.FXMLLoader
$ MethodHandler.invoke(FXMLLoader.java:1770)
javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1653 )
在com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
在com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblin gEvent(CompositeEventDispatcher.java:59)
在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl。 java:114)
在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
在javafx.event .Event.fireEvent(Event.java:198)
在javafx.scene.Node.fireEvent(Node.java:8390)
在javafx.scene.control.Button.fire(Button .java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)
在com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89)
在com.sun.javafx.event .CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
在com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
在com.sun.javafx.event.EventHandlerManager com.un.javafx.event.EventHandlerManager.dispatchBubblingEvent .java:59)
在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher。 java:58)
在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
在com.sun .javafx.event.EventUtil.fireEvent(EventUtil.java:54)
在javafx.event.Event.fireEvent(Ev ent.java:198)
javafx.scene.Scene $ MouseHandler.process(Scene.java:3758)
javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java:3486)
在javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
在javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2495)
在com.sun.javafx。 tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:350)
在com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:275)
at ja va.security.AccessController.doPrivileged(Native Method)
在com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 350(GlassViewEventHandler.java:385)
在com.sun.javafx。 tk.quantum.GlassViewEventHandler $$ Lambda $ 201 / 45156577.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun .javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
在com.sun.glass.ui.View.handleMouseEvent(View.java:555)
在com.sun.glass .ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda $ null $ 48(GtkApplication.java:139)
在com.sun.glass.ui.gtk.GtkApplication $$ Lambda $ 41 / 1364335809.run(未知来源)
在java.lang.Thread.run(Thread.java:745)
引起的:java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke (MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil。 invoke(MethodUtil.java:275)
在javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1767)
... 50更多
导致:java.lang.NoClassDefFoundError :org / myorg / foobar / processor / ProcessorException
在org.myorg.foobar.gui.Main.initCenterSection(Main.java:244)
在org.myorg.foobar.gui.Main.initApplicationScene( Main.java:195)
在org.myorg.foobar.gui.Main.actionLogin(Main.java:413)
在org.myorg.foobar.gui.controller.ControllerLogin.actionLogin(ControllerLogin。 java:97)
... 60更多
导致:java.lang.ClassNotFoundException:org.myorg.foobar.processor.ProcessorException
在java.net.URLClassLoader.findClass(URLClassLoader。 java:381)
在java.lang.ClassLoader.loadCl屁股(ClassLoader.java:424)
at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331)
在java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 64更多

来自爆炸应用程序jar的META-INF / MANIFEST.MF



 清单版本:1.0 
Archiver-Version:Plexus Archiver
内置:myorg
类路径:FoobarProcessor-0.0.1-SNAPSHOT.jar
创建者:Apache Maven 3.3.3
Build-Jdk:1.8.0_45
主类:org.myorg.foobar .gui.Main

这是处理器库jar的列表

  META-INF / 
META-INF / MANIFEST.MF
org /
org / myorg /
org / myorg / foobar /
org / myorg / foobar / processor /
org / myorg / foobar / processor / ProcessorException.class
org / myorg / foobar / processor / Processor.class
META-INF / maven /
META-INF / maven / org.myorg.foobar /
META-INF / maven / org.myorg.foobar / FoobarProcessor /
META-INF / maven / org.myorg.foobar / FoobarProcessor / pom.xml
META-INF / maven / org.myorg.foobar / FoobarProcessor / pom.properties
META-INF / INDEX.LIST

这是来自库jar的META-INF / MANIFEST.MF

 清单版本:1.0 
Archiver-Version:Plexus Archiver
内置:myorg
创建者:Apache Maven 3.3.3
Build-Jdk:1.8.0_45


解决方案

这应该被标记为我刚刚遇到的这个问题的重复



这是确切的问题和解决方案,我无法找到所有以前的搜索,直到我确切知道我正在搜索的内容,即 可执行文件JAR忽略了自己的Class-Path属性



所以,除了一个maven问题,通常的稀疏的maven文档,根本就没有一个java问题,这根本就没有解决问题。不幸的是,我复制的示例配置包括设置,默认情况下该设置被关闭。最后的解决方案,不要使用索引与maven插件。


I am new to java so not sure I fully understand jar files.

I want to put some common code in a library jar, which I then use from applications that are in different jars. I have searched on this but only come up with people saying yes and then showing examples of classes in class files calling classes in jars, which is not the same as classes in a jar calling classes in a different jar.

I have tried to do this and thought I had it working, until I tried to run the application on the command line, at which point I get class not found errors. Yet the code works in eclipse and from what I have seen that appears to be a common problem where code runs in the ide under development but the minute someone tries to run their code outside of eclipse it fails.

So is it possible for an application built as a jar to call/use classes in an different jar file and if so how?

This is what I tried to test the problem

FoobarInterface.java

package org.myorg.mylibrary;

public interface FoobarInterface
{
    String echo(final String val);
}

Foobar.java

package org.myorg.mylibrary;

public class Foobar implements FoobarInterface
{

    public String echo(final String val)
    {
        StringBuilder sb = new StringBuilder("echoed: ");
        sb.append(val);
        return(sb.toString());
    }

}

pom.xml for the library jar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.myorg</groupId>
  <artifactId>mylibrary</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mylibraryname</name>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <index>true</index>
            <manifest>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

which produces mylibrary-0.0.1-SNAPSHOT.jar

The client code is

Main.java

package org.myorg.myapp;

import org.myorg.mylibrary.Foobar;

public class Main
{

    public Main(String[] args)
    {
        Foobar foobar = new Foobar();
        StringBuilder sb = new StringBuilder("RESULT: ");
        sb.append(foobar.echo(args[0]));
        System.out.println(sb.toString());
    }

    public static void main(String[] args)
    {
        Main foo = new Main(args);
    }

}

pom.xml for the app jar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.myorg</groupId>
      <artifactId>myapp</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>myappname</name>
      <dependencies>
        <dependency>
            <groupId>org.myorg</groupId>
            <artifactId>mylibrary</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
      </dependencies>

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <archive>
                <index>true</index>
                <manifest>
                  <mainClass>org.myorg.myapp.Main</mainClass>
                </manifest>
                <manifestEntries>
                 <Class-Path>.</Class-Path>
                </manifestEntries>
              </archive>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

I have also tried all of these settings for the Class-Path element

. ./mylibrary-0.0.1-SNAPSHOT.jar mylibrary-0.0.1-SNAPSHOT.jar

So in theory after all that, I can run the application using this command

java -jar myapp-0.0.1-SNAPSHOT.jar

but I always get this error

Exception in thread "main" java.lang.NoClassDefFoundError: org/myorg/mylibrary/Foobar

no matter how I specify the Class-Path element in the app pom.xml and no matter whether I add classpath to the command line, e.g.

java -classpath mylibrary-0.0.1-SNAPSHOT.jar -jar myapp-0.0.1-SNAPSHOT.jar

and yes both jars iare in the same directory and I am running this on linux.

EDIT:

JB Nizet requested the results of running the command as java -jar but unfortunately I have deleted the test code since he got me past the problem. However if it is any good, here is the output from the source that first raised the problem for me and still exhibits the same exception but is also now working using the 2nd command he suggested.

java -jar FoobarGUI-0.0.1-SNAPSHOT.jar org.myorg.foobar.gui.Main
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8390)
        at javafx.scene.control.Button.fire(Button.java:185)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)                                        
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)                                             
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)                                        
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)                                                                   
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)                                                                       
        at javafx.event.Event.fireEvent(Event.java:198)                                                                                      
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)                                                                          
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)                                                                      
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)                                                                        
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)                                                                  
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)                        
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)                        
        at java.security.AccessController.doPrivileged(Native Method)                                                                        
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)                       
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$201/45156577.get(Unknown Source)                                          
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)                                            
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)                                  
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)                                                                             
        at com.sun.glass.ui.View.notifyMouse(View.java:927)                                                                                  
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)                                                                       
        at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)                                                       
        at com.sun.glass.ui.gtk.GtkApplication$$Lambda$41/1364335809.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
        ... 50 more
Caused by: java.lang.NoClassDefFoundError: org/myorg/foobar/processor/ProcessorException
        at org.myorg.foobar.gui.Main.initCenterSection(Main.java:244)
        at org.myorg.foobar.gui.Main.initApplicationScene(Main.java:195)
        at org.myorg.foobar.gui.Main.actionLogin(Main.java:413)
        at org.myorg.foobar.gui.controller.ControllerLogin.actionLogin(ControllerLogin.java:97)
        ... 60 more
Caused by: java.lang.ClassNotFoundException: org.myorg.foobar.processor.ProcessorException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 64 more

META-INF/MANIFEST.MF from the exploded application jar

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: myorg
Class-Path: FoobarProcessor-0.0.1-SNAPSHOT.jar
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_45
Main-Class: org.myorg.foobar.gui.Main

this is a listing of the Processor library jar

META-INF/
META-INF/MANIFEST.MF
org/
org/myorg/
org/myorg/foobar/
org/myorg/foobar/processor/
org/myorg/foobar/processor/ProcessorException.class
org/myorg/foobar/processor/Processor.class
META-INF/maven/
META-INF/maven/org.myorg.foobar/
META-INF/maven/org.myorg.foobar/FoobarProcessor/
META-INF/maven/org.myorg.foobar/FoobarProcessor/pom.xml
META-INF/maven/org.myorg.foobar/FoobarProcessor/pom.properties
META-INF/INDEX.LIST

and this is META-INF/MANIFEST.MF from the library jar

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: myorg
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_45

解决方案

This should be marked as a duplicate of this question I just came across

It is the exact problem and solution, which I could not find with all my previous searching until I knew exactly what I was searching for, i.e. Executable JAR ignores its own Class-Path attribute

So not a java problem at all but a maven problem and the usual sparse maven documentation sheds no light on the problem at all. Unfortunately the sample config I copied included the setting, which by default is turned off. Final solution, don't use indexing with the maven plugin.

这篇关于一个jar中的类可以在不同的jar中使用一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆