java中cmd调用bandizip问题
如视频所见,当程序用控制台调用bz.exe时,任务管理器是没有出现“Bandzip Command line tool”进程的,但当我关闭程序时,该进程再出现,为什么呢?
代码如下:
//GUI类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class GUI extends JFrame {
public GUI(){
try {
UIManager.setLookAndFeel(javax.swing.plaf.nimbus.NimbusLookAndFeel.class.getName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
UnsupportedLookAndFeelException e) {
throw new RuntimeException(e);
}
Container co = getContentPane();
setLayout(new GridLayout(5,1));
JPanel file_path_panel = new JPanel();
JLabel file_path_label = new JLabel("压缩包路径");
JButton file_path_set_button = new JButton("打开...");
JPanel out_path_panel = new JPanel();
JPanel two_buttons_panel = new JPanel(new GridLayout(2,1));
JLabel out_path_label = new JLabel("导出路径");
JButton out_path_button = new JButton("解压到...");
JButton out_path_this_button = new JButton("解压到当前文件夹");
JTextArea jta = new JTextArea();
jta.setLineWrap(true);
JButton start_crack = new JButton("开始");
JPanel crack_mode = new JPanel(new GridLayout(1,7));
JCheckBox is_number = new JCheckBox("数字");
JCheckBox is_alphabet = new JCheckBox("小写字母");
JCheckBox is_alohabet_toUpperCase = new JCheckBox("大写字母");
JCheckBox is_symbol = new JCheckBox("特殊字符");
JTextField key_start_number = new JTextField();
key_start_number.addKeyListener(new ControlTheInput());
JLabel bolang = new JLabel("~");
JTextField key_end_number = new JTextField();
key_end_number.addKeyListener(new ControlTheInput());
start_crack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
ProcessBuilder builder = new ProcessBuilder("cmd.exe");
builder.redirectErrorStream(true);
Process process = null;
try {
process = builder.start();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), "GBK"));
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
// try {
// process.waitFor();
// } catch (InterruptedException ex) {
// throw new RuntimeException(ex);
// }
try {
bw.write("bz.exe x -o:"+out_path_label.getText()+" "+file_path_label.getText());
bw.newLine();
bw.flush();
String s;
while ((s = br.readLine()) != null){
System.out.println(s);
jta.append(s+"\n");
}
// bw.write("y\n");
// bw.flush();
// while ((s = br.readLine()) != null){
// System.out.println(s);
// jta.append(s);
// }
} catch (IOException ex) {
throw new RuntimeException(ex);
}
KeyDictionary kd = new KeyDictionary();
for(int i = Integer.parseInt(key_start_number.getText());i <= Integer.parseInt(key_end_number.getText());i++){
kd.init(is_number.isSelected(),is_alphabet.isSelected(),is_alohabet_toUpperCase.isSelected(),is_symbol.isSelected(),i);
String end_key = kd.get_end_key();
while (true){
String key = kd.GetKey();
try {
bw.write(key);
bw.newLine();
bw.flush();
String c = null;
if (!read(c,jta,br)){
break;
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
if (key.equals(end_key)){
break;
}
}
}
try {
br.close();
bw.close();
process.destroy();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}).start();
}
});
file_path_set_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser file_path_chooser = new JFileChooser();
file_path_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int i = file_path_chooser.showOpenDialog(getContentPane());
if(i == JFileChooser.APPROVE_OPTION) {
File zip_file = file_path_chooser.getSelectedFile();
file_path_label.setText(zip_file.getAbsolutePath());
}
}
});
out_path_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser out_path_chooser = new JFileChooser();
out_path_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int i = out_path_chooser.showOpenDialog(getContentPane());
if(i == JFileChooser.APPROVE_OPTION) {
File out_path = out_path_chooser.getSelectedFile();
out_path_label.setText(out_path.getAbsolutePath());
}
}
});
out_path_this_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String[] split = file_path_label.getText().split("\\\\");
String new_file_path = file_path_label.getText().replace(split[split.length-1],"");
out_path_label.setText(new_file_path);
}
});
co.add(jta);
file_path_panel.add(file_path_label,BorderLayout.CENTER);
file_path_panel.add(file_path_set_button,BorderLayout.EAST);
co.add(file_path_panel);
two_buttons_panel.add(out_path_button);
two_buttons_panel.add(out_path_this_button);
out_path_panel.add(out_path_label);
out_path_panel.add(two_buttons_panel);
co.add(out_path_panel);
crack_mode.add(is_number);
crack_mode.add(is_alphabet);
crack_mode.add(is_alohabet_toUpperCase);
crack_mode.add(is_symbol);
crack_mode.add(key_start_number);
crack_mode.add(bolang);
crack_mode.add(key_end_number);
co.add(crack_mode);
co.add(start_crack);
setBounds(600,300,600,600);
setVisible(true);
setTitle("Zip Crack");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private boolean read(String c,JTextArea jta,BufferedReader br) {
while (true){
try {
if ((c = br.readLine()) == null) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
jta.append(c+"\n");
System.out.println(c);
if (c.equals("Invalid password")){
return true;
}
}
return false;
}
class ControlTheInput extends KeyAdapter {
public void keyTyped(KeyEvent e) {
String key="0123456789"+(char)8;
if(key.indexOf(e.getKeyChar())<0){
e.consume();//如果不是数字则取消
}
}
}
public static void main(String[] args) {
new GUI();
}
}
//KeyDictionary类
public class KeyDictionary {
public static final String NUMBER = "0123456789";
/**
* 字母
*/
public static final String ALPHABET = "abcdefghijklmnopqrstuvwyxz";
/**
* 符号
*/
public static final String SYMBOL = "~!@#$%^&*()_+[]{};,.<>?-=";
private String base;
private StringBuilder sb;
private boolean isinit;
private int indexs;
public void init(boolean isnumber,boolean isalphabet,boolean isalphabetuppercase,boolean issymbol,int length){
base = "";
if (isnumber){
base += NUMBER;
}
if (isalphabet){
base += ALPHABET;
}
if (isalphabetuppercase){
base += ALPHABET.toUpperCase();
}
if (issymbol){
base += SYMBOL;
}
sb = new StringBuilder();
sb.setLength(0);
for (int i = 0;i < length;i++){
sb.append(0);
}
indexs = length;
isinit = true;
}
private String GetKey(int index){
if (isinit){
isinit = false;
return sb.toString();
}
if (sb.charAt(index) == base.charAt(base.length()-1)){
sb.replace(index,index+1,"0");
GetKey(index - 1);
}else {
sb.replace(index,index+1, String.valueOf(base.charAt(base.indexOf(sb.charAt(index))+1)));
}
return sb.toString();
}
public static String String_pow(String s,int t){
String number = "";
for (int i = 0;i<t;i++){
number += s;
}
return number;
}
private String get_end_char(){return String.valueOf(base.charAt(base.length()-1));}
public String GetKey(){
return GetKey(indexs-1);
}
public String get_end_key(){return String_pow(get_end_char(),indexs);}
}