How to run an external program from within an event handler?

View: New views
1 Messages — Rating Filter:   Alert me  

How to run an external program from within an event handler?

by Ted Byers :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I doubt this would be a FAQ, as I don't think this would commonly be done.  However, I have need to run a perl script from within an event handler for a command button.

This is my current code:

        try {
            java.lang.ProcessBuilder pn = new java.lang.ProcessBuilder("test_date.pl");
            pn.directory(new java.io.File("K:\\work"));
            java.lang.Process pr = pn.start();
            java.io.BufferedInputStream bis = (java.io.BufferedInputStream)pr.getInputStream();
            String tmp = new String("");
            byte b[] = new byte[1000];
            int i = 0;
            while (i != -1) {
                bis.read(b);
                tmp += new String(b);
            }
            getSelectionsDisplayTextArea().setText(getSelectionsDisplayTextArea().getText() + "\n\n" + tmp);
        } catch (java.io.IOException ex) {
            getSelectionsDisplayTextArea().setText(getSelectionsDisplayTextArea().getText() + "\n\n" + ex.getMessage());
        }
 
The present perl script and buffered read is not significant since  the real perl script will, when my colleague finishes it, write only a few characters to indicate the script ran to completion.

That is not the problem.  I suspect that I am running into security related permissions.  Here is the output I get from this code:

"Cannot run program "test_date.pl" (in directory "K:\work"): CreateProcess error=2, The system cannot find the file specified"

Of course, test_date.pl exists and is located in "K:\work"), and if executed from a commandline window, produces output like this:

C:\Perl\bin\perl.exe -w k:/Work/test_date.pl
20080814153051
20080813153051
20080812153051
20080811153051
20080808153051
20080807153051
20080806153051
20080805153051
20080804153051


If I provide the full path to the perl interpreter, changing the call to "java.lang.ProcessBuilder pn = new java.lang.ProcessBuilder("C:\\Perl\\bin\\perl.exe -w k:\\Work\\test_date.pl");", I get the following error.

Cannot run program "C:\Perl\bin\perl.exe -w k:\Work\test_date.pl" (in directory "K:\work"): CreateProcess error=123, The filename, directory name, or volume label syntax is incorrect

Of course, the volume label, path and filename are correct, so i don't know what the error in the syntax might be.

My system (Windows XP) is configured to see perl scripts (any file with the 'pl' extension) as executable, and such scripts get automatically passed to the perl interpreter.

Any ideas on how to fix this?

Thanks

Ted