Get return value from Java class file in Switchscripter

Post Reply
automation
Member
Posts: 40
Joined: Tue Jan 15, 2019 10:19 pm

Get return value from Java class file in Switchscripter

Post by automation »

I execute a java class (see java code below) like this in SwitchScripter

Code: Select all

var status = Process.execute("java -cp " + path + " XSDValidator " + xtdPath + " " + sourcePath);
It works fine and I can get the println like this

Code: Select all

s.log(3, "Java message stdout: %1", Process.stdout);
s.log(3, "Java error message stderr: %1", Process.stderr);
But I want in SwitchScripter grab if the java class return true or false (if boolean isValid is true or false in the java class).

status is always 0 even if the function in the java class returns true or false.

Code: Select all

s.log(3, "--- :   " + status);
I want something like this in SwitchScripter

Code: Select all

if(status == false) { //the java class returns false and isValid in the function is false.
//Write error to the log
} else {
//Write all is ok to the log
}
The Java class

Code: Select all

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class XSDValidator {
   public static void main(String[] args) {
      if(args.length !=2){
         System.out.println("Usage : XSDValidator <file-name.xsd> <file-name.xml>" );
      } else {
         boolean isValid = validateXMLSchema(args[0],args[1]);
         
         if(isValid){
            System.out.println(args[1] + " is valid against " + args[0]);
         } else {
            System.out.println(args[1] + " is not valid against " + args[0]);
         }
      }
   }
   
   public static boolean validateXMLSchema(String xsdPath, String xmlPath){
      try {
         SchemaFactory factory =
            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(xsdPath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(xmlPath)));
      } catch (IOException e){
         System.out.println("Exception: "+e.getMessage());
         return false;
      }catch(SAXException e1){
         System.out.println("SAX Exception: "+e1.getMessage());
         return false;
      }
		
      return true;
	
   }
}
bens
Advanced member
Posts: 253
Joined: Thu Mar 03, 2011 10:13 am

Re: Get return value from Java class file in Switchscripter

Post by bens »

Java applications don't have an exit status like normal command-line applications. Technically what you're doing when running "java -cp <path>" is running java.exe, and its return value will be 0 (success) because the code executed correctly. You also can't access the isValid variable because it's a local variable in your Java code and has no meaning outside of that.

To get what you want I think you have three options:
1. Don't use Java and validate the XML in a different way.
2. Use the Java code as it is now and parse Process.stdout or Process.stderr to check the output.
3. This is probably the option you'll want: in your Java code add a line System.exit( isValid ); (or something similar). System.exit() will quit the Java VM and use the parameter as the exit code. The convention is that 0 is success and 1 (or non-zero) is failure, but really it's whatever you want.
Post Reply