Change the approach to solve this problem - place the startup script in the Windows operating system start menu. This way, when the window starts, it will start, just like starting a project in a service
I can't get engine instance when I run my java program by window service
57 views (last 30 days)
Show older comments
My software system is developed using Java. One function of the program is to send variables to the MATLAB window. When I run my Java program in the development environment or command-line window, it works well and sends variables correctly to the MATLAB window. But when I start my system with a window operating system service or scheduled task, variables cannot be sent to the MATLAB window. I carefully checked and found that when running my software program using the window service, I cannot retrieve the engine instance currently running in the MATLAB window.
0 Comments
More Answers (2)
Rishav
on 19 Sep 2024 at 11:30
Hi halcyon,
Please refer to the following MATLAB Answers post which shows how to use 'getInstance' to retrieve the current MATLAB Engine instance:
Rushikesh
on 20 Sep 2024 at 9:44
Edited: Rushikesh
on 20 Sep 2024 at 11:49
There can be multiple reasons why a Windows operating system service, created from a Java program, may be unable to access a MATLAB shared engine instance. Below are some potential causes and solutions:
(Assuming that you are creating EXE from executable JAR or creating bash file calling JAR file and then using Task creator to create windows service passing EXE/bash file)
1. The method used to create a Windows service from your Java application can sometimes lead to errors, especially when JAR files are bundled into an EXE or called via a bash script, and the “com.mathworks.engine” package is not detected. If you are generating an EXE from a Java program, ensure that the full path to the MATLAB engine.jar file is included in the 'classpath' section. For instance, in JSmooth, which is used to create an EXE from a Java file, you can specify dependent JAR files in the Application > Classpath area. If you are using a bash script executed from a Windows service, ensure the engine path is specified.
2. An error may also occur if the task/service is created without selecting a specific MATLAB license holder. In such cases, select "Run only when the user is logged on" to ensure the MATLAB engine is accessible for that user's license. This option is available under General > Security Options when creating a new task using "taskschd.msc".
3. When creating a task to run as a service, if the option "Start task only if the computer is on AC power" is selected under Conditions > Power, and the computer is running on battery, this can cause issues. Make sure to deselect this option if the computer is operating on battery power.
4. There may be a problem with the MATLAB shared engine itself. Try restarting MATLAB and initiating a new shared session. You can verify the session name using the “matlab.engine.engineName” command in the MATLAB command window and ensure the same session name is recognized in your Java program when executed via the command line.
Hope this helps.
See Also
Categories
Find more on Call MATLAB from C in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!