gpt4 book ai didi

java - jar 中的类可以使用不同 jar 中的类吗

转载 作者:太空宇宙 更新时间:2023-11-04 13:46:18 25 4
gpt4 key购买 nike

我是 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);
}

}

应用程序 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

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

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

这是处理器库 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

最佳答案

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

mylibrary-0.0.1-SNAPSHOT.jar

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

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

您还可以显式设置类路径,并显式设置主类,这更容易,并且不依赖于 list 中的相对路径。

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

不应在同一命令中使用 -classpath 和 -jar 选项。两者择其一。

关于java - jar 中的类可以使用不同 jar 中的类吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com