Setting up Eclipse for Compiling and Running a Java® File
Instead of using the COMSOL commands for compiling and running a Java® file that uses the COMSOL API one can use an Integrated Development Environment for doing these tasks. Using Eclipse makes it easier to write the Java code because Eclipse has built-in support for code completion and syntax highlighting. Furthermore, the debugger that comes as a part of Eclipse can be used to run the code line by line to verify the function of the code and check for any programming errors. Eclipse is free and can be downloaded from www.eclipse.org. To set up Eclipse for running an exported Java file, perform the following actions in Eclipse:
1
2
Go to the Libraries tab, select the Classpath entry, and click Add External JARs. Add all JAR files placed in the plugins directory under the COMSOL installation directory (typically C:\Program Files\COMSOL\COMSOL64\Multiphysics). This allows Eclipse to find the definitions of the classes used by the COMSOL API and to run the code in client/server mode. Click Finish.
3
Drag and drop your exported Java file the src folder of your Eclipse project.
4
ModelUtil.initStandalone(false);
The argument should be false for programs that do not use graphics and true for applications that do.
5
To run your Java program, you can create a Run Configuration in Eclipse. You do this from Run Configurations on the Run menu in Eclipse. There, select the Environment tab and then click the New button. In the New Environment Variable dialog, add the following variables; then click OK:
-
On Windows, use the Name PATH (on Windows) and enter the following text in Value: <comsolinstalldir>/lib/win64, where <comsolinstalldir> is the directory where COMSOL Multiphysics is installed.
-
On Linux, use the Name LD_LIBRARY_PATH and enter the following text in Value: <comsolinstalldir>/lib/glnxa64:<comsolinstalldir>/lib/glnxa64/gcc:<comsolinstalldir>/ext/graphicsmagick/glnxa64:<comsolinstalldir>/ext/cadimport/glnxa64, where <comsolinstalldir> is the directory where COMSOL Multiphysics is installed. Also add the variable with the Name LD_PRELOAD and enter the Value <comsolinstalldir>/java/glnxa64/jre/lib/amd64/libjsig.so. If the operating system is set to use a comma as the decimal separator, add a variable with the Name LC_ALL and the Value set to C to enforce the COMSOL software to use a period as the decimal separator.
-
On macOS, use the Name DYLD_LIBRARY_PATH and enter the following text in Value: <comsolinstalldir>/lib/maci64:<comsolinstalldir>/ext/graphicsmagick/maci64:<comsolinstalldir>/ext/cadimport/maci64, where <comsolinstalldir> is the directory where COMSOL Multiphysics is installed.
Click Apply.
6
7
public static void main(String[] args) {
    ModelUtil.connect("localhost", 2036);
    run();
    ModelUtil.disconnect();
}
When you have edited the main method you must save the file. Eclipse automatically compiles the file.
8
You also need to call System.exit(0) at the end of the Java program to terminate the process.
9
The Java program can now be started in either Run or Debug mode from Eclipse. Notice that the COMSOL server window responds by writing that a connection has been set up when your application starts.