懒人方案
网盘:
链接:https://pan.baidu.com/s/1x4OB1IF2HZGgtLhd1Kr_AQ
提取码:glx7
网盘内是已生成可用工具,下载可以直接使用,使用前看一下READ.txt文件。
Maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zy.java</groupId> <artifactId>Util</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.github.core-lib</groupId> <artifactId>xjar</artifactId> <version>v1.1.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.github.core-lib</groupId> <artifactId>loadkit</artifactId> <version>v1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.6.RELEASE</version> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
java代码
package com.zy.java; import io.xjar.XConstants; import io.xjar.XKit; import io.xjar.boot.XBoot; import io.xjar.key.XKey; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.security.NoSuchAlgorithmException; public class Practice{ private static String password = "123456"; private static JButton openItem, saveItem; private static FileDialog openDia, saveDia;// 定义“打开、保存”对话框 private static String filePathOld;//旧文件路径 private static String filePathNew;//新文件路径 private static String fileName;//文件名 static JFrame f = null; static JTextField locationText, typeText; public static void main(String[] args) { int gap = 5; f = new JFrame("加密程序"); ImageIcon icon = new ImageIcon("src\\images\\2.ico"); f.setIconImage(icon.getImage());// 给窗体设置图标方法 f.setSize(550, 310); f.setLocation(200, 200); f.setLayout(null); JPanel pInput = new JPanel(); pInput.setBounds(gap, gap, 520, 60); pInput.setLayout(new GridLayout(2, 3, gap, gap)); JLabel location = new JLabel("JAR路径:"); locationText = new JTextField(10); openItem = new JButton("选取");// 创建“打开"菜单项 JLabel type = new JLabel("保存路径:"); typeText = new JTextField(10); saveItem = new JButton("选取");// 创建“保存"菜单项 openDia = new FileDialog(f, "打开", FileDialog.LOAD); saveDia = new FileDialog(f, "保存", FileDialog.SAVE); JButton b = new JButton("生成"); pInput.add(location); pInput.add(locationText); pInput.add(openItem); pInput.add(type); pInput.add(typeText); pInput.add(saveItem); //文本域 final JTextArea ta = new JTextArea(); ta.setLineWrap(true); b.setBounds(220, 60 + 30, 80, 30); ta.setBounds(gap, 80 + 60, 523, 120); f.add(pInput); f.add(b); f.add(ta); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); myEvent(); //鼠标监听 b.addActionListener(new ActionListener() { boolean checkedpass = true; public void actionPerformed(ActionEvent e) { XKey xKey = null; System.out.println(" --- : "+fileName); String newPath = filePathNew+"\\"+fileName+"-xjar.jar"; String oldPath = filePathOld; try { xKey = XKit.key(password); XBoot.encrypt(oldPath, newPath, xKey, XConstants.MODE_DANGER); ta.append(" Success! "); } catch (NoSuchAlgorithmException eg) { eg.printStackTrace(); } catch (Exception eg) { eg.printStackTrace(); } checkedpass = true; checkEmpty(locationText, "JAR路径"); checkEmpty(typeText, "保存路径"); if (checkedpass) { String model = "加密jar包保存路径 :%s"; String result = String.format(model, newPath); ta.setText(""); ta.append(result); } } //检验是否为空 private void checkEmpty(JTextField tf, String msg) { if (!checkedpass) return; String value = tf.getText(); if (value.length() == 0) { JOptionPane.showMessageDialog(f, msg + " 不能为空"); tf.grabFocus(); checkedpass = false; } } }); } //选取按钮监听 private static void myEvent() { // 选取监听 openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openDia.setVisible(true);//显示打开文件对话框 String dirpath = openDia.getDirectory();//获取打开文件路径并保存到字符串中。 String Name = openDia.getFile();//获取打开文件名称并保存到字符串中 System.out.println(" --- : " + Name.split(".jar")[0]); fileName = Name.split(".jar")[0]; filePathOld = dirpath+Name; if (dirpath == null || Name == null)//判断路径和文件是否为空 return; locationText.setText(dirpath+Name); } }); // 选取监听 saveItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); chooser.showDialog(new JLabel(), "选择"); File file = chooser.getSelectedFile(); if (file == null) { System.out.println(" 路径 :"+file.getAbsoluteFile().toString()); typeText.setText(file.getAbsoluteFile().toString()); filePathNew = file.getAbsoluteFile().toString(); }else{ System.out.println(" 路径 :"+file.getAbsoluteFile().toString()); typeText.setText(file.getAbsoluteFile().toString()); filePathNew = file.getAbsoluteFile().toString(); } } }); // 窗体关闭监听 f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
代码先进行打包,Maven ——> clean ——> install
代码运行测试
选择jar文件,保存路径,点击生成即可。
下载exe生成器
1.
exe4j : https://www.apponic.com/phome/download/95409-2-1593674778-a86aa6a3165855674c85c5a9dc7c8dbb/
下载安装完成后,界面如图, 第一步完成注册,Name,Company随便填就是了,注册码:A-XVK258563F-1p4lv7mg7sav (网上很多啊,随便搜一个就行)
2.
点击 next ,勾选JAR in EXE
3.
点击 next ,填写打包后的 exe程序名称和保存路径
4.
点击 next ,设置程序方式,兼容系统64位
5.
点击 next ,添加jar包,选择运行的主类的main方法
6.
点击 next ,设置JDK运行版本
7.
复制jre,放到jre的空文件夹下:D:/jre/jre
8.
加载jre环境
9.
一直点 next ,等待安装完成。
10.
尝试运行exe文件
成功!!
转载请注明:SuperIT » java:编写jar包加密工具,防止反编译