Prevent jar from being decompiled & run jar without installing jdk

Prevent jar from being decompiled

1.pom.xml

<repositories>
    <repository>
        <id>jitpack</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
      <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
    </dependency>
    <dependency>
        <groupId>org.kordamp.bootstrapfx</groupId>
        <artifactId>bootstrapfx-core</artifactId>
        <version>0.2.4</version>
    </dependency>
    <dependency>
        <groupId>com.github.core-lib</groupId>
        <artifactId>xjar</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-compress</artifactId>
        <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>com.github.core-lib</groupId>
        <artifactId>loadkit</artifactId>
        <version>v1.0.1</version>
    </dependency>
</dependencies>

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.8.3</version>
    <configuration>
        <vendor>xiaoying</vendor>
        <mainClass>com.ying.demo.Mm</mainClass> #注意修改代码路径
    </configuration>
</plugin>

2. Encrypted startup class

import io.xjar.XKit;
import io.xjar.boot.XBoot;
import io.xjar.key.XKey;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class Mm extends Application {
    public void start(Stage primaryStage) throws Exception {
        FlowPane pane = new FlowPane();
        pane.setPadding(new Insets(11, 500, 13, 14));
        pane.setHgap(5);
        pane.setVgap(5);

        Label lab1 = new Label("指定加密前jar包路径(例如:D:\\work\\target\\demo-0.0.1-SNAPSHOT.jar)");
        Label lab2 = new Label("指定加密密码");
        /*Label lab3 = new Label("指定需要加密的资源(例如:com/ying/**)");*/
        Label lab4 = new Label("加密后JAR包输出路径(例如:D:\\work\\target\\demo.jar)");

        TextField text1 = new TextField();
        text1.setPrefWidth(320);
        TextField text2 = new TextField();
        /*TextField text3 = new TextField();
        text3.setPrefWidth(320);*/
        TextField text4 = new TextField();
        text4.setPrefWidth(320);
        TextArea mess = new TextArea();
        mess.setPrefWidth(400);
        mess.setPrefHeight(120);

        Button button = new Button("加密");

        button.setOnAction(e -> {
            String value1 = text1.getText();
            String value2 = text2.getText();
            /*String value3 = text3.getText();*/
            String value4 = text4.getText();
            Alert alert = new Alert(Alert.AlertType.WARNING);
            Alert alertError = new Alert(Alert.AlertType.ERROR);
            Alert alertOk = new Alert(Alert.AlertType.INFORMATION);
            if (value1 == null || value1.trim().isEmpty()) {
                alert.setContentText("指定加密的jar包路径输入有误");
                alert.showAndWait();
                return;
            }
            if (value2 == null || value2.trim().isEmpty()) {
                alert.setContentText("指定加密密码输入有误");
                alert.showAndWait();
                return;
            }
            /*if(value3 == null || value3.trim().isEmpty()){
                alert.setContentText("指定需要加密的资源输入有误");
                alert.showAndWait();
                return;
            }*/
            if (value4 == null || value4.trim().isEmpty()) {
                alert.setContentText("指定加密后JAR包输出路径输入有误");
                alert.showAndWait();
                return;
            }
            try {
                XKey xKey = XKit.key(value2);
                XBoot.encrypt(value1, value4, xKey);
            } catch (Exception ex) {
                ex.printStackTrace();
                alertError.setContentText(ex.getMessage());
                alertError.showAndWait();
                return;
            }
            String path = value4.substring(0, value4.lastIndexOf("\\") == -1 ? 0 : value4.lastIndexOf("\\") + 1) + "password.key";
            File file = new File(path);
            //判断文件是否存在
            try {
                if (file.createNewFile()) {
                    //创建一个用于操作文件的字节输出流对象,创建就必须明确数据存储目的地
                    FileOutputStream fos = new FileOutputStream(file);
                    //写入
                    fos.write(("password: " + value2).getBytes(StandardCharsets.UTF_8));
                    //刷新并关闭流
                    fos.flush();
                    fos.close();
                }
            } catch (Exception eio) {
                eio.printStackTrace();
                alertError.setContentText(eio.getMessage());
                alertError.showAndWait();
                return;
            }

            String jar = value4.substring(value4.lastIndexOf("\\") == -1 ? 0 : value4.lastIndexOf("\\") + 1);
            String mes = "(1)命令行启动,输入密码\n" +
                    "java -jar " + jar + " \n" +
                    "(2)传参启动\n" +
                    "java -jar " + jar + " --xjar.password=" + value2 + "\n" +
                    "(3)后台启动\n" +
                    "nohup java -jar " + jar + " --xjar.keyfile=password.key";
            alertOk.setContentText("加密成功\n");
            alertOk.showAndWait();
            mess.setText(mes);

        });
        pane.getChildren().addAll(lab1, text1, lab2, text2, lab4, text4, button, mess);
        Scene scene = new Scene(pane, 480, 480);
        primaryStage.setTitle("encrypt-v1.0");
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

3. Run the encryption class

Fill in the jar path that needs to be encrypted, and the path to store the jar after encryption

try to decompile

4. Start the project

(1) Start the command line, enter the password: java -jar demo.jar

(2) Start by passing parameters: java -jar demo.jar --xjar.password=123

(3) Background startup: nohup java -jar demo.jar --xjar.keyfile=password.key


Run jar without installing jdk

Foreword: There is a demand that the customer does not buy a server, but wants you to run it on his win system, and then he does not know how to install JDK, and there are many customers. As a qualified developer, he can't be idle and has nothing to do, and installs JDK for customers every day. JDK, there is this article (the principle is to package the jdk and the project together for him, and write a bat script, let him double-click to start the project directly).

1. Copy jre under jdk

Copy your computer's jre

Note: If the version above jdk9 does not have jre, but has jmods, you need to manually generate it yourself, as shown in the figure below

Enter the directory where the jdk is located, and manually generate the jre file through the command

bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre

2. Create start.bat file

@echo off
start /b  ./jre/bin/java.exe -jar demo.jar --xjar.password=123
cmd #这行可以删除,作用只是防止小黑窗口关闭

Note: The console content will not be displayed when javaw.exe is started, but the console content will be displayed when java.exe is started

stop.bat: script to stop the project

@echo off
set port=8080  #项目端口号
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do taskkill /pid %%m -t -f
exit

3. Put them (jre, demo.jar, start.bat) in the same folder and give them to the customer together. The customer can start by double-clicking start.bat

4. Double-click start.bat to run the project successfully

knock off!

Guess you like

Origin blog.csdn.net/weixin_46522803/article/details/129240983