wood burning stoves 2.0*
The moose likes Servlets and the fly likes running an executable file on the server by client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "running an executable file on the server by client" Watch "running an executable file on the server by client" New topic
Author

running an executable file on the server by client

karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi guys,
can anyone please tell me how can a client run an executable file on the server.right now in my
application,the client wishes to run an executable file on the server,he is able to access the server,but rather than running the executable file,he gets an option for saving it on his local machine,so how can i make the executable run on the server itself and not save on the client machine.
i had some code for running/opening files on the server by client but all of these files open in the web browser and since an executable file cannot open in a web browser therefore it only gives an option of saving it,but i want the executable to run on the server.
any help/comments would be greatly appreciated.
thanks in advance for your help
karan
Mayer Salzer
Greenhorn

Joined: Apr 01, 2002
Posts: 20
Hi,
If I understand you correctly you want the client's request (via the servlet) to kick off (or run) an "exe" file on the server. You can do that by calling the "Runtime" class' "exec" method.
Example (in windows environment):
Runtime.getRuntime().exec("calc.exe");
would start the claculator . . . You can do the same for your own "exe" file provided you include the path for it . . .
[ June 07, 2002: Message edited by: Mayer Salzer ]

Sun Certified Programmer for Java 2 Platform
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi there,
thanks for posting a reply to my question.actually i knew about this command Runtime.getRuntime().exec("calc.exe"); but the problem is that when i invoke this command with the file's url like this
Runtime.getRuntime().exec("http://servers IP address/the path on the server/filename.exe"),then rather than executing the file,it gives an option for saving it on my computer(the clients machine)..
so please suggest me whats going wrong here..i appreciate your help in this matter..
cheers
karan
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Originally posted by karan, chopra:
hi there,
thanks for posting a reply to my question.actually i knew about this command Runtime.getRuntime().exec("calc.exe"); but the problem is that when i invoke this command with the file's url like this
Runtime.getRuntime().exec("http://servers IP address/the path on the server/filename.exe"),then rather than executing the file,it gives an option for saving it on my computer(the clients machine)..
so please suggest me whats going wrong here..i appreciate your help in this matter..
cheers
karan


Are you making the call to exec on the client or server side? The Runtime.getRuntime().exec(String) should be called in the servlet code.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Mayer Salzer
Greenhorn

Joined: Apr 01, 2002
Posts: 20
You problem is the path to the "exe" file. Since your invoking it on the server side, you need your class (servlet) to execute it directly, instead of executing it on the client side (downloading it through the "http . . " path) and then being at the mercy of the browser as to whether the file is opened, saved, etc.
Instead of using the virtual directory Runtime.getRuntime().exec("http://servers IP address/the path on the server/filename.exe")
You should use the actual physical directory where the "exe" is located Runtime.getRuntime().exec("/physical/location/of/your/filename.exe") (on unix) or Runtime.getRuntime().exec("C:\\physical\\location\\of\\your\\filename.exe") (on windows) I'm sure you get the idea by now . . .
[ June 07, 2002: Message edited by: Mayer Salzer ]
[ June 07, 2002: Message edited by: Mayer Salzer ]
[ June 07, 2002: Message edited by: Mayer Salzer ]
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi guys,
thanks a lot for all your help.now i am able to run an executable file on the server but i have a slight problem.The problem is as follows,as i said now i am able to run an executable file on the server,but i want to run an executable file made from a C++ program(its an executable file generated by the borland C++ compiler),and for some reason the code that i wrote to execute the files does not work for this C++ executable.i dont know why..however when i double click the C++ executable directly on the server then it executes nicely without any problem.
So my question is that why is this C++ executable file not running from my java code?it is an executable and should also run like any other executable file,but it isnt..
any ideas guys?would appreciate it..
take it easy
karan
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
My guess is that the C++ program has some dependency on the "current" directory. When you execute it directly, the current directory is the location of the exe file, but from a server exec the "current" directory is elsewhere.
Bill
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi there,
well but when i try to execute that C++ executable from the client machine then i see something happening(as in a DOS window for the C++ executable pops up but only for a split second and then dissapears),so i was thinking that maybe the executable is infact running but then on checking i found that its NOT..wonder why?
if the reason for that is as stated by william then is there any solution for it?i mean there must be a way to know the current directory of the program..any suggestions folks?
thanks a lot
karan
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
You don't have any control over what the servlet engine considers the "current" directory. What you can do is execute your C++ program indirectly through a batch file. The batch file can CD to a different directory (which only applies to that process). So - write a batch (.bat or .cmd in Windows) file that has the absolute location of the executable program.
Its all in chapter 10 "Connecting to Legacy Programs" in my servlets book. Includes a cool utility for testing exec processes outside the servlet environment(gee, I forgot I wrote that! What a cool utility.)
Bill
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi william,
i think the problem is something else.actually i tried running my other C++ executable by the same methods(with and without using the bat file) and it worked.but i think there is different problem with the C++ executable that i want to run,the probelm is that this C++ executable produces some output files after it has successfully run in the same directory in which it is present(ie on the server).So,i think that the problem is that when i try to run the executable from the java front-end from the client machine,then the executable on the server runs allright but there is no output files generated on the server..but why is this happening is beyond my conprehension..
does this mean that the C++ executable cannot create output files on the server?kindly help me out..would really appreciate it..
thanks for ur time
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi william,
i figured out wat the problem was and now the C++ executable runs fine and the output files are also generated but now there is a slight problem.for some reason(again beyond my comprehension )the output files are being copied in the bin directory of tomcat(thats the server i am using for the servlets) and also for the C++ executable to run fine i have to copy the input files for the C++ executable in the bin dir of tomcat(though when i run the executable directly on the server by double clicking it the input files should be in the same dir as the executable and not the bin of tomcat of course).
is there any way by which this probelm can be fixed?i mean wat i want is that the input files and output files should be in some different dir than the bin of tomcat so that more than one users can execute their respective C++ executable files in their respective dirs.
any ideas?
take it easy
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
i was thinking that maybe we can modify the config files of tomcat or something..is that possible?
take it easy
karan
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
If the output files are ending up in tomcat\bin then the problem is what I originally said - your C++ program has a dependency on what is the "current" directory. When you run by double-clicking, that sets the "current" directory for that task to the directory the exe file lives in.
Have you tried my batch file suggestion?
The exec process is very flexible so you could substitute target directories into the batch file, giving different directories for different users.
Bill
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi william,
thanx for replying to my query..well i have indeed tried your batch file suggestion,ie,i created a batch file in which i wrote pathofthedir\filename.exe thats it.bit ihad the same problem,ie,the files were being read from the bin dir of tomcat only and the output files were generated in the bin dir of tomcat.i didnt quite understand what u meant when u said the following:
"The exec process is very flexible so you could substitute target directories into the batch file, giving different directories for different users"
can u please elaborate on it.qould really appreciate it.
thanks a lot
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi william,
you were absolutely right in saying that the C++ executable has a dependency on the current working dir and it assumes the current working dir to be different than what it actually should be,i realised this just now when i tried to run the C++ executable from the client machine itself but not by double clicking it but by calling it frmo the java interface,now also the executable requires the input files to be in some different dir than the one in which itslef is.and the output files are also generated in that dir(ie dofferent than the dir of the executable file).
so please tell me what should i do,as in what were you saying about making a batch file.please tell me what should i write in the batch file,right now i have just written the following in the batch file
C:\foo\C++executable.exe
but on running this batch file on the client machine from the java interface the output files are generated in different dir and not in the same dir as the C++ executable file.so what more should i write in the batch file?please help me out..would appreciate it..
thanks a ton in advance
karan
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
Your batch file should look like this


And then use runtime.exec()


Shubhrajit
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
Originally posted by Shubhrajit Chatterjee:
Your batch file should look like this


And then use runtime.exec()

hi there,
thanks a lot for all your help guys..now the application is working,javaranch di it again!!keep up the good work!!
cheers
karan
 
jQuery in Action, 2nd edition
 
subject: running an executable file on the server by client
 
Similar Threads
Using Servlet to send down a file
Help Needed in Applets....Urgent
Type of J2EE application
Serializing arguments to remote methods
running an executable file on the server by client