tags:

views:

281

answers:

5

Is it possible to allow a client to click a button, and then trigger a batch file to run on the server? I am able to run the batch file on the server from within server machine, but I want to run it on the server, but launched from the client machine from a website. How would I do this?

+2  A: 

How your client is interacting with your server is the question. Anyways, I assume a servlet application, it may be considered a web app though but you will have a browser as a client wouldn't you.

So, upon receiving of that particular request, just run that method which suppose to execute the batch file. Where is the problem. OR we can elaborate if you can tell the architecture of your application, and how client is interacting with the server.

Adeel Ansari
+1  A: 

Yes you can.

Assuming that its a web application you can just have a JSP page / Servlet execute the same operation you were running on server machine.

The only problem is that you may have to execute it asynchronously (i.e your servlet puts in the request for execution of the batch task) & returns back to update the UI. This is especially important if your batch task is a long running one.

gnlogic
A: 

Sounds like what you're really looking for is the exec call. Such as:

    Runtime rt = Runtime.getRuntime();
    try {
        rt.exec("ifconfig");
    } catch (IOException e) {
        e.printStackTrace();
    }

Call this from your button click or whatever it is you're triggering the event with. I'm not sure how to receive anything back but that's another matter.

Scott Bennett-McLeish
This won't work if it's called from the GUI because, it'll start up the process on the client and not the server.
Outlaw Programmer
Actually, it will run on the server, in the interpretation of the question it says 'when a button is clicked on a website' and not in say a java desktop client. Taken to mean that this code would then be executed by a servlet or similar.
Scott Bennett-McLeish
A: 

You can invoke the batch file from a servlet using ProcessBuilder.

 ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory("myDir");
 Process p = pb.start();

http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html

Edit: You can always read stdout, stderr and return code of the process using p.getOutputStream() ,p.getErrorStream(). If you need to write on stdin use p.getInputStream().

Kalecser
A: 

Long before there were "Application Servers", web servers have been running server-side scripts based on client-side input. This is what CGI is all about. Needless to say, when people started developing application servers, they were sure to include in them some way of accessing the CGI capabilities that web developers had become used to.

So the answer to your question is that you can almost certainly run a server-side script based on client-side input. The problem with answering your question is that you haven't really told anyone what your server side is. Knowing what operating system your application server is on is also important in giving you a complete answer.

Since your question is posted under the Java tag, everyone is assuming that your server side is some J2EE application server. If that is the case, then all the answers so far give good clues to what you need to do. (If you are not running on an application server, then you need to look into the CGI capabilities of your web server (and change your tag to CGI).)

Scott's answer tells you the old way to execute your script or any executable. Kalecser's answer tells you the newer "kosher" way to do it. gnlogic and Vinegar sugest your application server might provide built-in API for doing this.

gnlogic also brings up the point of synchronization. I can't comment on an unknown application server API, but Runtime.exec and ProcessBuilder both create an asynchronous process. Dealing with an async process is a totaly different question.

Mike