You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Steve Butcher (Steve-O)" <st...@comcast.net> on 2005/03/25 18:51:22 UTC

launching windowed program with Runtime#exec() under Tomcat 5.0

I've been looking through the archives and reading the posts regarding 
Tomcat and Runtime#exec(). Almost everything I've read about 
Runtime#exec() points to the javaworld article on the pitfalls of 
Runtime#exec(). However, most of the examples seem to be for commands 
or shell scripts where some kind of interaction with the command line 
is desired.

The problem I'm having is with launching a windowed program, VLC, 
(http://www.videolan.org/)

I'm running Tomcat 5.0 (NOT as a service), JDK 1.4.2_07, W2KPro, VLC 
0.8.1

On the command line, the following works fine (VLC launches and the 
stream plays).

  >"C:\Program Files\VideoLAN\VLC\vlc.exe" --extraintf=rc 
--rc-host=127.0.0.1:9066 --rc-quiet "rtsp://192.168.0.66/" 
:sout=#duplicate{dst=display}

basically, the command line options tell VLC to open a simple interface 
for remote control, open an input stream over rtsp and send the output 
to the display. More complicated outputs (files, transcodings, streams) 
can be constructed.

Then I build that into a class:

  public class Launcher {
       public static main( String[] args) throws Exception {
            new Launcher().play();
       }

       public void play() throws Exception {
            String command = "\"C:\\Program 
Files\\VideoLAN\\VLC\\vlc.exe\" --extraintf=rc --rc-host=127.0.0.1:9066 
--rc-quiet \"rtsp://192.168.0.66/\" :sout=#duplicate{dst=display}";
            Runtime.getRuntime().exec( command);
       }
  }

and invoked it from the command line:

 >java Launcher

and *that* works fine (VLC launches and the stream plays) which leads 
me to believe that there is nothing wrong with using Runtime#exec() in 
this particular instance AND the command is fine.

HOWEVER, if I call the very same code in main() above from a JSP:

<%
	new Launcher().play();
%>

VLC launches but hangs with an "End Program" dialog.

IF I replace the command string with:

command = "notepad.exe";

Notepad launches just fine, directly from the code AND from a JSP which 
leads me to believe it is not a permission problem under Tomcat.

I've even gone so far as to create a BAT file with the same command and 
launch it using:

command = "cmd.exe /C C:\\path\\to\\file\\vlc";

and that works on the command line, directly in the code but NOT via 
JSP.

I'm nearing my wits ends so if someone out there ever figured out a 
similar problem or has a suggestion, I'd appreciate it.

Thanks in advance,
Steve





---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: launching windowed program with Runtime#exec() under Tomcat 5.0

Posted by Jason Bainbridge <jb...@gmail.com>.
On Fri, 25 Mar 2005 19:31:48 +0100, Markus Schönhaber
<ma...@schoenhaber.de> wrote:
> Am Freitag, 25. März 2005 18:51 schrieb Steve Butcher:

> Does vlc produce any output on stdout/stderr? If it does, it may stop when the
> output-buffer fills up. So you have to read Process#getOutputStream() to make
> it continue.

that makes more sense! It's been a while since I've played with this
stuff... Below is a snippet of code I used in a spellchecker
application that you can look at for ideas:

try

        {   

                        

            String osName = System.getProperty("os.name" );

                //System.out.println("OS Name: " + osName);

            String[] cmd = new String[3];

 

            if( osName.equals( "Windows NT" ) | osName.equals( "Windows 2000" ))

            {

                cmd[0] = "cmd.exe" ;

                cmd[1] = "/C" ;

                cmd[2] = "aspell -a";

            }

            else if( osName.equals( "Windows 95" ) )

            {

                cmd[0] = "command.com" ;

                cmd[1] = "/C" ;

                cmd[2] ="aspell -a";

            }

            

            Runtime rt = Runtime.getRuntime();

            System.out.println("Execing " + cmd[0] + " " + cmd[1]

                               + " " + cmd[2]);

            Process proc = rt.exec(cmd);

 

                PrintWriter stdin = new
PrintWriter(proc.getOutputStream(), true);

                stdin.write(spellCheck);

                //System.out.println(spellCheck);

                //stdin.write(26);

                stdin.close();

            

                InputStreamReader isr = new
InputStreamReader(proc.getInputStream());

            BufferedReader br = new BufferedReader(isr);

            String line=null;

                int i = 0;

                try {

            while ( (line = br.readLine()) != null) {

                        if (line.equals("*")){

                                    aSuggWords[i] = "correct";

                        }else{

                                    aSuggWords[i] = line;

                        }

                //out.println(line + "<br>");

                        returnString = returnString + "<br>" + line;

                        i++;

                        }

            } catch (IOException ioe)

              {

                ioe.printStackTrace();  

              }

                proc.destroy();

Regards,


-- 
Jason Bainbridge
http://kde.org - webmaster@kde.org
Personal Site - http://jasonbainbridge.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: launching windowed program with Runtime#exec() under Tomcat 5.0

Posted by "Steve Butcher (Steve-O)" <st...@comcast.net>.
w00t!

I got it to work.

It turns out that VLC *is* sending output to both stderr and stdout. 
 From the oft-mentioned JavaWorld article, 
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html, I 
added the StreamGobblers to play();

public void play() throws Exception {
	String command = "\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" 
--extraintf=rc --rc-host=127.0.0.1:9066 --rc-quiet 
\"rtsp://192.168.0.66/\" :sout=#duplicate{dst=display}";
	Process child = Runtime.getRuntime().exec( command);
	StreamGobbler err = new StreamGobbler( child.getErrorStream(), "err");
	StreamGobbler out = new StreamGobble( child.getInputStream(), "out");
	err.start();
	out.start();
}

and now Launcher works both when called directly and when called from a 
JSP.

Now, as a result of this wild ride, it is not readily apparent:

1) where stderr gets piped to in DOS...when I ran the command directly 
at the DOS prompt, where was all of this output?
2) why it was running fine as >java Launcher but not when called from a 
JSP...the buffer of Runtime#exec() changes? It doesn't seem likely.

all of which would have given clues as to why and how everything was 
working and how it needed to be dealt with.

I guess the moral of the story is....ALWAYS gobble err and out even if 
you don't think they're being used.

Thanks to everyone for their help.

Cheers,
Steve

On Mar 25, 2005, at 1:48 PM, Steve Butcher (Steve-O) wrote:
>
> I thought about this as well and originally dismissed the idea because:
>
> 1) when I run it at the command line (DOS prompt), there is no output.
> 2) when I run >java Launcher, it runs fine. It was not immediately 
> apparent to me why the buffer might fill up when it was called via 
> Tomcat v. "directly" (#1 notwithstanding).
>
> However, I have since attempted a few things with the original code 
> that lead me to believe that there is definitely *something* going. If 
> I change the previous play() method to:
>
>       public void play() throws Exception {
>            String command = "\"C:\\Program 
> Files\\VideoLAN\\VLC\\vlc.exe\" --extraintf=rc 
> --rc-host=127.0.0.1:9066 --rc-quiet \"rtsp://192.168.0.66/\" 
> :sout=#duplicate{dst=display}";
>            Process child = Runtime.getRuntime().exec( command);
> 	  int exit = child.waitFor();
>       }
>
> I can get the direct version (>java Launcher) to exhibit the same 
> behavior (VLC launches but hangs with "End Program" dialog).
>
> In any case, because you don't want your page to wait on a windowed 
> application, you shouldn't call Process#waitFor() anyway which doesn't 
> return until the windowed application is quit; however, it is an 
> interesting clue.
>
> Cheers,
> Steve


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: launching windowed program with Runtime#exec() under Tomcat 5.0

Posted by "Steve Butcher (Steve-O)" <st...@comcast.net>.
On Mar 25, 2005, at 1:31 PM, Markus Schönhaber wrote:

> Am Freitag, 25. März 2005 18:51 schrieb Steve Butcher:
> Does vlc produce any output on stdout/stderr? If it does, it may stop 
> when the
> output-buffer fills up. So you have to read Process#getOutputStream() 
> to make
> it continue.
>
> Regards
> mks


I thought about this as well and originally dismissed the idea because:

1) when I run it at the command line (DOS prompt), there is no output.
2) when I run >java Launcher, it runs fine. It was not immediately 
apparent to me why the buffer might fill up when it was called via 
Tomcat v. "directly" (#1 notwithstanding).

However, I have since attempted a few things with the original code 
that lead me to believe that there is definitely *something* going. If 
I change the previous play() method to:

       public void play() throws Exception {
            String command = "\"C:\\Program 
Files\\VideoLAN\\VLC\\vlc.exe\" --extraintf=rc --rc-host=127.0.0.1:9066 
--rc-quiet \"rtsp://192.168.0.66/\" :sout=#duplicate{dst=display}";
            Process child = Runtime.getRuntime().exec( command);
	  int exit = child.waitFor();
       }

I can get the direct version (>java Launcher) to exhibit the same 
behavior (VLC launches but hangs with "End Program" dialog).

In any case, because you don't want your page to wait on a windowed 
application, you shouldn't call Process#waitFor() anyway which doesn't 
return until the windowed application is quit; however, it is an 
interesting clue.

Cheers,
Steve



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: launching windowed program with Runtime#exec() under Tomcat 5.0

Posted by Markus Schönhaber <ma...@schoenhaber.de>.
Am Freitag, 25. März 2005 18:51 schrieb Steve Butcher:
>
> Then I build that into a class:
>
>   public class Launcher {
>        public static main( String[] args) throws Exception {
>             new Launcher().play();
>        }
>
>        public void play() throws Exception {
>             String command = "\"C:\\Program
> Files\\VideoLAN\\VLC\\vlc.exe\" --extraintf=rc --rc-host=127.0.0.1:9066
> --rc-quiet \"rtsp://192.168.0.66/\" :sout=#duplicate{dst=display}";
>             Runtime.getRuntime().exec( command);
>        }
>   }
>
> and invoked it from the command line:
>  >java Launcher
>
> and *that* works fine (VLC launches and the stream plays) which leads
> me to believe that there is nothing wrong with using Runtime#exec() in
> this particular instance AND the command is fine.
>
> HOWEVER, if I call the very same code in main() above from a JSP:
>
> <%
> 	new Launcher().play();
> %>
>
> VLC launches but hangs with an "End Program" dialog.
>
> IF I replace the command string with:
>
> command = "notepad.exe";
>
> Notepad launches just fine, directly from the code AND from a JSP which
> leads me to believe it is not a permission problem under Tomcat.
>
Does vlc produce any output on stdout/stderr? If it does, it may stop when the 
output-buffer fills up. So you have to read Process#getOutputStream() to make 
it continue.

Regards
mks

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: launching windowed program with Runtime#exec() under Tomcat 5.0

Posted by Jason Bainbridge <jb...@gmail.com>.
On Fri, 25 Mar 2005 13:14:45 -0500, Steve-O Steve Butcher
<st...@comcast.net> wrote:
> Dear Jason,
> 
> Thank you for the reply.
> 
> I'm not running Tomcat as a service. Originally, I was running it as a
> service and I even checked "Allow service to interact with desktop".
> After that failed, I tried running it simply using start.bat.

Would help if I learned to read... Just for curiosity try downloading
filemon from sysinternals.com to see if you do have some weird sort of
pernisions problem.

-- 
Jason Bainbridge
http://kde.org - webmaster@kde.org
Personal Site - http://jasonbainbridge.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: launching windowed program with Runtime#exec() under Tomcat 5.0

Posted by "Steve Butcher (Steve-O)" <st...@comcast.net>.
Dear Jason,

Thank you for the reply.

I'm not running Tomcat as a service. Originally, I was running it as a 
service and I even checked "Allow service to interact with desktop". 
After that failed, I tried running it simply using start.bat.

Cheers,
Steve

On Mar 25, 2005, at 12:56 PM, Jason Bainbridge wrote:

> How are you running Tomcat? As a service?  Try running it via
> startup.bat so it uses your credentials and then trying.
>
> Also try checking "Allow service to interact with desktop" if you are
> running the service as Localsystem (although I wouldn't recommend
> doing that for Production use, I'd create an account with the
> permissions you need and use that instead)
>
> Regards,
> Jason


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: launching windowed program with Runtime#exec() under Tomcat 5.0

Posted by Jason Bainbridge <jb...@gmail.com>.
How are you running Tomcat? As a service?  Try running it via
startup.bat so it uses your credentials and then trying.

Also try checking "Allow service to interact with desktop" if you are
running the service as Localsystem (although I wouldn't recommend
doing that for Production use, I'd create an account with the
permissions you need and use that instead)

Regards,
Jason


On Fri, 25 Mar 2005 12:51:22 -0500, Steve-O Steve Butcher
<st...@comcast.net> wrote:
> I've been looking through the archives and reading the posts regarding
> Tomcat and Runtime#exec(). Almost everything I've read about
> Runtime#exec() points to the javaworld article on the pitfalls of
> Runtime#exec(). However, most of the examples seem to be for commands
> or shell scripts where some kind of interaction with the command line
> is desired.
> 
> The problem I'm having is with launching a windowed program, VLC,
> (http://www.videolan.org/)
> 
> I'm running Tomcat 5.0 (NOT as a service), JDK 1.4.2_07, W2KPro, VLC
> 0.8.1
> 
> On the command line, the following works fine (VLC launches and the
> stream plays).
> 
>   >"C:\Program Files\VideoLAN\VLC\vlc.exe" --extraintf=rc
> --rc-host=127.0.0.1:9066 --rc-quiet "rtsp://192.168.0.66/"
> :sout=#duplicate{dst=display}
> 
> basically, the command line options tell VLC to open a simple interface
> for remote control, open an input stream over rtsp and send the output
> to the display. More complicated outputs (files, transcodings, streams)
> can be constructed.
> 
> Then I build that into a class:
> 
>   public class Launcher {
>        public static main( String[] args) throws Exception {
>             new Launcher().play();
>        }
> 
>        public void play() throws Exception {
>             String command = "\"C:\\Program
> Files\\VideoLAN\\VLC\\vlc.exe\" --extraintf=rc --rc-host=127.0.0.1:9066
> --rc-quiet \"rtsp://192.168.0.66/\" :sout=#duplicate{dst=display}";
>             Runtime.getRuntime().exec( command);
>        }
>   }
> 
> and invoked it from the command line:
> 
>  >java Launcher
> 
> and *that* works fine (VLC launches and the stream plays) which leads
> me to believe that there is nothing wrong with using Runtime#exec() in
> this particular instance AND the command is fine.
> 
> HOWEVER, if I call the very same code in main() above from a JSP:
> 
> <%
>         new Launcher().play();
> %>
> 
> VLC launches but hangs with an "End Program" dialog.
> 
> IF I replace the command string with:
> 
> command = "notepad.exe";
> 
> Notepad launches just fine, directly from the code AND from a JSP which
> leads me to believe it is not a permission problem under Tomcat.
> 
> I've even gone so far as to create a BAT file with the same command and
> launch it using:
> 
> command = "cmd.exe /C C:\\path\\to\\file\\vlc";
> 
> and that works on the command line, directly in the code but NOT via
> JSP.
> 
> I'm nearing my wits ends so if someone out there ever figured out a
> similar problem or has a suggestion, I'd appreciate it.
> 
> Thanks in advance,
> Steve
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 


-- 
Jason Bainbridge
http://kde.org - webmaster@kde.org
Personal Site - http://jasonbainbridge.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org