Passing input parameters through Java action

0
Hi I'm looking to pass input parameters to an external java component using Java Action functionality in mendix Input parameter is setup and i'm trying to call external java library by adding following custom logic (shown below). Do I need to write a line code to load .jar file from userlib (as in code below) or will this be automatically called upon by mendix?                   try {                     // Construct the URL to the external Java component                     String modelURL = "";                     String externalComponentURL = modelURL + "?username=password=";                       // Load the .jar file from the userlib directory                     URL jarUrl = new URL("file://" + System.getProperty("mendix.home") + "/userlib/externalComponent.jar");                     URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl});                       // Load the main class from the .jar file                     Class<?> externalClass = classLoader.loadClass("com.example.ExternalComponent");                       // Instantiate the class                     Object externalComponent = externalClass.newInstance();                       // Cast the external component to the appropriate type                     // Replace 'ExternalComponentInterface' with the actual interface provided by your external component                     ExternalComponentInterface externalInterface = (ExternalComponentInterface) externalComponent;                       // Make a call to the external component passing the provided parameters                     // Replace 'invokeMethod' with the actual method you need to call on the external component                     String output = externalInterface.invokeMethod(externalComponentURL);                       // Process the output as needed                     java.nio.file.Files.write(java.nio.file.Paths.get("OutputParameters.txt"), output.getBytes());                   } catch (Exception e) {                     // Handle exceptions                     e.printStackTrace();                 }
asked
1 answers
0

You have to import it first. For instance:

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package templaterformendix.actions;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.webui.CustomJavaAction;
import hr.ngs.templater.Configuration;
import hr.ngs.templater.IDocumentFactory;
import templaterformendix.implementation.TemplaterJson;

You are then able to use these in your code.

Regards,

Ronald

answered