001: /*
002: * To change this template, choose Tools | Templates
003: * and open the template in the editor.
004: */
005:
006: package it.businesslogic.ireport.util;
007:
008: import java.io.File;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.OutputStream;
012: import java.io.PrintStream;
013: import java.util.ArrayList;
014: import java.util.List;
015: import java.util.logging.Level;
016: import java.util.logging.Logger;
017:
018: /**
019: *
020: * @author gtoffoli
021: */
022: public class JavaProcessExecuter {
023:
024: private List<String> classpathEntries;
025: private List<String> parameters;
026: private String mainClass = null;
027:
028: private String javaExecutable = null;
029:
030: public List<String> getClasspathEntries() {
031: return classpathEntries;
032: }
033:
034: public void setClasspathEntries(List<String> calsspathEntries) {
035: this .classpathEntries = calsspathEntries;
036: }
037:
038: public List<String> getParameters() {
039: return parameters;
040: }
041:
042: public void setParameters(List<String> parameters) {
043: this .parameters = parameters;
044: }
045:
046: public String getMainClass() {
047: return mainClass;
048: }
049:
050: public void setMainClass(String mainClass) {
051: this .mainClass = mainClass;
052: }
053:
054: public String getJavaExecutable() {
055: return javaExecutable;
056: }
057:
058: public void setJavaExecutable(String javaExecutable) {
059: this .javaExecutable = javaExecutable;
060: }
061:
062: public JavaProcessExecuter() {
063: parameters = new ArrayList<String>();
064: classpathEntries = new ArrayList<String>();
065:
066: String java_home = System.getProperty("java.home");
067: if (java_home != null) {
068: javaExecutable = java_home + File.separator + "bin"
069: + File.separator + "java";
070: if (System.getProperty("os.name").toLowerCase().contains(
071: "windows")) {
072: javaExecutable += ".exe";
073: }
074: }
075: //else throw new Exception("Unable to locate the java home");
076: }
077:
078: public void dumpOutput(final InputStream in) {
079: Runnable r = new Runnable() {
080:
081: public void run() {
082: try {
083: String s = "";
084: byte[] buffer = new byte[1024];
085: int count = 0;
086: while ((count = in.read(buffer)) >= 0) {
087: if (count > 0)
088: s += new String(buffer, 0, count);
089: }
090: System.out.println(s);
091: System.out.flush();
092: in.close();
093: } catch (IOException ex) {
094:
095: }
096: }
097: };
098:
099: new Thread(r).start();
100:
101: }
102:
103: public void execute() throws IOException {
104: try {
105: String classpath = "";
106: for (String cpitem : getClasspathEntries()) {
107: classpath += cpitem + File.pathSeparator;
108: }
109:
110: List<String> cmdList = new ArrayList<String>();
111: cmdList.add(javaExecutable);
112: cmdList.add("-Xmx512m");
113: cmdList.add("-cp");
114: cmdList.add(classpath);
115: cmdList.add(getMainClass());
116: cmdList.addAll(getParameters());
117:
118: String[] cmdArray = new String[cmdList.size()];
119: Process process = Runtime.getRuntime().exec(
120: cmdList.toArray(cmdArray));
121:
122: // Redirect the output stream...
123: // Get the input stream and read from it
124: dumpOutput(process.getErrorStream());
125: dumpOutput(process.getInputStream());
126:
127: process.waitFor();
128:
129: } catch (InterruptedException ex) {
130: ex.printStackTrace();
131: }
132:
133: }
134:
135: }
|