*
The moose likes EJB and other Java EE Technologies and the fly likes running native applications through web server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "running native applications through web server" Watch "running native applications through web server" New topic
Author

running native applications through web server

jay akhawri
Ranch Hand

Joined: Nov 28, 2004
Posts: 47
hi all,

This post is inspired by my search for remote shell applets. I am using tomcat 5.0 web container. the problem is that i want to run a native program on my server side system with a link like http://my_ip_address:service_port/nativeprog.jsp

to run programs like windows media player, notepad, regedit, cmd.exe etc. etc....

as well as all those core java programs that i executed on my command line.

right now i am just concentrating on running the programs...later i wud like to redirect a "JOB DONE" message to my client.

my first question is ...will container allow this...and if not....obviously i will have to code a brand new web server which will allow these services.now considering I AM NOT THAT GOOD in coding, though i am always open to learn how to code a "Personal web server" ---> will be a new challenge for the time being.

i am looking forward for an open discussion!!

bye
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Jay,

Because you�ve mentioned the use of an web server I suppose that you�re interested in running shell scripts or OS commands remotely, over the http protocol.
In theory any piece of java code that runs as a standalone application should run from within the container as well. Hence if a java program runs out of the container it should (at least in theory) run if it is "copied-and-paste" in a servlet or jsp; of course passing the right input parameters or setting the right environment variables etc. is mandatory, no matter whether the code is "outside or inside" of the container. Another obvious condition is that the user must have the right security privileges. Here I�m including file permissions as well. Usually the containers restrict the access to other external files, setting the java.security.policy in the startup script like this:

Now admitting that all these conditions are satisfied then we might reduce the problem to a much simpler one: runing external commands or shell scripts from within java applications. This might also have a very simple answer because java provides you a class that can help you doing this: java.lang.Runtime. Hence all you have to do is to write couple of pretty simple java classes that can use Runtime in order to take the command arguments and run a specified script or OS command. These are really easy to test too. Notice that Runtime has its own limitations but for what you'd like doing it should be good enough.
Next you�d probably like to wrap those classes within a command-servlet and deploy them to your favorite web server. Finally build and deploy the command-console pages in order to allow your web browsers to run the commands remotely (using the comman servlet).
Of course this is a very insecure and basic application and I�m sure you�d never consider it more than a practice-and-learn exercise.
Regards.


I think, therefore I exist -- Rene Descartes
jay akhawri
Ranch Hand

Joined: Nov 28, 2004
Posts: 47
hi valentin,

great suggestion.........so in theory i can run
---------------------------------------------------------
Process proc = Runtime.getRuntime().exec( SomeCommand );
---------------------------------------------------------

from within my jsp. asssuming that i am having privilage to do that on server system. great.

I have another question , i am not using my server on linux platform yet i want to access an external device (its a small device connected at com1 to which i access through hyperterminal, which shows blinking leds in a morse code format for some incoming text message, typed in hyperterminal), through my web server offcource.

how can i get around this problem(assuming i have privilage). how i will write the code which can access hardware ports? and send information through it.
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704

how can i get around this problem(assuming i have privilage). how i will write the code which can access hardware ports? and send information through it.

As a rule of thumb you should always ask yourself the question "can I do it in Java?" If the answer is yes, that�s it you have fairly good chances to achieve your goal.
However I doubt that your external device has a java interface but it might have a C interface though. Then build a small C application and try to see if JNI works for you. As you might see things could get little bit more complicated. Another tricky and fast solution would be to write a script that outputs the result of your command to a file and have the jsp first calling the script, waiting a little bit for the command to execute and finally reading that file. Sure it is against all specs and all odds, but you�re kind of using "dirty" programming here anyway.
Regards.
jay akhawri
Ranch Hand

Joined: Nov 28, 2004
Posts: 47
hi valentin,

i am truly understanding your point.

About half an hour ago i just checked around for a solution and after going through the search i found something called "Java Communication API"..i am still not sure but it seems it might be "one of the" solution.

and as for ur idea of using script file... we sure can also use a database which will store client arguments and after arguments are stored will triger a class which will perform our task(using runtime.exec() or using new ProcessBuilder class) and update the database table with result(done/not done)...then the updation will triger another event, which will triger another class to read the updated dtabase and ...to respond the client appropriately.

"I Just hope One day ... i can glow a bulb in my home from a remote place"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: running native applications through web server
 
Similar Threads
Caching Proxy Server Advice
What is meant by "Web Server"
Security error accessing url
Hotfix Code in tomcat container running groovy application
New to Java Web Application