java servlet, run exe file in a server to the client - java

I have a java servlet running in a server, plus an 'exe file' located in the same server,
i want , in respond to the client passed parameters to the servlet , to run the exe file located on the server and show it to the client , even a screen shot,,
any ideas??!! please help

You can use Process and Runtime classes
Eg :
Runtime r = Runtime.getRuntime();
Process p = r.getRuntime().exec("C:\\newfolder\\run.exe");
For taking screenshot refer to how to take sc in java
This way you can save the image and then send this image to user.
For sending image to client refer to how to send file from sever to client
these are.the pieces , you need to put them together
UPDATE 1 : to kill the exe you can use p.destroy() ( not a good implementation though, as it forcefully kills the process)
UPDATE2 : to check if the process( which is executing your exe) hence to check if the exe is running or not, you can refer to how to check if a process is running

You can run an external command in Java by the following code:
Process p = Runtime.getRuntime().exec("your_external_program_here");
You can pass in parameters as well, simply amend the above line to include what parameters you want to pass into the program.
To retrieve the 'output' of the process you need to get the input stream for the process:
InputStream output = p.getInputStream();
Note the input stream is the piped output of the process. You can then view the contents (advisable to use a buffered reader) like this:
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(output));
while ((line = reader.readLine()) != null) { ... }
Or alternatively you can look at ProcessBuilder which is easier to use :)

Related

SSH with JSch for multiple commands with intermediate Output

I am very new to JSch. I am trying to run few commands on a server which I am getting as input from some other system.
What I am doing is taking those commands and passing them as a parameter to a java method.
For Eg:
public String readFileFromPath(String server, String path,
String fileName);
Here first we have to cd to 'path', then we need to read some particular content from the file present on the path.
To implement this I did following :
Session session = sshOperations.getSessionWithTimeout(USER,server,SSHPORT,1000);
Channel shellChannel = sshOperations.getShellChannel(session);
InputStream in = new PipedInputStream();
PipedOutputStream consoleInput = new PipedOutputStream((PipedInputStream) in);
OutputStream out = new PipedOutputStream();
BufferedReader consoleOutput = new BufferedReader(new InputStreamReader(new PipedInputStream((PipedOutputStream) out)));
shellChannel.setInputStream(in);
shellChannel.setOutputStream(out);
shellChannel.connect(1000);
consoleInput.write(("cd "+path).getBytes());
// first While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
// execute second command
consoleInput.write("cat some.properties".getBytes());
// second While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
Now what I know is whenever I connect to that server I get a welcome text :
"You are using <serverName> server.
Please contact admin for any issues"
So, after the first while loop my cd command ran and it prints the message mentioned above. But, after this it waits for more output from the output stream (it is stuck at this point )and the output stream can't product anything until I run another command.
Somehow I want to exit from the first while loop without writing the logic for consuming the 2 lines(fixed lines). As for the next command I will not know how many lines will come as output in stream.
Please suggest the logic to the get the desired output i.e. I ran a command and some logic consumes it and then I get get to run another command and so on until all the commands which came as parameter are executed.
Is there any other way to achive the same?
Thanks
Do not use "shell" channel. The "shell" channel is intended to implement an interactive session (hence the welcome message), not to automate command execution.
To automate command execution, use "exec" channel. See Multiple commands through JSch shell.
Though you actually do not need multiple commands. There's no need for cd. Just use a full path in the cat command
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("cat " + path + "/some.properties");
channel.connect();
Though actually, if you want to read contents of files, use SFTP, instead of running console commands like cat. SFTP is a standardized API to access files over SFTP.
See SFTP file transfer using Java JSch.

Calling Python from Java (Tomcat6) as sub-process

I am trying to call a python script from a java/tomcat6 webapp. I am currently using the following code:
Process p = Runtime.getRuntime().exec("python <file.py>");
InputStream in = p.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader b = new BufferedReader(isr);
logger.info("PYTHON OUTPUT");
String line = null;
while ( (line = b.readLine()) != null){
logger.info(line);
}
p.waitFor();
logger.info("COMPLETE PYTHON OUTPUT");
logger.info("EXIT VALUE: "+p.exitValue());
I can't really see any output in the catalinia.out file from the python script and using an adapter library like jython is not possible as the script relies on several machine learning libraries that need python's Numpy module to work.
Help?
The explanation is probably one (or more) of following:
The command is failing and writing error messages to its "stderr" fd ... which you are not looking at.
The command is failing to launch because the command name is incorrect; e.g. it can't be found on $PATH.
The command is trying to read from its stdin fd ... but you haven't provided any input (yet).
It could be a problem with command-line splitting; e.g if you are using pathnames with embedded spaces, or other things that would normally be handled by the shell.
Also, since this is python, this could be a problem with python-specific environment variables, the current directory and/or the effective user that is executing the command.
How to proceed:
Determine if the python command is actually starting. For instance. "hack" the "" to write something to a temporary file on startup.
Change to using ProcessBuilder to create the Process object. This will give you more control over the streams and how they are handled.
Find out what is going to the child processes "stderr". (ProcessBuilder allows you to redirect it to "stdout" ...)

Detecting Graphical interface after calling Runtime.exec()

I'm trying to integrate with a legacy system. The legacy system uses dialog windows to report errors. It have no return codes at all except for the dialog windows. I start the legacy system with Runtime.exec().
Is there a way to detect if the executed program has spawned dialog windows or any other graphical interface? This solution is done in Windows and the executed program is an exe.
If the legacy system report errors in console, is possible get your erros.
Simply take the inputstream of error and do your reading.
Like this:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// any error message?
InputStream error = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(error);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(" ERROR >" + line);
I recommend to read: Runtime.exec() quirks
Hope this help.
You can use this JNA snippet to poll for windows started by your process.
AFAIK, you can only get the standard and error output streams from a process using the Java Process API.
So the solution i did, is to use the snippet code that #Gerrett Hall linked to. That snippet code check the active window before i run the command and save the name.
Then after a vile if the command have not returned check if the active window have changed. if it have, kill the process(Alt. send a return key global).
To get the info from the dialog i could use Ctrl + C to copy the content of the dialog and reading the paste buffer to copy the message in to the log. Have not figured out that part yet.
And yes this is a ugly hack but so is the legacy system to.

How can i run a .jar file in java

I'm making an update function for my project, it's working great, until i want it to restart, basically I download the new file and replace it with the old one, and then i want to run it again, now for some reason it doesn't wna run, and i don't get any error...
Here is the complete update class:
http://dl.dropbox.com/u/38414202/Update.txt
Here is the method i'm using to run my .jar file:
String currDir = new File("(CoN).jar").getAbsolutePath();
Process runManager = Runtime.getRuntime().exec("java -jar " + currDir);
It's not clear to me, why do you need to run the jar with a call to exec() . Given that you need to run the code in the .jar file from a Java program, you could simply run the main() method as defined in the jar's manifest, and capture its output - wherever that is.
Using exec() is OK when you need to call a program from the underlying operating system, but there are easier ways to do this if both the caller and the callee are Java programs.
Now, if your jar is gonna change dynamically and you need to update your program according to a new jar, there are mechanisms for reloading its contents, for instance take a look ath this other post.
The JavaDocs for the Process class specifically point out that if you don't capture the output stream of the Process and promptly read it that the process could halt. If this is the case, then you wouldn't see the process that you started run.
I think you have to capture the stream like this :
BufferedReader stdInput = new BufferedReader(new InputStreamReader(runManager.getInputStream()),8*1024);
BufferedReader stdError = new BufferedReader(new InputStreamReader(runManager.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
The exec function doesn't automatically lookup into the PATH to start a process, so you have to pass the complete path for the java binary.
You can do that by using the java.home system property, see this answer: ProcessBuilder - Start another process / JVM - HowTo?
No one here seemed to help me, so I went to ask my friend and I had it almost right. It abiously required the string to be an array.
solution:
String[] cmd = {"java", "-jar", currDir};
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
e1.printStackTrace();
}

Problem with copy files using cmd in Java

I'm trying to copy a bunch of files with a specific extension from one folder to another using the copy command, heres wat im doing,
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/c";
command[2] = "copy C:\\output\\html\\*.txt C:\\output\\";
ProcessBuilder copyFiles = new ProcessBuilder(command);
p = copyFiles.start();
p.waitFor();
the thing is, this code works fine for files less than some 5 or so, but just stops responding wen the number of files are more (even for 15 files) !! and the files are not copied either!!
I dont know what the problem is, will be glad if someone could help! :)
You're not reading the output the copy command is generating.
When spawning a child process using ProcessBuilder, output generated by your child process gets written to a buffer. If this buffer isn't read from, it eventually fills up. When it fills up, the copy command can't write any more to it and so is blocked by the operating system. It is then forced to wait until space is made in the buffer by reading from it.
I ran your code with 20 files and I found that it did indeed hang.
One way to solve your problem is to redirect the output from copy to NUL. Most of the output from copy is a list of all the files it has copied, which you probably don't care too much for. To do this redirection, modify the line that assigns to command[2] to the following:
command[2] = "copy C:\\output\\html\\*.txt C:\\output\\ >NUL 2>NUL";
However, if there is a problem copying files, you might not know about it if you do this.
Alternatively, you can read the output that the copy command generates. The following code sends it to System.out, but you can easily send it elsewhere or completely ignore it if you wish:
String[] command = { "cmd", "/c", "copy C:\\output\\html\\*.txt C:\\output\\" };
ProcessBuilder copyFiles = new ProcessBuilder(command);
copyFiles.redirectErrorStream(true);
p = copyFiles.start();
// The InputStream we get from the Process reads from the standard output
// of the process (and also the standard error, by virtue of the line
// copyFiles.redirectErrorStream(true) ).
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
do {
line = reader.readLine();
if (line != null) { System.out.println(line); }
} while (line != null);
reader.close();
p.waitFor();
I gave each approach a quick test with the same 20 files and neither approach hung.
EDIT: You might also want to try a 'hybrid' approach, by throwing away what copy writes to standard output (e.g. the list of files it's copying) but using the second approach to read in what it writes to standard error (e.g. error messages). To do this, you'd add the >NUL, which redirects the standard output of copy to NUL, but you wouldn't add the 2>NUL, since that redirects standard error to NUL.

Categories