• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Can't get Runtime.getRuntime().exec() worked properly under Tomcat

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to start another program (like cmd, notepad, explorer, etc...) from my web application on the same machine where Tomcat is running. For simplicity of an example, I created a ServletContextListener and put the following code in it to open mpv-player and play some media-file when the application context is being initialized. Similar code can be used in servlets and the "outer" program can be different of course.

So, when I run Tomcat from the console (or from IDEA while debugging), everything works fine: when my web-app is being initialized, mpv-player starts and plays the media. However, when I run Tomcat as a Windows service, everything changes: I can hear the sound of media playing, I can see the mpv-process started in Windows Task Manager, but the graphical interface of the program doesn't show up. Similar things happen when I try to 'exec' any other program (like notepad, explorer or whatever) - no exception is thrown, they just don't show up, but still created in the Task Manger.

Is there any chance to bring them into view? Imagine, I just want to click some link in my web-app to open Word, Excel or some local directory in the server machine (and it's obviously the same maching where the calling request is coming from).

P.S.
If you ask me what is the purpose of that? Well, I want to create a stand-alone java-program with GUI but don't want to sink myself into Swing routine with all those panes and layouts. Why not use web-app as a GUI-replacement for my java-program, since Tomcat is at hand all the time.



 
Ranch Hand
Posts: 68
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how this works in Windows, but in Unix like environments, there's an environment variable that needs to be set to tell a GUI application what windowing system it should connect to. Running a command without specifying that (and it's usually inherited from the places you're likely to start programs) will result in failure.

Of course, you're running windows, so I have zero clue if this has parallel considerations for that system, so if I just wasted your time, I apologize.

 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Windows service isn't attached to any display device. So yes, you can run those programs that way (except for Notepad which you can't) but there won't be anywhere you can see them.

And anyway the idea that the client will want to open Word on the server machine is a totally abnormal idea for a web app server like Tomcat. Normally the client and server are running on two different machines and allowing the client to run arbitrary code on the server is a security risk.

And why would you need a Swing GUI with many panes and layouts, just to cause other applications to run?
 
Toby Eggitt
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, implicit, in what I was pondering is where do you expect this stuff to show up even if it works?

Since you seem to be configuring this to run when the servlet context is initialized, I guess you are trying to run it on the server, but if you're expecting the output to show up for any client you're definitely misunderstanding what webservers do. And of course, the other side of the question is why on earth would you want to use a webserver to launch a "system" program?! Most servers run headless where nobody can see them anyway
 
Alex Escudero
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I understand that using web-app under Tomcat for opening local directories and running other programs on server machine is totally insecure, but in my case the client is supposed to be on the same machine where the server is running and I'm ready to take the risks. I was just pondering over the question if there any possibility to exploit web-app as a replacement for GUI java-program.

And why would you need a Swing GUI with many panes and layouts, just to cause other applications to run?



No, my application is supposed to do a little more than just cause other programs to run - the problem is that it's the only thing my web-app can't do at the moment. Also, it's not necessaryly ServletContextListener who does all those things.

So far, I just found only one possible workaround (though, not as elegant as I hoped for) - it's to create another java-program on the server. This program would play the part of an "executer" running as a daemon and listening some port for the instruction coming from the web-app. In this case web-app could delegate some tasks it can't do to the "executer".
 
Toby Eggitt
Ranch Hand
Posts: 68
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think that since you can hear the music, the approach is clearly workable. You just have to discover how to get a program that's started from a context that doesn't have a connection to your windowing system to connect successfully to your windowing system. I can tell you how to do that in Unix, but unfortunately have no clue in your environment. But at least it's a direction for investigation. Maybe go to a Windows admin forum and ask "how to start a gui program on my local system from a service" and see if they can help? Good luck!
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alex Escudero wrote:So far, I just found only one possible workaround (though, not as elegant as I hoped for) - it's to create another java-program on the server.



Here's another workaround: your problem is that a Windows service doesn't have a display screen attached to it. So, workaround: Don't run Tomcat as a service. Start it from the command line instead.
 
Saloon Keeper
Posts: 21115
131
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec() and similar functions to spawn and run an out-of-VM application are not recommended if you can avoid them. It's complicated, messy, and violates the write-once/run-anywhere paradigm.

In any event, Swing (and AWT and SWT) don't run in webapps. To do a GUI in a webapp, you use HTML. Tomcat would just be a useless extra layer of complexity here, adding nothing.

Further, as people have already noted, when the client is a different machine than the machine running Tomcat, a GUI application launched through Tomcat would not be accessible to the client. Windows is a single-user OS and doesn't have the mechanisms for a program to run on one desktop (the tomcat machine) but be displayed on another (the remote client). The Unix-style OS's, including Linux do have such a mechanism, but they run over a completely different TCP/IP port than Tomcat uses and require the remote user to be running an X server program (X windows "clients" are the hosts, and their "servers" are the remote clients!).

Windows does not come provided with an X server, and in fact, I know of no Windows X servers that are available non-commercially (that is, free). Plus, not all machines running Tomcat are running X clients. Windows machines almost universally are not, and my production webservers run no GUI at all - strictly terminal-mode. An X desktop is an extra 100M or so of RAM that I don't want to waste on a machine that only connects to a monitor when I'm trouble-shooting it.

In short, this ain't gonna work. You're better off simply using the GUI desktop to launch the app in the traditional way. If you want to run a multi-media app from Tomcat, you're going to have to write a multi-media web app yourself, not just borrow a desktop app.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!