Java 申请未完成

Java application not completing

正在尝试添加 JFrame 以查看它是否有助于使用 launch4j 将小 jar 文件转换为 .exe。我编写了一个简短的程序来帮助在工作中对 HPLC 数据进行排序,并希望使其成为一个简单的点击。

当我从命令行 运行 它工作时 java KFile 并且 JFileChooser 让我为脚本选择工作目录。当我将它转换为 .exe 时,JFileChooser 从未呈现并且 .exe 关闭。

我读到我可能需要一个 JFrame 父级,因此我创建了一个 JFrame,但现在脚本在完成之前挂起,就好像在等待框架关闭一样。我是 java 的新手,所以我不确定如何解决这个问题。

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.FileVisitResult;
import java.nio.MappedByteBuffer;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.util.Collection;
import java.util.ArrayList;
import java.nio.file.SimpleFileVisitor;



public class KFile extends SimpleFileVisitor<Path> {

    public static void main(String[] args) {

        Path currPath = Paths.get("");
        String currDir = currPath.toAbsolutePath().toString();
        System.out.println(currDir);


        File dataDir = chooseDir("open");
        File destDir = chooseDir("save");


        if(!destDir.exists()) {

            try {
                destDir.mkdir();
            }
            catch (SecurityException se) {
                System.out.println("Couldn't make directory!");
            }

        }
        int n = 0;
        if(dataDir.exists()) {
            Collection<Path> allDir  = new ArrayList<Path>();
            try {
                addTree(dataDir.toPath(),allDir);
            }
            catch (IOException e) {
                System.out.println("Error with scanning");
            }
            for( Path thisPath : allDir ) {
                if(thisPath.toString().contains("Report.pdf")) {
                    Path thisDir = thisPath.getParent();
                    File f = new File(thisDir.toString(), "\Report.txt");
                    n = n + 1;
                    String fileName = "Report " + n + ".pdf";
                    try {
                        fileName = parseName(f);
                        System.out.println(fileName);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    File thisFile = new File(destDir + "\" + fileName);

                    try {
                        copyFile(thisPath.toFile(),thisFile);
                    } catch ( IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }


    }


    public static boolean copyFile(File sourceFile, File destFile) throws IOException {
        //create file if it doesn't exist.
        if(!destFile.exists()) {
            destFile.createNewFile();           
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        }
        finally {
            if(source != null) {
                source.close();
            }
            if(destination != null) {
                destination.close();
                return true;
            }
            return false;
        }

    }

    public static File chooseDir(String s) {

        JFrame myFrame = new JFrame("HPLC Data Transfer");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.pack();
        myFrame.setVisible(true);

        JFileChooser chooser = new JFileChooser();
        File currDir = new File(System.getProperty("user.home") + "\Documents");

        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setCurrentDirectory(currDir);

        int choice = 0;
        if (s.equals("save")) {
            choice = chooser.showSaveDialog(myFrame);
        } else {
            choice = chooser.showOpenDialog(myFrame);
        }

        myFrame.setVisible(false);
        myFrame.removeAll();
        if(choice == JFileChooser.APPROVE_OPTION) {
            System.out.println("You chose to open: " + chooser.getSelectedFile().getName());
            return chooser.getSelectedFile();
        }
        return new File("");
    }

    static String parseName(File f) throws IOException {

        BufferedReader textReader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-16"));

        int lnCnt = 32;
        String[] fileData = new String[lnCnt];

        for (int i = 0; i < lnCnt; i++) {
            fileData[i] = textReader.readLine();
        }
        fileData[1] = fileData[1].replace("\uFEFF","");
        String name = fileData[1].substring(13) + ".pdf";

        textReader.close();
        return name;
    }

    static void addTree(Path directory, final Collection<Path> all)
        throws IOException {
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
            all.add(file);
            return FileVisitResult.CONTINUE;
        }
    });
}

}

您可以尝试更改

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

然后调用

myFrame.dispose();

终止JFrame。 由于 javadocsEXIT_ON_CLOSE 使用 System.exit(); 终止了整个程序,我不确定这是否是停止您的应用程序的问题,但我希望它能有所帮助:)

看起来您刚刚在处理 JFrame 时调用了 setVisible(false)。这只是隐藏了您的 JFrame,并没有摆脱它。如果您想完全摆脱框架(及其所有资源),请调用 myFrame.dispose();