eclipse中的Maven项目可以作为java应用程序运行,但jar文件不能正常运行
我已经搜索过了,也许这个答案和我的问题很相似,但我还是不明白 我的项目结构: 包装学eclipse中的Maven项目可以作为java应用程序运行,但jar文件不能正常运行,java,css,eclipse,maven,Java,Css,Eclipse,Maven,我已经搜索过了,也许这个答案和我的问题很相似,但我还是不明白 我的项目结构: 包装学 ----src/main/java --------用户界面 ------------Init.java ----src/main/resources --------css ------------Init.css --------图像 ------------icon.png package ui; import javafx.application.Application; import javafx.
----src/main/java
--------用户界面
------------Init.java
----src/main/resources
--------css
------------Init.css
--------图像
------------icon.png
package ui;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Init extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button button = new Button();
button.setPrefSize(50, 50);
button.setId("button");
Group group = new Group();
group.getChildren().add(button);
Scene scene = new Scene(group,300,300);
scene.getStylesheets().add(getClass().getResource("../main/resources/css/Init.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
当我选择RunAs->java应用程序时,它工作得很好
但是当我运行maven包制作的jar时,它显示
(Init:23是scene.getStylesheets().add(getClass().getResource(“../main/resources/css/Init.css”).toExternalForm());)
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>PackageTry</groupId>
<artifactId>PackageTry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ui.Init</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4.0.0
包装
包装
0.0.1-快照
src
src
**/*.爪哇
org.apache.maven.plugins
maven阴影插件
2.4.3
包裹
阴凉处
ui.Init
我的问题是:如何使jar正常运行。谢谢大家! maven shade插件将只复制maven依赖项。您可能手动将jfxrt.jar添加到构建路径中,因此它在eclipse中不会丢失。您可以使用maven dependency插件复制依赖项,并尝试更改着色jar中的清单以包含复制的依赖项。或者,使用shade插件,您可以添加jfxrt.jar硬编码idk-see以获取引用i changed scene.getStylesheets().add(getClass().getResource(“../main/resources/css/Init.css”).toExternalForm();添加(getClass().getResource(“/main/resources/css/Init.css”).toExternalForm());那就行了
<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>PackageTry</groupId>
<artifactId>PackageTry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ui.Init</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>