I can't get engine instance when I run my java program by window service

2 Ansichten (letzte 30 Tage)

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.

Akzeptierte Antwort

halcyon
halcyon am 29 Sep. 2024
Bearbeitet: halcyon am 29 Sep. 2024
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

Weitere Antworten (2)

Rishav
Rishav am 19 Sep. 2024
Hi halcyon,
Please refer to the following MATLAB Answers post which shows how to use 'getInstance' to retrieve the current MATLAB Engine instance:
  1 Kommentar
halcyon
halcyon am 20 Sep. 2024
Thank you. But it's not the same usage scenario. I used the find method in the program to find the shared engine in the MATLAB window, but what you provided was to start an engine independently. My program can only send variables to the user's MATLAB window interface if it finds a shared engine, and a standalone engine cannot interact with users who are using MATLAB.

Melden Sie sich an, um zu kommentieren.


Rushikesh
Rushikesh am 20 Sep. 2024
Bearbeitet: Rushikesh am 20 Sep. 2024
Hello @halcyon,
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.
  1 Kommentar
halcyon
halcyon am 23 Sep. 2024
  1. I installed the Windows service by using the executable jar wrapped in the Winsw tool. All dependent JARs, including engine. jar, are already included in the executable JARs.
  2. The user I am using to run the window service is the same as the currently login user. The current login user can run Matlab programs.
  3. I ran the program on the server without a battery
  4. I am certain that the issue is not related to the MATLAB session.I have made multiple attempts to restart Matlab windows.The newly opened MATLAB window can receive variables sent by jar programs running on the command line。But when I tried to send variables using a program running on the window service, the MATLAB window couldn't receive them.After a program running as a window service attempts to send variables to a Matlab window,the programs running on the command line can't send variable to matlab window.That is to say, the program running as a service destroied the shared engine by the Matlab window.Unless I restart Matlab, command-line programs cannot send variables anymore.
I guess it's related to the window service, maybe due to some limitations, after all, I can run it normally in command-line mode.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Call MATLAB from C finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by