package com.xie.jad;
import java.awt.Color;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
/**
* java反编译工具的调用
* 封装了jad1.5.8g
* @author centre
*
*/
@SuppressWarnings("serial")
public class JadFrame extends JFrame{
private JLabel lb;
private JTextField tf;
private JButton btnOk,btnSect;
private TextArea ta;
private JPanel p1,p2;
private JadFrame jf;
private void launchFrame(){
this.setBounds(600, 400, 320, 240);
this.setTitle("java反编译工具beta2.0");
//this.setIconImage();
//this.setLayout(null);
p1=new JPanel(null);
p1.setBounds(0, 0, 320, 60);
p1.setBackground(Color.pink);
p2=new JPanel(null);
p2.setBounds(0, 60, 320,180);
lb=new JLabel("请选择目录:");
lb.setBounds(0, 20, 70, 20);
tf=new JTextField("C:/");
tf.setBounds(70, 20, 110, 20);
btnOk=new JButton("反编译");
btnOk.setBounds(240,20, 80, 20);
btnOk.addActionListener(new DecodeMonitor());
btnSect=new JButton("选择");
btnSect.addActionListener(new SelectMonitor());
btnSect.setBounds(180, 20, 60, 20);
ta=new TextArea("初始化成功!",23,1);
ta.setBackground(Color.ORANGE);
ta.setBounds(0, 60, 320, 160);
p1.add(lb);
p1.add(tf);
p1.add(btnSect);
p1.add(btnOk);
p2.add(ta);
this.add(p1);
this.add(p2);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setVisible(true);
}
public JadFrame(){
jf=this;
launchFrame();
}
public JLabel getLb() {
return lb;
}
public void setLb(JLabel lb) {
this.lb = lb;
}
public JTextField getTf() {
return tf;
}
public void setTf(JTextField tf) {
this.tf = tf;
}
public JButton getBtnOk() {
return btnOk;
}
public void setBtnOk(JButton btnOk) {
this.btnOk = btnOk;
}
public JButton getBtnSect() {
return btnSect;
}
public void setBtnSect(JButton btnSect) {
this.btnSect = btnSect;
}
public TextArea getTa() {
return ta;
}
public void setTa(TextArea ta) {
this.ta = ta;
}
public JPanel getP1() {
return p1;
}
public void setP1(JPanel p1) {
this.p1 = p1;
}
public JPanel getP2() {
return p2;
}
public void setP2(JPanel p2) {
this.p2 = p2;
}
public static void main(String[] args){
new JadFrame();
}
/**
* 选择按钮的监视器
* @author centre
*
*/
class SelectMonitor implements ActionListener{
private JFileChooser chooser;
@Override
public void actionPerformed(ActionEvent e) {
selectDir();
}
/**
* @author centre
* java如何打开一个选择框
*/
private void selectDir(){
//根据制定目录,打开一个选择框
chooser = new JFileChooser(tf.getText());
//选择打开模式,有3中选择,1只打开目录,2只打开文件,3都可以打开
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//显示的选择框依附于哪个窗口
int num= chooser.showOpenDialog(jf);
if (num==JFileChooser.APPROVE_OPTION) {
tf.setText(chooser.getSelectedFile().getPath());
}
}
}
/**
*
* @author centre
* 调用jad.exe进行反编译
*
*/
class DecodeMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String pathTmp=tf.getText();
//注意java中的/和/
String path=pathTmp.replace("//", "/");
int b=findClass(path);
if (b==1) {
ta.setText(ta.getText()+"/n"+"反编译完成!");
}else {
ta.setText(ta.getText()+"/n"+"编译过程中出现错误,请查看。");
}
}
/**
* @author centre
* @param path String 传入目录和文件目录
* @return int 没有任何错误返回1,否则返回0
* 用递归方法找到所有的.class文件,然后将其反编译到该目录下
*/
private int findClass(String path){
int b=1;
File baseFile=new File(path);
Pattern p=Pattern.compile(".*class$");
Matcher m=null;
String tmp=null;
//String[] fileList=baseFile.list();
if (!baseFile.exists()) {
new JDialog(jf, "该目录不存在!").setVisible(true);
}
if (baseFile.isDirectory()) {
String[] fileList=baseFile.list();
for (int i = 0; i < fileList.length; i++) {
File readFile=new File(path+"/"+fileList[i]);
if (readFile.isDirectory()) {
findClass(path+"/"+fileList[i]);
}else {
tmp=readFile.getName();
m=p.matcher(tmp);
String path1=readFile.getAbsolutePath().replace("//", "/");
String path2=readFile.getParent().replace("//", "/");
if (m.find()) {
try {
//java调用exe文件
Runtime.getRuntime().exec("jad.exe -o -d "+path2+" -s java "+path1);
ta.setText(ta.getText()+"/n"+path1+"反编译成功!");
System.out.println(path2);
} catch (IOException e) {
b=0;
e.printStackTrace();
}
}else {
b=0;
ta.setText(ta.getText()+"/n"+path1+"该文件不能反编译!");
}
}
}
}else {
tmp=baseFile.getName();
m=p.matcher(tmp);
String path1=baseFile.getAbsolutePath().replace("//", "/");
String path2=baseFile.getParent().replace("//", "/");
if (m.find()) {
try {
Runtime.getRuntime().exec("jad.exe -o -d "+path2+" -s java "+path1);
ta.setText(ta.getText()+"/n"+path1+"反编译成功!");
System.out.println(path2);
} catch (IOException e) {
b=0;
e.printStackTrace();
}
}else {
b=0;
ta.setText(ta.getText()+"/n"+path1+"该文件不能反编译!");
}
}
return b;
}
}
}
本文介绍了一个使用Java实现的反编译工具,该工具能够遍历指定目录下的所有.class文件,并利用jad.exe对其进行反编译,同时提供了友好的图形用户界面。
418

被折叠的 条评论
为什么被折叠?



