jar中的类可以使用不同jar中的类

时间:2015-06-12 13:29:11

标签: java eclipse maven jar

我是java的新手,所以不确定我是否完全理解jar文件。

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

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

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

这是我试图测试的问题

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());
    }

}

库jar的pom.xml

<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>

生成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)
    {
        Main foo = new Main(args);
    }

}

app jar的pom.xml

<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>

我还尝试了Class-Path元素的所有这些设置

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

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

java -jar myapp-0.0.1-SNAPSHOT.jar

但我总是收到此错误

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

无论我如何在app pom.xml中指定Class-Path元素,无论我是否将classpath添加到命令行,例如

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

并且是的,两个罐子都在同一个目录中,我在linux上运行它。

编辑:

JB Nizet请求以 java -jar 运行命令的结果,但不幸的是我已经删除了测试代码,因为他让我解决了问题。但是如果它有任何好处,那么源代码的输出首先为我提出问题,但仍然表现出相同的异常,但现在也正在使用他建议的第二个命令。

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

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

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

这是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

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

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

3 个答案:

答案 0 :(得分:0)

如果应用程序jar文件的清单的Class-Path属性是

mylibrary-0.0.1-SNAPSHOT.jar

并且两个jar文件在同一目录中,然后运行

java -jar myapp-0.0.1-SNAPSHOT.jar should work.

您还可以明确地设置类路径,并且明确地设置主类,这更容易,并且不依赖于清单中的相对路径。

java -cp myapp-0.0.1-SNAPSHOT.jar:mylibrary-0.0.1-SNAPSHOT.jar org.myorg.myapp.Main

您不应在同一命令中使用-classpath和-jar选项。它是一个或另一个。

答案 1 :(得分:0)

在pom.xml中尝试一些修改

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>org.myorg.myapp.Main</mainClass>
                <addClasspath>true</addClasspath>
            </manifest>
        </archive>
    </configuration>
</plugin>

在此之后,如果您仍然遇到问题,只需使用任何解压缩工具解压缩myapp-0.0.1-SNAPSHOT.jar即可。解压缩后,检查META-INF文件夹中MANIFEST文件中mylibrary-0.0.1-SNAPSHOT.jar的路径。

答案 2 :(得分:0)

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

这是确切的问题和解决方案,在我确切知道我要搜索的内容之前,我找不到所有的搜索,即 可执行JAR忽略其自己的类路径属性

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