execute command line bat files

0
Is it possible to execute a .bat on a local or remote computer from the application?   Thanks, Costin Marzea
asked
5 answers
1

Costin,

As far as I know, the only communication mechanisms available to a Mendix app are web based (i.e. http calls).  It has been some time since I have used bat files, but if there is a way that they can be invoked using a web service, you may be able to call them on a remote computer from a Mendix app.  On your local machine, you might be able accomplish this with some custom Java code.  Otherwise, I don’t think so.

Hope that helps,

Mike

answered
1

Costin,

Browsers do not support calling BAT files from web applications. They will never allow you to execute on the local file system. (unless you use new PWA function https://web.dev/native-file-system/ but it will be limited to read write) 

Wrapping you app in https://www.electronjs.org/ will give you some more optoins (using Node JS apis)

De server should/could use BAT files eighter, as they will run on linux system (most of the time, unless you host yourself) However there you can use Java Action to execute some OS action. Though that will be limited for security reasons.

Cheers, Andries

 

answered
1

Definitely it is possible, you need to write some Java code which creates a BAT file in your deployment folder, write the lines in that file, and execute it. An example which installs Node and some Node Module in a BAT file (Linux code is not complete, Windows it is);




// BEGIN USER CODE
		
		String delim="";
		String scriptName="";
		String cmd="";
		Boolean Linux=false; //placeholder to change future scripts based on OS type
		if (Linux) {
			delim = "/";
			scriptName="install.sh";
			cmd = "install.sh";
		}
		else {
			delim = "\\";
			scriptName="install.bat";
			cmd = "cmd /c install.bat";
		}
		
		try {
		String resourceFolder=com.mendix.core.Core.getConfiguration().getResourcesPath()+"";
		MakeMxLog(getContext(),"resourceFolder",resourceFolder);
		MakeMxLog(getContext(),scriptName,resourceFolder+delim+scriptName);
		
		
		final File file = new File(resourceFolder+delim+scriptName);
		file.createNewFile();
		
		PrintWriter writer = new PrintWriter(file, "UTF-8");
		writer.println("cd "+resourceFolder);
		MakeMxLog(getContext(),scriptName,"cd "+resourceFolder);
		
		if (Linux) {
			MakeMxLog(getContext(),scriptName,"chmod u+x "+resourceFolder+delim+scriptName);
			writer.println("chmod u+x "+resourceFolder+delim+scriptName);
	
			}
		
		writer.println("mkdir node");
		MakeMxLog(getContext(),scriptName,"mkdir node");
		writer.println("cd "+ resourceFolder+delim+"node");
		MakeMxLog(getContext(),scriptName,"cd "+ resourceFolder+delim+"node");
		writer.println("npm install solc@0.5");
		MakeMxLog(getContext(),scriptName,"npm install solc@0.5");
		writer.println("exit");
		writer.close();
		Process p =null;
		
		if (Linux) {
	        ProcessBuilder pb = new ProcessBuilder(resourceFolder+delim+scriptName);
	        p = pb.start();
	        MakeMxLog(getContext(),resourceFolder+delim+scriptName,"script executed Linux");
		}
		else {
		 p =  Runtime.getRuntime().exec(cmd, null, new File(resourceFolder));
		 MakeMxLog(getContext(),scriptName,"script executed Windows");
			p.waitFor();
			file.delete();
	
		}
		
		StringWriter writer2 = new StringWriter();
		IOUtils.copy(p.getInputStream(), writer2, "UTF-8");
		String response = writer2.toString();
		Core.getLogger("CMD").info(response);
		 MakeMxLog(getContext(),scriptName,response);
		return true;
		
		}
		
		catch(Exception e)  {
			 MakeMxLog(getContext(),"exception",e.getMessage());
			return false;
		}
				
				
				
		// END USER CODE
	}



 

answered
1

I used the REST API to call the Talend service and everything worked fine.

answered
0

Hi guys,

Thank you for the answers. I will use a REST API to launch the Talend jobs on execution Servers. 

I hope that Mendix supports REST API calls.

 

Kind Regards

Costin Marzea

answered