If you live outside the United States, by submitting your email address you consent to having your personal data transferred to and processed in the United States.
I’m not sure which issue you want help with, issuing a cl command, or passing a javascript variable to the java class.
Issuing a cl command is pretty straightforward:
Process p = Runtime.getRuntime().exec(cmd);
System.out(new BufferedReader(
new InputStreamReader(p.getInputStream())));
System.err(new BufferedReader(
new InputStreamReader(p.getErrorStream())));
You need to output the input and error streams as shown to to flush the buffer, or the command might freeze.
I’m interested how you’re passing a javascript parameter to a java program.
I’m not sure which issue you want help with, issuing a cl command, or passing a javascript variable to the java class.
Issuing a cl command is pretty straightforward:
Process p = Runtime.getRuntime().exec(cmd);
System.out(new BufferedReader(
new InputStreamReader(p.getInputStream())));
System.err(new BufferedReader(
new InputStreamReader(p.getErrorStream())));
You need to output the input and error streams as shown to to flush the buffer, or the command might freeze.
I’m interested how you’re passing a javascript parameter to a java program.
Cheers!
Do you have sample code of call a CL from Java? I am fairly new to Java and I have not found any good examples of doing this…
Thanks!
/**
*
*/
package com.ashwin;
/**
* @author Ashwin
*
*/
public class WindowsExecCommand {
public static Process proc = null;
/**
* @param command
*/
public static void execCommand(String command) {
try {
proc = null;
String osName = System.getProperty(“os.name”);
String[] cmd = new String[3];
if (osName.equals(“Windows 95″)) {
cmd[0] = “command.com”;
cmd[1] = “/C”;
cmd[2] = command;
} else if (osName.contains(“Windows”)) {
cmd[0] = “cmd.exe”;
cmd[1] = “/C”;
cmd[2] = command;
}
Runtime rt = Runtime.getRuntime();
System.out.println(“Execing ” + cmd[0] + ” ” + cmd[1] + ” ”
+ cmd[2]);
if (command.endsWith(“.bat”)) {
proc = rt.exec(command);
} else {
proc = rt.exec(cmd);
}
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc
.getErrorStream(), “ERROR”);
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc
.getInputStream(), “OUTPUT”);
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println(“ExitValue: ” + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}finally{
proc = null;
}
}
}
package com.ashwin;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author Ashwin
*
*/
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + “>” + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
if (osName.equals(”Windows 95″)) {
cmd[0] = “command.com”;
cmd[1] = “/C”;
cmd[2] = command;
} else if (osName.contains(”Windows”)) {
cmd[0] = “cmd.exe”;
cmd[1] = “/C”;
cmd[2] = command;
}
It might be pointed out that neither “Windows 95″ nor “Windows” will be appropriate to run a CL program.
Regardless, CL programs are called by Java just like any HLL program. It doesn’t matter that it was compiled from CL.
Tom