赞
踩
- <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>
- 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();
- }
- }
填写需要加密的jar路径,和加密后存放jar的路径
尝试反编译
(1)命令行启动,输入密码:java -jar demo.jar
(2)传参启动:java -jar demo.jar --xjar.password=123
(3)后台启动:nohup java -jar demo.jar --xjar.keyfile=password.key
前言:有一种需求,客户不买服务器,要你运行在他的win系统上,然后又不会装JDK,客户又多,作为一名合格的开发又不能闲着没屁事,天天给客户装JDK,所有就有了这篇文章(原理就是把jdk和项目打包一起给他,在写个bat脚本,让他双击直接启动项目)。
复制你电脑的jre
注:如果是jdk9以上版本没有jre,但是有jmods,需要自己手动生成,如下图所示
进入jdk所在目录下,通过命令手动生成jre文件
bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre
- @echo off
- start /b ./jre/bin/java.exe -jar demo.jar --xjar.password=123
- cmd #这行可以删除,作用只是防止小黑窗口关闭
注:javaw.exe启动不会显示控制台内容,java.exe启动会显示控制台内容
stop.bat:停止项目的脚本
- @echo off
- set port=8080 #项目端口号
- for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do taskkill /pid %%m -t -f
- exit
收工!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。