Java Programming

Last modified: November 29, 2023

1 Introduction

With Java actions, you can extend the functionality of your application in situations where it would be hard to implement this functionality in microflows.

For information about Java actions in Studio Pro, see Java Actions.

2 Writing Code in .java Files of Your Java Actions

In .java files of your Java actions, the method executeAction is called by the Mendix Runtime when the Java action is being executed. The method throws all exceptions that occur. This means you can do error handling in the microflow calling this Java action. If you would like to do your own error handling within the action, use try / catch statements.

Between the lines // BEGIN USER CODE and // END USER CODE, you can write custom code that will always be called when executing the action. You can call other methods in the section between // BEGIN EXTRA CODE and // END EXTRA CODE.

Code outside the blocks is regenerated each time when you deploy your model, thus any modifications that you make in them will be overwritten. However, your imports will be preserved.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 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 app.
// Special characters, for example, é, ö, à, etc. are supported in comments.

package myfirstmodule.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

public class JavaAction_1 extends CustomJavaAction<java.lang.Void>
{
	public JavaAction_1(IContext context)
	{
		super(context);
	}
	@java.lang.Override
	public java.lang.Void executeAction() throws Exception
	{
		// BEGIN USER CODE
		return true;
		// END USER CODE
	}
	/**
	 * Returns a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "JavaAction_1";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

3 Using the Mendix Java Library

You can use the Mendix Java library in the Java code that you write for your Java actions.

This library is automatically added to your libraries when you import your app into Eclipse, it is called mxruntime.jar.

For details on usage and examples, see Using the Java API.

4 Opening HTTP Connections

Most cloud infrastructure services (including those used by the Mendix Cloud) will close HTTP connections automatically if there is no traffic for a few minutes, even if your activity is still waiting for a response. This means that, if your activity calls a web service which takes a long time to respond, the connection may be closed without the activity being aware of this and your activity will not receive a response, but instead get stuck waiting indefinitely for data to arrive.

You should therefore ensure that you always set a timeout for any connections you make in your custom Java code.

5 Using Eclipse as an Environment to Write Your Mendix Java Actions

For details on this topic, see Using Eclipse.

6 Documents in This Category

  • Troubleshooting
  • Lists the problematic JAR files and the known workarounds.

  • Using Eclipse
  • Describes how to set up Eclipse, and how to add a Mendix application to Eclipse and launch it.

  • Java Version Migration
  • Describes consequences for a Mendix app when migrating from one Java version to another.