Sidebar

Is there a way to have QIE run an external executable or batch file?

+1 vote
823 views
asked Nov 6, 2013 by matt-w-2627 (3,220 points)
Is it possible, as apart of a channel, to have QIE run an executable? Notepad.exe or VPNClient.exe for example?

3 Answers

+1 vote
 
Best answer
Yes, using the Java Class ProcessBuilder,  (http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html)

each parameter you want to pass, must be provided separately. In this example below, I am calling a VPN program and having it connect to a specific VPN Profile.

here is an example:

var program1 = "C:\\Program Files (x86)\\Cisco Systems\\VPN Client\\vpngui.exe";
var param1_1 = "-c";
var param1_2 = "-sd";
var param1_3 = "VPNProfileName";
var processBuilder = new java.lang.ProcessBuilder(program1, param1_1, param1_2, param1_3);
processBuilder.redirectErrorStream(true);
processBuilder.start();
answered Nov 6, 2013 by matt-w-2627 (3,220 points)
selected Nov 6, 2013 by matt-w-2627
0 votes

In addition, you will want to redirectOutput, redirectError, monitor the process for when it stops, and force stop the process if it has run too long.  Below are some of the extra checks you can make:

var pb = new java.lang.ProcessBuilder(
   "C:\\ImageMagick\\magick.exe",
   "C:\\qie_files\\outbound\\QLTest\\"+qie.getVariable('filename')+".pdf",
   "C:\\qie_files\\outbound\\QLTest\\"+qie.getVariable('filename')+".tiff");

pb.directory(new java.io.File("C:/Users/qiedev/Documents"));
pb.redirectErrorStream(true);
pb.redirectOutput(java.lang.ProcessBuilder.Redirect.INHERIT);
pb.redirectError(java.lang.ProcessBuilder.Redirect.INHERIT);

// wait for the process to complete.
var process = pb.start();
qie.debug('after Start');

var startTime = qie.deduceDate(qie.getSystemDate());
while (process.isAlive()) {
   var now = qie.deduceDate(qie.getSystemDate());
   if (now.getTime() > startTime.getTime() + 10000) {
      break;
   }
   qie.debug('Sleeping for 10 ms.');
   qie.pause(10);
}
if (process.isAlive()) {
   qie.warn('I will need to destroy it.');
   process.destroy();
}
qie.debug('Exit Value = ' + process.exitValue());
 

answered May 3, 2017 by mike-r-7535 (13,830 points)
0 votes

To capture the output of the command executed as a string in QIE, use the following

var pb = new java.lang.ProcessBuilder(
   "C:\\ImageMagick\\magick.exe",
   "C:\\qie_files\\outbound\\QLTest\\"+qie.getVariable('filename')+".pdf",
   "C:\\qie_files\\outbound\\QLTest\\"+qie.getVariable('filename')+".tiff");

pb.directory(new java.io.File("C:/Users/qiedev/Documents"));
pb.redirectErrorStream(true);

// These must be set to PIPE so we can get the output later
pb.redirectError(java.lang.ProcessBuilder.Redirect.PIPE);
pb.redirectOutput(java.lang.ProcessBuilder.Redirect.PIPE);

// wait for the process to complete.
var process = pb.start();
qie.debug('after Start');

var startTime = qie.deduceDate(qie.getSystemDate());
while (process.isAlive()) {
   var now = qie.deduceDate(qie.getSystemDate());
   if (now.getTime() > startTime.getTime() + 10000) {
      break;
   }
   qie.debug('Sleeping for 10 ms.');
   qie.pause(10);
}
if (process.isAlive()) {
   qie.warn('I will need to destroy it.');
   process.destroy();
}

// Get the output of the command as an InputStream
var inputStream = process.getInputStream();

// Convert the InputStream to a string
var outString = org.apache.commons.io.IOUtils.toString(inputStream, 'UTF-8');

// Set the output of the command as the message
message.setNode('/', outString);

qie.debug('Exit Value = ' + process.exitValue());
answered Nov 29, 2023 by jon-t-7005 (7,590 points)
...