简体   繁体   中英

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. Now when i run that .jar file the login panel class file of my project should open first. And for security purpose, i want that the .jar file should not be able to get extracted by someone in future.

You can create an executable jar with multiple classes using Maven for packaging your classes. Then set the main class that serves as the application entry point using the MAVEN SHADE PLUGIN.

Read more about maven shade plugin here: [ 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. Just make your login panel class contain your main method. maven-shade-plugin will put up all your dependencies and your class files in a single jar and compile them.

You can build the project by "mvn clean package" Once build your jar will be available in 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. You can't do much to stop that.

For creating the executable jar you need to put all your classes in single directory. then add manifest.txt file having written 'Main-Class: className' where class name is entry point(in your case login panel class). and then run following command jar -cvfm newJar.jar manifest.txt *.class It will generate newJar.jar

And use java -jar newJar.jar command to run the jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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