简体   繁体   English

Java:使用-cp选项运行的jar文件中的启动画面?

[英]Java: splash screen in a jar file run with the -cp option?

Is it possible to put the splash screen image in the jar file and use the -splash option on the command line? 是否可以将初始屏幕图像放在jar文件中,并在命令行上使用-splash选项?

Background: I have 3 jar files that have a fun dependency tree, where the main class I want to run is in the "common" jar (and it reflectively calls the main classes of the two other jars). 背景:我有3个具有有趣的依赖关系树的jar文件,我要运行的主类位于“公共” jar中(它反射地调用了其他两个jar的主类)。 So using the -jar command line is kind of out of the question. 因此,使用-jar命令行是不可能的。

Yes, I know you can use the SplashScreen-Image line in the manifest, but all manifests seem to be ignored when using the -cp option of the java command line. 是的,我知道您可以在清单中使用SplashScreen-Image行,但是当使用java命令行的-cp选项时,所有清单似乎都将被忽略。 I kind of want to keep resources self contained in the jars if possible, so I want one of the jars to hold the splash screen in question. 我想尽可能地将资源保持在罐子中,所以我想要其中一个罐子来保存初始屏幕。 But paths for the splash that follow "xx.jar!path", or "xx.jar/path" seem not to work. 但对于路径splash遵循“xx.jar!路径”,或“xx.jar /路径”似乎并没有工作。 Is it possible, and how? 有可能吗?如何?

EDIT: okay, let me try this again. 编辑:好的,让我再试一次。 I have three jars. 我有三个罐子。 One has graphics logic in it, let's call it the "UI", one has business logic in it, let's call it the "Core", and one has common classes between the two. 一个包含图形逻辑,我们称它为“ UI”,一个包含业务逻辑,我们称其为“ Core”,一个包含两者之间的通用类。 Both the Core component and the UI component can be run as their own app, and they have their own main classes and their jars are set up to include the common jar on the class path. Core组件和UI组件都可以作为自己的应用程序运行,并且它们具有自己的主类,并且其jar设置为在类路径中包括公共jar。 But the common jar has a class with a main method which calls both main classes. 但是普通的jar有一个带有main方法的类,该方法调用了两个main类。 The common jar, being a library between the two, is not supposed to have the UI or Core jar as a dependency because either one of them could be missing if the components are run separately. 通用jar是两者之间的库,因此不应将UI或Core jar作为依赖项,因为如果单独运行组件,则可能会缺少其中一个。

All I want is a way to reference a splash screen packed in the UI jar when calling the main class in the common jar. 我想要的是一种在公共jar中调用主类时引用包装在UI jar中的初始屏幕的方法。 I've already worked out the logistics of calling this common jar's main with the -cp option. 我已经解决了使用-cp选项调用此通用jar的main的-cp It just doesn't have a proper splash screen yet. 它只是没有合适的启动屏幕。 Is it possible, and how? 有可能吗?如何? (I was hoping to make this question generic and not include all that info, but whatever.) (我希望使这个问题变得笼统,不包括所有信息,而是其他信息。)

I don't find your justification for doing this persuasive, but here's the way to make it work. 我没有找到这样做的理由,但是这是使其发挥作用的方法。

Set the environment variables _JAVA_SPLASH_JAR to the file system path to the JAR that contains the splash screen image, and _JAVA_SPLASH_FILE to the name of the splash screen file within that JAR. 将环境变量_JAVA_SPLASH_JAR设置为包含初始屏幕图像的JAR的文件系统路径,并将_JAVA_SPLASH_FILE设置为该JAR中初始屏幕文件的名称。 For example, in a bash or sh shell, 例如,在bashsh shell中,

(export _JAVA_SPLASH_JAR=app.jar; \
 export _JAVA_SPLASH_FILE=META-INF/splash.jpg; \
 java -cp app.jar com.y.app.Main )

I think this approach is bad, because the environment variables are not documented as part of the java launcher interface, and implementation changes could break the splash screen or the entire launcher. 我认为这种方法是不好的,因为环境变量未作为java启动器接口的一部分进行记录,并且实现更改可能会破坏启动屏幕或整个启动器。

The explanation for using it is flimsy. 使用它的解释很脆弱。 Of course you wouldn't want any circular dependencies between the common library and the applications. 当然,您不希望公共库和应用程序之间有任何循环依赖。 But this has nothing to do with the archive structure. 但这与存档结构无关。 Why go to the trouble of removing the compile-time dependencies from the combo main class on the applications, when it has a strong runtime dependency on them? 当它对应用程序具有强大的运行时依赖关系时,为什么要从组合主类上删除编译时依赖关系呢?

This "common" main class is not part of the common library. 这个“公共”主类不是公共库的一部分。 It's another application. 这是另一个应用程序。 It would make more sense to allow it to have static dependencies on the applications, and to package it in a separate archive, with appropriate Main-Class , Class-Path , and SplashScreen-Image so that it can be launched with the -jar option. 允许它对应用程序具有静态依赖性,并使用适当的Main-ClassClass-PathSplashScreen-Image打包到一个单独的存档中,这样才可以使用-jar选项启动。 。 Then when your other applications are deployed individually, their common library won't contain the unnecessary combo application. 这样,当您单独部署其他应用程序时,它们的公共库将不会包含不必要的组合应用程序。 And when a new version of Java is installed, the splash screen will display correctly instead of crashing the launcher. 并且,当安装了新版本的Java时,启动屏幕将正确显示,而不会使启动器崩溃。

Use -jar and specify the location of any other jars you can use in your Class-Path manifest attribute. 使用-jar并指定您可以在Class-Path清单属性中使用的任何其他jar的位置。 Things that are on that classpath don't need to exist. 该类路径上的东西不需要存在。 This will still work. 这仍然会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用清单文件中的 -cp 选项和类路径运行 java 程序 - run a java program using -cp option and classpath in manifest file 在jar文件中使用java -cp时出错 - Error when using java -cp with jar file Java与jar一起运行时不遵循-cp选项的类路径规范 - Java not honoring the classpath specification with -cp option when running with a jar scala jar文件由java执行 - cp与jar - scala jar file executed by java - cp vs. jar Java Jar中使用文件的Splash Argument - Java Splash Argument using file inside Jar java maven launch4j没有启动画面; 同一罐子,并使用Launch4J GUI启动画面或JAVA-JAR:好 - java maven launch4j no splash screen; same jar and using Launch4J GUI splash screen OR JAVA - JAR: Ok Java 一起使用“-cp”和“-jar” - Java using “-cp” and “-jar” together 是否可以使用java -cp“ jarName.jar” com.hw.Main运行带有Spring Boot打包的jar? - Is it possible run a spring boot packaged jar with java -cp “jarName.jar” com.hw.Main? 使用 -jar 选项运行 java 命令时,您可以使用 -cp 提供额外的库吗? - When running a java command with the -jar option, can you supply additional libraries using -cp? 为什么 Java -cp 选项在使用 spring-boot-maven-plugin 时 -jar 不起作用? - Why is the Java -cp option not working when -jar does while using the spring-boot-maven-plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM