简体   繁体   English

如何制作具有多个类的.jar文件?

[英]How to make a .jar file with multiple classes?

Suppose i've created multiple classes and want to combine them in a single .jar file which should be portable. 假设我已经创建了多个类,并希望将它们合并到一个可移植的.jar文件中。 Now when i run that .jar file the login panel class file of my project should open first. 现在,当我运行该.jar文件时,我的项目的登录面板类文件应首先打开。 And for security purpose, i want that the .jar file should not be able to get extracted by someone in future. 为了安全起见,我希望将来无法将.jar文件提取出来。

You can create an executable jar with multiple classes using Maven for packaging your classes. 您可以使用Maven打包类来创建具有多个类的可执行jar。 Then set the main class that serves as the application entry point using the MAVEN SHADE PLUGIN. 然后使用MAVEN SHADE PLUGIN设置用作应用程序入口点的主类。

Read more about maven shade plugin here: [ https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html][1] 在此处阅读有关Maven阴影插件的更多信息:[ https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html] [1 ]

For your 1st question : You can use maven-shade-plugin to build an executable jar. 对于第一个问题:您可以使用maven-shade-plugin来构建可执行jar。 Just make your login panel class contain your main method. 只需使您的登录面板类包含您的main方法即可。 maven-shade-plugin will put up all your dependencies and your class files in a single jar and compile them. maven-shade-plugin会将所有依赖项和类文件放在一个jar中并进行编译。

You can build the project by "mvn clean package" Once build your jar will be available in target/ 您可以通过“ mvn clean package”来构建项目。一旦构建,jar将在target /

maven : goal 专家:目标

<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>`enter code here`
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>your.main.class.file</mainClass>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

For your 2nd question : A jar file can always be extracted. 对于第二个问题:始终可以提取jar文件。 You can't do much to stop that. 您不能做很多事情来阻止这种情况。

For creating the executable jar you need to put all your classes in single directory. 为了创建可执行jar,您需要将所有类放在单个目录中。 then add manifest.txt file having written 'Main-Class: className' where class name is entry point(in your case login panel class). 然后添加已写有“ Main-Class:className”的manifest.txt文件,其中类名是入口点(在您的情况下为登录面板类)。 and then run following command jar -cvfm newJar.jar manifest.txt *.class It will generate newJar.jar 然后运行以下命令jar -cvfm newJar.jar manifest.txt * .class它将生成newJar.jar

And use java -jar newJar.jar command to run the jar. 并使用java -jar newJar.jar命令运行jar。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM