Running a batch file

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

Running a batch file

#1

Postby Steve James » Wed Oct 28, 2015 8:10 am

Hi I have a need to run a 3rd party batch file. I've managed to lift some examples found online and the basics are working. The code can retrieve the screen output including errors. I'm definitely not Java competent but it seems to work ok.

I noticed the following
Because of this conflict, it's a good idea not to use importPackage on the java.lang package
in http://portal.ebasetech.com/cp/doc/Java ... c361045033

So instead of importing java.lang should I have fully qualified the relevant references?
This is where things get confusing as I then get errors like "is not a function, it is object"
eg
var input = new java.lang.BufferedReader(new java.lang.InputStreamReader(pr.getInputStream()));

Sorry if this appears to be a basic query but Java has intricacies that at the moment are 'interesting'.

Code: Select all

importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);
importPackage(java.lang);
importPackage(java.io);

try
{
  var cmd = ["c:\\folderx\\test.bat","107"];

  var builder = new ProcessBuilder(cmd) 
	var pr = builder.start();
	
	var input = new BufferedReader(new InputStreamReader(pr.getInputStream())); 
	var lineI = null; 
	var ResI = "";
	
	while((lineI=input.readLine()) != null) 
	{ 
			log(lineI);
		 	ResI += lineI + '<br>';
	&#125; 
						
	var inputE = new BufferedReader&#40;new InputStreamReader&#40;pr.getErrorStream&#40;&#41;&#41;&#41;; 
	var lineE = null;
	var ResE = "";
						
	ResE += "ERRORS - ";
	while &#40;&#40;lineE = inputE.readLine&#40;&#41;&#41; != null&#41;
	&#123;
		ResE = ResE + lineE + "<br>";
	&#125;
						
	var exitVal = pr.waitFor&#40;&#41;; 
	log&#40;"Exited with error code " + exitVal&#41;; 

	var message = ResI + '<br><br>' + ResE + '<br><br>' + exitVal; 
	event.owner.addErrorMessage&#40;message&#41;;
&#125;
catch&#40;e&#41;
&#123;
  event.owner.addErrorMessage&#40;e.message&#41;;
&#125;
For the moment I've got a simple batch file (it's hugely faster than trying to run the 3rd party batch file).

Code: Select all

echo off
echo write to log file START >> log.txt

set arg1=%1
rem pass to log file
echo write to log file something else >> log.txt

echo parameter passed - %arg1%

deliberate error
echo write to log file END >> log.txt

exit /b %arg1%
Thanks
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#2

Postby Steve James » Wed Oct 28, 2015 8:44 am

:-(

Embarrassingly I think I've just worked out that BufferedReader and InputStreamReader are java.io not java.lang; which of course makes sense but the error message kind of directed me toward java.lang?.

Is there any way to get intellisense at the higher level or is that another daft question?
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#3

Postby Jon » Wed Oct 28, 2015 9:09 am

Steve,

The import of java.lang probably doesn't matter one way or the other. If you miss it out, then you have to say:

Code: Select all

var builder = new java.lang.ProcessBuilder&#40;cmd&#41;;
(incidentally, the semicolon is missing from the end of that line in your code).

The error you're getting usually means that something is wrong with the code. With Javascript everything is evaluated at the point of execution, so it's only at this point that the Javascript engine attempts to work out whether passed arguments are the required type. You get code assist on the Java objects and their methods, but the system won't display Java type incompatibility errors in the same way that a Java IDE will - this is just the nature of Javascript.

I'm not too sure what's wrong with your code. Some comments:

The builder.start() line will start a separate process asynchronously which will then run independently of the main thread. If you want to read the output from this process, I think you have to first wait for it to finish executing. Something like:

Code: Select all

var builder = new ProcessBuilder&#40;cmd&#41;
var pr = builder.start&#40;&#41;;
pr.waitFor&#40;&#41;;
Do you mean to read the input stream, or are you trying to log the output? i.e. do you want..

Code: Select all

var input = new BufferedReader&#40;new InputStreamReader&#40;pr.getOutputStream&#40;&#41;&#41;&#41;; 
Jon
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#4

Postby Steve James » Wed Oct 28, 2015 9:29 am

Thanks Jon.

semi-colon - thanks yes I'd spotted that but didn't fix the code in the post.

The "is not a function, it is object" error message turned out to be me pointing BufferedReader and InputStreamReader at java.lang instead of java.io

Like you say the code above works with

Code: Select all

importPackage&#40;com.ebasetech.xi.api&#41;;
importPackage&#40;com.ebasetech.xi.services&#41;;
importPackage&#40;java.lang&#41;;
importPackage&#40;java.io&#41;;
//blah blah
var builder = new ProcessBuilder&#40;cmd&#41;;
or

Code: Select all

importPackage&#40;com.ebasetech.xi.api&#41;;
importPackage&#40;com.ebasetech.xi.services&#41;;
importPackage&#40;java.io&#41;;
//blah blah
var builder = new java.lang.ProcessBuilder&#40;cmd&#41;;
It does feel odd but the code returns the output from the batch file by getting the inputStream. No idea why but hey ho.

I may have to tweak the order of the code once I start running the 3rd party batch file which can take up to 5 minutes to run but at least it works with this simple example.

Thanks
0 x


Who is online

Users browsing this forum: No registered users and 14 guests