Answers.Body: <p><code>JFactory</code> is deprecated in Joomla 5 and should be replaced with <code>Factory</code>.<br>
  I raised an issue for this: <a href="https://lab.civicrm.org/dev/joomla/-/issues/55" rel="nofollow noreferrer">https://lab.civicrm.org/dev/joomla/-/issues/55</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-17T12:31:38.923Z
Answers.Text: Thanks for the link. I added some code that seems resolve errors like Attempted to load class "JFactory" and forget a "use" statement
Answers.UserDisplayName: Alain Rihs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-09T17:40:54.497Z
Answers.LastActivityDate: 2024-04-09T17:40:54.497Z
Answers.UserDisplayName: Guitarman
Answers.Body: <p>basically it's this:</p>
  <ol>
  <li><p>In your website root folder, create folders and a file on your server with the code from the above answer.  We usually use something like ./html/helperfiles/civi_cron.php</p>
  </li>
  <li><p>Edit the file and add a super user's username and password - you may be able to use an administrator user (haven't tried that).   No worries as the file is not accessible on its own.  We usually create a &quot;CronUser&quot; for this purpose</p>
  </li>
  <li><p>Create an article and menu item (single article). Perhaps something like &quot;CiviCRM Cron&quot;</p>
  </li>
  <li><p>In the body of the article use the Sourcerer plugin (<a href="https://regularlabs.com/sourcerer/features" rel="nofollow noreferrer">https://regularlabs.com/sourcerer/features</a>) or something similar, to call the php file. If you used the folder &amp; file names above your code should look something like.</p>
  </li>
  </ol>
  <pre><code>&lt;?php include_once('html/helperfiles/civi_cron.php'); ?&gt;
  </code></pre>
  <ol start="5">
  <li>Test it out:  <a href="https://your-site.com/civicrm-cron?debug=1" rel="nofollow noreferrer">https://your-site.com/civicrm-cron?debug=1</a></li>
  </ol>
  <p>If all works and no errors, then you can remove the ?debug=1 and then use this URL in your server's cron.</p>
  <p>I hope this helps!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-13T18:12:44.787Z
Answers.Text: Finally had a chance to try this out, and even with Sourcer pro, all I'm getting is an 'echo' of the include file in the HTML and, of course, it doesn't trigger the jobs.
  
  There must be something I'm missing :(
Answers.UserDisplayName: Neil Z
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-11T17:25:37.837Z
Answers.LastActivityDate: 2024-10-11T17:25:37.837Z
Answers.UserDisplayName: KevinB
Answers.Body: <p>So not really an answer - more of a workaround.  I created a php file to include (via Sourcerer plugin) in an article which is publicly accessible. The file does several things</p>
  <ol>
  <li>Logs in as an admin user</li>
  <li>Accesses and runs CiviCRM JobManager function - which is the same as the Administrator &gt; System Settings &gt; Scheduled Jobs &gt; &quot;run all scheduled jobs manually&quot; link</li>
  <li>Logs out the admin user</li>
  </ol>
  <p>Because we're already accessing the Joomla framework there are no errors from CiviCRM trying to load via version 3.x methods.  I simply use this URL to this article (actually menu alias) in place of the regular cron URL.</p>
  <p>Here's my include code. To use simply change the vars at the top to match a user that has the correct privileges to access the CiviCRM admin settings.</p>
  <pre>
  // vars
  $username = 'YOUR_USERNAME';
  $password = 'YOUR_PASSWORD';
  
  $debug=0;
  if ((isset($_GET['debug'])) && ($_GET['debug'] == 1)) $debug = 1;
  if ($debug) ini_set('display_errors', 1);
  
  // No direct access
  defined('_JEXEC') or die;
  
  // Include Joomla framework
  use Joomla\CMS\Factory;
  
  $app = Factory::getApplication('site');
  
  // Attempt to log in the user
  $credentials = array('username' => $username, 'password' => $password);
  $options = array('silent' => true);
  
  $response = $app->login($credentials, $options);
  
  // Check if the login was successful
  if ($response !== true) {
      die('Login failed.');
  }
  
  // Initialize CiviCRM
  define('CIVICRM_SETTINGS_PATH', JPATH_ADMINISTRATOR . '/components/com_civicrm/civicrm.settings.php');
  require_once CIVICRM_SETTINGS_PATH;
  require_once 'CRM/Core/Config.php';
  
  $config = CRM_Core_Config::singleton();
  
  // Manually run all scheduled jobs
  require_once 'CRM/Core/JobManager.php';
  
  $jobManager = new CRM_Core_JobManager();
  $result = $jobManager->execute(FALSE);
  
  if ($result['is_error']) {
      echo 'An error occurred while running the scheduled jobs: ' . $result['error_message'];
  } else {
      echo 'Scheduled jobs ran successfully.';
  }
  
  $app->logout(); // Perform the logout
  echo 'User logged out.';
  
  </pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-22T03:20:40.677Z
Answers.LastActivityDate: 2024-07-22T03:20:40.677Z
Answers.UserDisplayName: KevinB
Answers.Body: <p>I've just moved from J4 to J5, and like others here, have found that the deprecations have broken the cron job.</p>
  <p>I've looked at the developer discussions on this, and I understand the reasoning, but having to manually run a job gets rather old.</p>
  <p>If we can just get a working CRON for the time being that would suffice for many.</p>
  <p>If Kevin can give an example of how his PHP file is called, it would help me greatly.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-11T17:12:19.773Z
Answers.Text: will add additional answer below (running out of space here)
Answers.UserDisplayName: KevinB
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-10T06:03:41.140Z
Answers.LastActivityDate: 2024-10-10T06:03:41.140Z
Answers.UserDisplayName: Neil Z
Body: <p>I'm running CiviCRM 5.70.2 and Joomla! 5.0.3 w/ PHP 8.2 ... all seems to be working as expected with the only problem being the cron.</p>
  <p>In administrator/components/com_civicrm/civicrm/bin/cron.php ...</p>
  <ol>
  <li><p>To get it to run I had to first define CIVICRM_CONFDIR</p>
  </li>
  <li><p>Then I'm seeing errors in debug mode &quot;Attempted to load class &quot;JFactory&quot; from the global namespace.&quot;   ... which seems like it's not seeing Joomla's compatibility plugin or not loading the framework.</p>
  </li>
  </ol>
  <p>Any thoughts or suggestions?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-03-05T12:19:53.143Z
Comments.Text: Welcome to SE.  I haven't tried Joomla 5 but I find the [cv](https://docs.civicrm.org/sysadmin/en/latest/setup/jobs/#cv) method works well for cron.
Comments.UserDisplayName: Aidan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-04-09T12:38:42.133Z
Comments.Text: I can verify this is an issue, as it occurs with both running the cron jobs in cv and php in joomla 5 $ cv api job.execute --user=cronuser cwd=/var/www/joomla In Joomla.php line 651: Attempted to load class "JFactory" from the global namespace. Did you forget a "use" statement? or php /var/www/joomla/administrator/components/com_civicrm/civicrm/bin/cli.php Symfony\Component\ErrorHandler\Error\ClassNotFoundError^ {#675 #message: """ Attempted to load class "JFactory" from the global namespace.\n Did you forget a "use" statement? """ #code: 0 #file: "./administrator/components/com_civicrm/civicr
Comments.UserDisplayName: Larry G. Wapnitsky
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-07-17T12:30:26.670Z
Comments.Text: Where (setting file?) and how do you define CIVICRM_CONFDIR?
Comments.UserDisplayName: Alain Rihs
Tags: <joomla><cron>
Title: Joomla 5 = cron not working
ThreadText: # Joomla 5 = cron not working
  
  (Asked by: username_0 on 2024-03-04)
  
  I'm running CiviCRM 5.70.2 and Joomla! 5.0.3 w/ PHP 8.2 ... all seems to be working as expected with the only problem being the cron.
  
  In administrator/components/com\_civicrm/civicrm/bin/cron.php ...
  
  1. To get it to run I had to first define CIVICRM\_CONFDIR
  2. Then I'm seeing errors in debug mode "Attempted to load class "JFactory" from the global namespace." ... which seems like it's not seeing Joomla's compatibility plugin or not loading the framework.
  
  Any thoughts or suggestions?
  
  **Tags:** joomla, cron
  
  > **Comments:**
  > **username_1**: Welcome to SE.  I haven't tried Joomla 5 but I find the [cv](https://docs.civicrm.org/sysadmin/en/latest/setup/jobs/#cv) method works well for cron.  
  > **username_2**: I can verify this is an issue, as it occurs with both running the cron jobs in cv and php in joomla 5 $ cv api job.execute --user=cronuser cwd=/var/www/joomla In Joomla.php line 651: Attempted to load class "JFactory" from the global namespace. Did you forget a "use" statement? or php /var/www/joomla/administrator/components/com_civicrm/civicrm/bin/cli.php Symfony\Component\ErrorHandler\Error\ClassNotFoundError^ {#675 #message: """ Attempted to load class "JFactory" from the global namespace.\n Did you forget a "use" statement? """ #code: 0 #file: "./administrator/components/com_civicrm/civicr  
  > **username_3**: Where (setting file?) and how do you define CIVICRM_CONFDIR?  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_4 on 2024-04-09. Score: 2
  
  `JFactory` is deprecated in Joomla 5 and should be replaced with `Factory`.  
  I raised an issue for this: <https://lab.civicrm.org/dev/joomla/-/issues/55>
  
  > **Comments:**
  > **username_3**: Thanks for the link. I added some code that seems resolve errors like Attempted to load class "JFactory" and forget a "use" statement  
  
  ---
  
  ### Answer by username_0 on 2024-10-11. Score: 0
  
  basically it's this:
  
  1. In your website root folder, create folders and a file on your server with the code from the above answer. We usually use something like ./html/helperfiles/civi\_cron.php
  2. Edit the file and add a super user's username and password - you may be able to use an administrator user (haven't tried that). No worries as the file is not accessible on its own. We usually create a "CronUser" for this purpose
  3. Create an article and menu item (single article). Perhaps something like "CiviCRM Cron"
  4. In the body of the article use the Sourcerer plugin (<https://regularlabs.com/sourcerer/features>) or something similar, to call the php file. If you used the folder & file names above your code should look something like.
  
  ```
  <?php include_once('html/helperfiles/civi_cron.php'); ?>
  
  ```
  
  5. Test it out: <https://your-site.com/civicrm-cron?debug=1>
  
  If all works and no errors, then you can remove the ?debug=1 and then use this URL in your server's cron.
  
  I hope this helps!
  
  > **Comments:**
  > **username_5**: Finally had a chance to try this out, and even with Sourcer pro, all I'm getting is an 'echo' of the include file in the HTML and, of course, it doesn't trigger the jobs.  There must be something I'm missing :(  
  
  ---
  
  ### Answer by username_0 on 2024-07-22. Score: 0
  
  So not really an answer - more of a workaround. I created a php file to include (via Sourcerer plugin) in an article which is publicly accessible. The file does several things
  
  1. Logs in as an admin user
  2. Accesses and runs CiviCRM JobManager function - which is the same as the Administrator > System Settings > Scheduled Jobs > "run all scheduled jobs manually" link
  3. Logs out the admin user
  
  Because we're already accessing the Joomla framework there are no errors from CiviCRM trying to load via version 3.x methods. I simply use this URL to this article (actually menu alias) in place of the regular cron URL.
  
  Here's my include code. To use simply change the vars at the top to match a user that has the correct privileges to access the CiviCRM admin settings.
  
  ```
  
  // vars
  $username = 'YOUR_USERNAME';
  $password = 'YOUR_PASSWORD';
  
  $debug=0;
  if ((isset($_GET['debug'])) && ($_GET['debug'] == 1)) $debug = 1;
  if ($debug) ini_set('display_errors', 1);
  
  // No direct access
  defined('_JEXEC') or die;
  
  // Include Joomla framework
  use Joomla\CMS\Factory;
  
  $app = Factory::getApplication('site');
  
  // Attempt to log in the user
  $credentials = array('username' => $username, 'password' => $password);
  $options = array('silent' => true);
  
  $response = $app->login($credentials, $options);
  
  // Check if the login was successful
  if ($response !== true) {
      die('Login failed.');
  }
  
  // Initialize CiviCRM
  define('CIVICRM_SETTINGS_PATH', JPATH_ADMINISTRATOR . '/components/com_civicrm/civicrm.settings.php');
  require_once CIVICRM_SETTINGS_PATH;
  require_once 'CRM/Core/Config.php';
  
  $config = CRM_Core_Config::singleton();
  
  // Manually run all scheduled jobs
  require_once 'CRM/Core/JobManager.php';
  
  $jobManager = new CRM_Core_JobManager();
  $result = $jobManager->execute(FALSE);
  
  if ($result['is_error']) {
      echo 'An error occurred while running the scheduled jobs: ' . $result['error_message'];
  } else {
      echo 'Scheduled jobs ran successfully.';
  }
  
  $app->logout(); // Perform the logout
  echo 'User logged out.';
  
  ```
  
  
  ---
  
  ### Answer by username_5 on 2024-10-10. Score: 0
  
  I've just moved from J4 to J5, and like others here, have found that the deprecations have broken the cron job.
  
  I've looked at the developer discussions on this, and I understand the reasoning, but having to manually run a job gets rather old.
  
  If we can just get a working CRON for the time being that would suffice for many.
  
  If Kevin can give an example of how his PHP file is called, it would help me greatly.
  
  > **Comments:**
  > **username_0**: will add additional answer below (running out of space here)  
  
  ---
  
  