How to invoke main method of java program from a jsp? - java

I have a JSP from which I'm getting the input from the users. Now, I'd like to pass the input that get from this JSP as arguments to the main method of my java program and run the program from this JSP. Some help please!
Thanks in advance!
Ravilla

Bad idea. What are you trying to achieve? Even if you do that, by calling main() as any other arbitrary method, or by invoking Runtime or something, What you will achieve is a program running on server. What the client would do with that.
JSP/Servlet/Java Web programming is a request/response based stuff.
If you want to compute something using that program and planning to use the output as a response. Then why not include that particular piece as business component or something and call that in a usual way. May be in a separate thread, if its a long process, let the user interact with the application and notify user once it is done.

I had to program something like this a year ago and I ended up making it a webservice. This allowed my app to be called from a JSP. I think you should try this.

you need to execute command at OS level.
Process p = Runtime.getRuntime().exec("java -classpath "..." SomeClassContainingMain ...other arguments);
//you need to consume the outputs of the command if output/error is large otherwise the process is going to hang if output/error buffer is full. and create a seperate thead for it (not created here).
log.debug("PROCESS outputstream : " + p.getInputStream() );
log.debug("PROCESS errorstream : " + p.getErrorStream());
p.waitFor(); // Wait till the process is finished

Related

About Java ProcessBuilder's stream

first of all.. apologize what i can't upload code..
(thirdparty program is sdelete)
I'm work with third Party program on java ProcessBuilder.
like..
ProcessBuilder("cmd","/C","thirdParty.exe")
or
ProcessBuilder("thirdParty.exe");
and then.. I make two thread that using scanner that get ProcessBuilder's stream.
(one is input Stream print , other one is error Stream print)
When I execute this program.
It seems like fine at opening part. thirdParty program's opening Messages show up in console and .. according to priority.. Process percentage must show up, but doesn't..
Percentage not show up, but it's not hangs or frozzen..
thirdparty work's fine. Just Scanner can't get InputStream data.
(if not with java process, namely if i just execute program.. percentage show up properly)
and.. finally when thirdParty process finished.. all of percentage data show up at once!
is anyone know about this phenomenon?
advice please..

commande console form java

i'm trying to execute 2 commands via java programme with process
Process p = Runtime.getRuntime().exec(command1);
Process p2 = Runtime.getRuntime().exec(command2);
the problem is that the first one is ok but the seconde on cant be established
it is always bloqed in waitfor()
You might be running into the dreaded "need to empty the streams" problem. See When Runtime.exec() won't for details on it.
Also in the same article is some info on other traps you can run into if you're treating getRuntime().exec() like the command line.
When running an external procss that prints anything to stdout/stderr, you should read what it writes - otherwise it will block once it's buffer fills up.
you basically needs a thread to read from stdout and a thread to read from stderr of each process.

How to Send a Password to Process in Java

I am launching a process from java to run a command for me. This process runs for a little while, then needs a password to continue. Now I know that I can write to the in stream of the proces, but I am not quite sure how to detect when I need to write to it.
Possible solutions:
Is there a way that I can detect that the process is blocking?
Can I just write to the standard in immediately after executing the command and when the process hits a point when it needs it, it can just read from it?
Any other ideas?
It is not necessary to detect if the child process is blocking or not. If the child process is designed to block until input is provided to it via stdin, it will block until such input is provided.
It it necessary to keep in mind that the standard input, output and error buffer sizes are limited, and therefore it would be necessary for the child process to process the contents of the input buffer, and for the parent process to process the contents of the output and error buffers as soon as possible. Not doing so will result in the child process hanging.
Maybe you should get around the runas problem but not using runas. Google found me this: http://www.source-code.biz/snippets/c/1.htm Lets you pass your password at runtime....

java Runtime process - check if waiting for input

I wish to create a process using java's runtime:
for example. Process proc = Runtime.getRuntime().exec("cmd");
Then, I want to somehow wait for the process until it is in 'ready for input' state, which will verify it has finished all of its work. Anyway on how to do it?
One thing that is possible is echoing "---finished---" and checking if this line was written using the stdin of the process. But I dislike the idea.
Is there any better (more formal) approach to do this?
By the way, I need this effect as I descriobed it. I want to wait before writing new commands to the batch file.
Thank you.
Is there any better (more formal) approach to do this?
No. Java provides no way to determine if another program is waiting for input. Indeed, I don't believe that this is possible to determine this at all (for Linux/UNIX) without digging around in kernel memory. (And that would be a really bad idea ... IMO).
Your best bet is to check the external program's output for some text that indicates that a batch has just finished ... and hope that you don't get a false positive. (Or just queue up the batches without waiting for them to complete, though it sounds like that won't be a solution for you.)
Here are a couple ideas. They hinge on whether or not the program you're running expects to read and write from stdin and stdout.
There's no need to wait for a "ready for input" state once you've launched the process. If you need to send commands, use getOutputStream and write them. If the other program is still getting itself ready, the data you've written will happily sit in a buffer until the program reads from stdin.
Using ProcessBuilder will let you easily control arguments and environment. Example direct from JavaDocs:
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(new File("myDir"));
Process p = pb.start();
Then, to read or write:
OutputStream out = p.getOutputStream();
out.write("some useful data".getBytes());
out.flush();
Your question seems like a really complicated way of obtaining the final required result.
First, Java has the capacity to write to arbitrary files. Why not use that capability in a single threaded manner? Calling flush will ensure the write is actually performed...
If you really need concurrency for some reason, use a thread over a process. Then, make the thread do what it needs to do and nothing more. Have the parent thread join() the child thread. When the child thread is finished, your parent thread will resume.
Finally, calling available() on the output stream or input stream can tell you have much data Java can read before blocking. This isn't what you want exactly, but can be adapted to work I'm guessing.

Try to make a process that can TSKILL itself

So I have a problem with a process that I am running, whenever I try to stop it using process.destroy(), it does not stop.
I want to create a file (ProcessHandler) that extends Process and do the following:
ProcessHandler process = (ProcessHandler)Runtime.getRuntime().exec("cmd.exe /c \"java net/com/codeusa/Server 43594\"");
So, my problem is trying to convert Process to ProcessHandler where I can override the destroy() command, to make it TSKILL itself. I have figured out how to do everything but when I try the above like of code, I get a ClassCastException..
Anyone have an idea how I can make these be compatible. BTW the exec(String) command returns an instance of Process.
Firstly, if you are using Java 5 or later, I would recommend using ProcessBuilder instead of Runtime.getRuntime().exec(). For one thing, you don't have to worry about quoting arguments. Each separate command-line argument is a separate parameter. For example:
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/C", "java net/com/codeusa/Server 43594");
Process process = builder.start();
When starting a process using ProcessBuilder or Runtime.getRuntime().exec(), it's entirely up to the JVM to instantiate and return a subclass of Process of its choice, and there's no way to influence its decision. I assume your ProcessHandler class is one you've written yourself (I can't find a Java API class with that name). It might subclass Process, but even if it does there's no way for the JVM to return an instance of it when you use ProcessBuilder or Runtime.getRuntime().exec(). So your line of code above is guaranteed to throw a ClassCastException, assuming it doesn't throw some other exception.
I have had some experience in the past of processes that didn't respond to destroy() methods. Usually this was because the standard output or standard error being written by the process wasn't being read, and the process had ground to a halt because one or more of its I/O buffers had filled up. Does the process above write anything to its standard output or standard error, and if so, are you reading it?
Reading both the standard output and standard error streams is easier with ProcessBuilder: if you add the line builder.redirectErrorStream(true); between the two lines above, then you only need to read from the process's standard output. If you're stuck with Java 1.4 or earlier and Runtime.getRuntime().exec(), you'll have to set up two different objects in two different threads, one that reads from each stream.
I'm not sure what you are trying to achieve with your ProcessHandler class - you haven't provided the source code for it. Besides, I've never had the need to kill a process more forcibly than by using the destroy() method.
I figured out a whole new thing!! When I call the destroy() method for process, it destroys the cmd.exe process.. but I replaced cmd.exe with "java" and now when I call destroy(), the java.exe process terminates.. HURAY

Categories

Resources