Java 当文本文件不在类根目录中时,打开文本文件的按钮将不起作用

Java 当文本文件不在类根目录中时,打开文本文件的按钮将不起作用,java,swing,file,process,runtime.exec,Java,Swing,File,Process,Runtime.exec,我有这个按钮,这是为了打开一个文本文件,方便调整。当我把文本文件放在同一个目录中时,它可以正常工作,但是当我试图把它放在一个辅助目录中,并将路径更改为config/gameItems.txt时,它不会做任何事情,甚至不会告诉我它找不到该文件。有什么想法吗 JButton itemsButton = new JButton("Items"); //Add action listener to button itemsButton.ad

我有这个按钮,这是为了打开一个文本文件,方便调整。当我把文本文件放在同一个目录中时,它可以正常工作,但是当我试图把它放在一个辅助目录中,并将路径更改为config/gameItems.txt时,它不会做任何事情,甚至不会告诉我它找不到该文件。有什么想法吗

        JButton itemsButton = new JButton("Items");
            //Add action listener to button
            itemsButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                try {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "config/gameItems.txt");
                } catch (Exception a) {
                System.out.println("File not found");
                }
            }
            });

这是windows特定的代码,我认为问题在于使用文件分隔符/instad of\,请尝试将代码更改为

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "config\\gameItems.txt");

由于您是在Windows上执行此操作,请尝试以下操作:

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "config\\gameItems.txt");

有什么想法吗?为了更快地获得更好的帮助,请发布一个。2为什么不在JTextArea中打开文本文件?3档案里有什么?4即使文件需要编辑,使用Dekstop.getDesktop.openFile也会更加可靠。首先,它可以在OS X&*nix上工作。。阅读并执行的所有建议。这可能会解决问题。如果没有,它应该提供更多关于失败原因的信息。然后忽略它引用exec,并使用ProcessBuilder构建流程。还可以将字符串arg拆分为字符串[]args,以说明本身包含空格的参数。抱歉,不知道谁先回答。你得到+1也考虑。