aspose file tools*
The moose likes Servlets and the fly likes Create a process from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Create a process from Servlet" Watch "Create a process from Servlet" New topic
Author

Create a process from Servlet

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Hi Ranchers,

I am writing a servlet, in which I need to execute a Batch file.

Now I must tell you the how I wrote batch file. I am using Windows XP and RAD(Rational Application Developer). My project is of type "Dynamic Web Project". Now If I have created a batch file to execute something, then where should I place this batch file so that servlet can take it directly, If I call like this



Here the name of batch file is "batch.bat". Initially I have tried to put in main project itself. But it's not working.

Can anybody throw some light on this one.

Early helps would be appreciable.
Thanks.


The Best way to predict your future is to create it - Every great individual common man
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

For your convenience I am attaching the sample code for your inspection.




I am working on Windows XP and Java Compiler is 1.4.

Do let me know, If you need any kind of other clarifiactions.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You'll want to fully qualify the path to the batch file on your system.

Your command line Java programs knows what the current working directory is and finds the file using it.

In a Java web app the current working directory is going to be whatever directory the user was in when they started the server.
If the server is running as a Unix daemon or a Windows service, things get even fuzzier.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Ben Souther:

If the server is running as a Unix daemon or a Windows service, things get even fuzzier.



Why it is so? Can you please explain some more about it..And how can we prevent this from our application.?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ankur Sharma:



Why it is so? Can you please explain some more about it..And how can we prevent this from our application.?


To prevent it in your application, use a fully qualified path to your batch file.

"Present working directory (PWD)" is a command line concept.
With a command line (or shell), you CD to a particular directory.
Once there, that is your PWD. The DIR (or ls in Unix) command will show you all the files in that directory.

From a GUI application or (in this case) a server application, the user is never 'in' a particular directory. They're accessing your application remotely, over HTTP. The PWD will be whatever directory the administrator was in when she typed the command to start the server.

With a Windows Service, the server starts up when the the operating system starts. What would the PWD be in that case?


So, to make life simple, and your app more robust, don't assume to know where the PWD is. Provide a way to configure the exact location of the file that you want to access (such as a context-init-param).
[ August 29, 2007: Message edited by: Ben Souther ]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Ben Souther:


To prevent it in your application, use a fully qualified path to your batch file.

"Present working directory (PWD)" is a command line concept.
With a command line (or shell), you CD to a particular directory.
Once there, that is your PWD. The DIR (or ls in Unix) command will show you all the files in that directory.

From a GUI application or (in this case) a server application, the user is never 'in' a particular directory. They're accessing your application remotely, over HTTP. The PWD will be whatever directory the administrator was in when she typed the command to start the server.

With a Windows Service, the server starts up when the the operating system starts. What would the PWD be in that case?


So, to make life simple, and your app more robust, don't assume to know where the PWD is. Provide a way to configure the exact location of the file that you want to access (such as a context-init-param).

[ August 29, 2007: Message edited by: Ben Souther ]


Thanks Ben,

I will try to move in the same direction as you have suggested. Will let you know if I stuck somewhere.
Dilraj Singh
Greenhorn

Joined: Apr 22, 2007
Posts: 8
why not to just cache it when application server is starting up and then use it?

let me know if i did not understand the basics here.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Your approach of trying to read both std out and err out in the Thread which starts the Process



could lead to mysterious hangs. The amount of character stream output which the operating system will buffer is not something you can rely on. The Process might hang waiting for you to consume one stream while your code is looking at the other stream. This is discussed in the JavaDocs for Process.

The solution is to start a Thread for each stream.

Bill
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Dilraj Singh:
why not to just cache it when application server is starting up and then use it?

let me know if i did not understand the basics here.


Cache what when the server starts up?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by William Brogden:
Your approach of trying to read both std out and err out in the Thread which starts the Process



could lead to mysterious hangs. The amount of character stream output which the operating system will buffer is not something you can rely on. The Process might hang waiting for you to consume one stream while your code is looking at the other stream. This is discussed in the JavaDocs for Process.

The solution is to start a Thread for each stream.

Bill


Thanks Bill, But can you tell me how it is creating some problem if we are working on both out and error stream simultaneously.

I mean can you post the link related this issue. Can you please throw some more light on this issue.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
can you tell me how it is creating some problem if we are working on both out and error stream simultaneously.


But you are not reading both streams simultaneously - that readLine() method call waits for a complete line or the stream to be closed. Depending on the volume of output etc, this code may work sometimes and not others.

Here is an old review article on exec() pitfalls.


Bill
Dilraj Singh
Greenhorn

Joined: Apr 22, 2007
Posts: 8
Originally posted by Ben Souther:


Cache what when the server starts up?


Originally posted by Ben Souther:


Cache what when the server starts up?


I understood the problem is path of batch file.

If this is the case I would avoid to fill init paras of servlet as this is not servlets concern which operation system is being used,

I would make some separate property file containing paths to batch file like PATH_IN_UNIX,PATH_IN_WINDOWS and TYPE_OF_OPERATING_SYSTEM;and caching this property file in bootstrap process.

Also separate thread is also concern as William points out rightly.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Dilraj Singh:

If this is the case I would avoid to fill init paras of servlet as this is not servlets concern which operation system is being used,


Originally posted by Ben Souther:
So, to make life simple, and your app more robust, don't assume to know where the PWD is. Provide a way to configure the exact location of the file that you want to access (such as a context-init-param).


[ August 30, 2007: Message edited by: Ben Souther ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Create a process from Servlet