Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rekha Pai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also try to use Code Tags for a better look and feel!
 
Rekha Pai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
%>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic