0

I've built this actionPerformed method so that it reads a string I pass to the button (I needed to make my own button class to hold this new string) and depending on what it says, it does a different action. One of the possibilities of the string is to be something like: shell(""). This is supposed to run a system command (command line in windows, shell command in unix/linux) in the background. This is the source of the method:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.button) {
            if (password != "") {

            }
            if (action.startsWith("shell(\"")) {
                String tmpSHELL = action.substring(7, action.length() - 2);

                try {
                    Process p = Runtime.getRuntime().exec(tmpSHELL);
                } catch (IOException e1) {
                    ErrorDialog error = new ErrorDialog("Error handling your shell action");
                    System.exit(0);
                }

            }
            else if (action.startsWith("frame(\"")) {
                String tmpFRAME = action.substring(7, action.length() - 2);
                MenuFrame target = ConfigReader.getFrame(tmpFRAME);
                this.parent.setVisible(false);
                this.parent.validate();
                target.setVisible(true);
                target.validate();
            }
            else if (action.equals("exit()")) {
                System.exit(0);
            }
            else {
                ErrorDialog error = new ErrorDialog("You config file contains an invalid action command. Use either shell(), frame() or exit()");
                System.exit(0);
            }   
        }
    }

I know that I get into the method but I'm not sure if the command is being executed successfully. I'm currently in a windows environment so I made a simple batch script that echos some text then waits for a keystroke before printing the tree of the C: drive. I put the .bat into my working java directory and passed the string shell("test") (test is the name of the batch file). However, when I click the button I get an error dialog (the one I coded above).

Is there something wrong with my code or maybe my understanding of how executing a shell command works in java? The command is throwing an IO exception but I can't seem to figure out why. Thanks in advance for your help.

Stacktrace:

java.io.IOException: Cannot run program "test": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at Button.actionPerformed(Button.java:52)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 30 more
10
  • What exactly is the value of tmpSHELL? Commented May 25, 2011 at 14:02
  • 1
    It may be about how Windows starts a shell to run the script. In Unix/Linux, your put the path to the shell at the top: the she-bang. A slightly different test (or knowing more windows) would help: try a simple program. Commented May 25, 2011 at 14:03
  • 1
    I imagine you would need cmd in Windows, not shell. Can you log somewhere or show in the dialog window the e1 exception message and, optionally, stack trace? Commented May 25, 2011 at 14:04
  • 1
    Anything changes if you say shell("\test.bat") in your actual example? Alternative: put the test.bat in a directory that is listed on the PATH variable. Commented May 25, 2011 at 14:14
  • 1
    Try adding directory containing test to your PATH and restarting the IDE. Commented May 25, 2011 at 14:17

3 Answers 3

1
The system cannot find the file specified

Your file path is not right. Try passing an absolute file path.

shell("C:/somedirectory/test.bat")

Also, you can test this by removing the string test altogether. Hard code the runtime execution of the batch file by making the if statement always true and passing the path to your batch file to the Runtime.getRuntime().exec()

         if (password != "") {

         }
        if (true) {
            String tmpSHELL = action.substring(7, action.length() - 2);

            try {
                Process p = Runtime.getRuntime().exec("test");
            } catch (IOException e1) {
                ErrorDialog error = new ErrorDialog("Error handling your shell action");
                System.exit(0);
            }

        }

This should produce the same error. Then replace the file path with an absolute file path and you should be able to execute the batch file.

Process p = Runtime.getRuntime().exec("C:/somedirectory/test.bat");
Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer, and yeah it did end up being the path. The reason I couldn't get it to work when I tried typing in the full path was because I wasn't escaping my backslashes (\\ instead of ). Thanks for everyone's help.
1

On Windows try command line:

"cmd test /c"

Comments

1

It's because the command test isn't found in the system PATH environment variable.

If you go to the command line and type test it will fail. This is what the exception is indicating.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.