-1

I have this button which is meant to open a text file for easy tweaking. It works fine when I put the text file in the same directory, but when I try to put it in a subsidiary directory and change the path to "config/gameItems.txt" it doesn't do anything, not even tell me it can't find the file. Any thoughts?

        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");
                }
            }
            });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Peter F
  • 435
  • 1
  • 8
  • 17
  • 1) *"Any thoughts?"* For better help sooner, post an [SSCCE](http://sscce.org/). 2) Why not open the text file in a `JTextArea`? 3) What is in the file? 4) Even if the file needs editing, using `Dekstop.getDesktop().open(File)` will be far more reliable (it will work on OS X & * nix, for one thing). – Andrew Thompson Jun 27 '13 at 18:16
  • If persisting with `exec`.. Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Jun 27 '13 at 18:18

2 Answers2

1

This is windows specific code, I think the problem is using the file separator "/" instad of "\", try changing your code to

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "config\\gameItems.txt");
Mehul Rathod
  • 1,244
  • 8
  • 7
1

Since you're doing it on Windows try this:

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "config\\gameItems.txt");
darijan
  • 9,725
  • 25
  • 38
  • Also consider [`ProcessBuilder`](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html). – trashgod Jun 27 '13 at 23:02