File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to run a java class which executes a command from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to run a java class which executes a command from JSP" Watch "How to run a java class which executes a command from JSP" New topic
Author

How to run a java class which executes a command from JSP

Rekha Pai
Ranch Hand

Joined: Oct 30, 2008
Posts: 35
I have created a java class below.
If I use the static method ExecCmd of this class to copy a file, in jsp it does not work. Can anybody help me to solve this.

package Beans;
import java.io.*;

public class CmdExec {

public CmdExec() {}
public static void ExecCmd(String [] cmdline) {
try {
String line;
Process p = Runtime.getRuntime().exec(cmdline);
p.getInputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception err) {
System.out.println("EXEC failed: " + err.toString());
err.printStackTrace();
}
}
}
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rekha Pai:
I have created a java class below.
If I use the static method ExecCmd of this class to copy a file, in jsp it does not work. Can anybody help me to solve this.



Simply "it does not work" really does NOT give enough information. Can you tell us how exactly you are communicating between your JSP and the Java class? What values you are passing?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Also try to use Code Tags for a better look and feel!
Rekha Pai
Ranch Hand

Joined: Oct 30, 2008
Posts: 35
Originally posted by Raghavan Muthu:
Also try to use Code Tags for a better look and feel!


I have kept the CmdExec.class file in WEB-INF/classes.
I have created a String [] with {"cp", "x.doc", "y.doc"}
and passed this String array in jsp file using CmdExec.ExecCmd(String[])
This does not work through browser. It works fine on commandline with a main function written in java class.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rekha Pai:


This does not work through browser. It works fine on commandline with a main function written in java class.


Browser is also a client. So it should not really matter If I am right.

Can you paste your jsp code here? -- By using Code Tags
Rekha Pai
Ranch Hand

Joined: Oct 30, 2008
Posts: 35
Originally posted by Raghavan Muthu:


Browser is also a client. So it should not really matter If I am right.

Can you paste your jsp code here? -- By using Code Tags


<%@ jsp:import="Beans.CmdExec" />
<%!
String [] cmdline = {"cp","x.doc","y.doc"};
%>
<%
CmdExec.ExecCmd(cmdline);
%>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run a java class which executes a command from JSP
 
Similar Threads
the exec command is not able to deal with space some time
nslookup
how to r.exec("java filename");
try to run commands in command prompt by using java
Calling main method of one program from another program