我正在尝试制作一个程序,该程序使用几个Apache IO方法来复制文件,然后将其粘贴到另一个位置。它可以在Eclipse中完美运行,但是当我将其导出到JAR时,JAR无法运行。“ Past Filepaths.txt”文件在我的项目文件夹中。在任务管理器中查看时,可以看到它启动了几秒钟,然后消失了。我将其范围缩小为一个if
声明:
if (filePaths.length == 2){
source.setText(filePaths[0]);
dest.setText(filePaths[1]);
}
如果我将其注释掉,则JAR会运行。如果我不这样做,那不是。
这是我在大约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
),然后逐步执行代码以查看发生了什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句