if 语句导致 jar 不 运行

if statement causing jar to not run

我正在尝试使用一些 Apache IO 方法制作一个复制文件然后将其粘贴到另一个位置的程序。它在 Eclipse 中完美运行,但是当我将它导出到 JAR 时,JAR 没有 运行。 "Past Filepaths.txt" 文件在我的项目文件夹中。当我查看任务管理器时,我可以看到它启动了几秒钟,然后才消失。我已将其缩小为单个 if 语句:

if (filePaths.length == 2){
        source.setText(filePaths[0]);
        dest.setText(filePaths[1]);
}

如果我将其注释掉,JAR 运行s。如果我不这样做,它就不会。

这是我在大约 30 分钟内创建的一些非常粗糙的代码,作为一种脚本,用于帮助我四处移动一些文件,所以如果它看起来有点粗糙,我深表歉意。

我的完整代码:

public class Main {
    private JFrame jf = new JFrame();
    private JTextField source, dest;
    private String sourcePath, destPath;

public Main() {
    String[] filePaths = null;
    try {
        filePaths = FileUtils.readFileToString(new File("Past Filepaths.txt"), "ASCII").split("~");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    JPanel panel = new JPanel();
    JButton jButton = new JButton("Update Files");

    source = new JTextField("", 40);
    dest = new JTextField("", 40);

    if (filePaths.length == 2){
        source.setText(filePaths[0]);
        dest.setText(filePaths[1]);
    }

    jButton.addActionListener( (e) -> {
        updateVars();
        updateFiles();
    });
    jf.setSize(500, 200);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setLocationRelativeTo(null);
    jf.setResizable(false);
    jf.add(panel);
    panel.add(new JLabel("Source"));
    panel.add(source);
    panel.add(new JLabel("Destination"));
    panel.add(dest);
    panel.add(jButton);
    jf.setVisible(true);
}

private void updateVars(){
    sourcePath = source.getText();
    destPath = dest.getText();
}

private void updateFiles(){
    if(new File(sourcePath).exists() == false){
        JOptionPane.showMessageDialog(jf, sourcePath + " is not a valid file path!");
        return;
    }

    if(new File(destPath).exists() == false){
        JOptionPane.showMessageDialog(jf, destPath + " is not a valid file path!");
        return;
    }

    try {
        FileUtils.copyDirectory(new File(sourcePath), new File(destPath));
    } catch (IOException e) {
        e.printStackTrace();
    }

    File pastFiles = new File("Past Filepaths.txt");
    try{
        FileUtils.write(pastFiles, sourcePath + "~", "ASCII");
        FileUtils.write(pastFiles, destPath, "ASCII", true);
    }catch(Exception e){
        e.printStackTrace();
    }
}

这是调试问题,本身不是代码问题。所以,调试它。

以调试模式启动 JVM:

java -Xdebug -agentlib:jdwp=transport=dt_socket,address=9999,server=y,suspend=y <rest of your startup command>

请注意 suspend=y 将停止执行,直到您将 IDE 连接到它。

打开你的 IDE 并在你希望执行停止的地方设置一个断点,将你的 IDE 连接到调试端口 9999(根据 address=9999),然后单步执行代码以查看发生了什么。