Within the OpenMRS Webapp

Everything is delivered from the database in the form of objects: Patient, User, Concept, etc java objects.

You can fetch/save these objects using what we call "services" (PatientService, UserService, ConceptService).

You can get the services at any point by calling Context.getPatientService(), .getUserService(), etc.

To print out the names of all the patients in the system:

List<Patient> patients = Context.getPatientService().getAllPatients();
for (Patient patient : patients) {
  System.out.println("Patient: " + patient.getGivenName() + " " + patient.getFamilyName());

In an External Application using web services

See Web Services

In an External Application using the API Jar

The openmrs-api-xxx.jar file can be used in stand-alone java applications. There are three simple steps you need to follow:

  1. Download/build the openmrs api jar file "openmrs-api.****.jar" and include it on your classpath.
  2. Include the dependent libraries on your classpath, which you can get from openmrs-trunk/api/pom.xml.
  3. Fire up OpenMRS by calling: Context.startup(<connection.url>, <connection.username>, <connection.password>, getProperties());
  4. You need to surround any units of work with Context.openSession() (which startup() calls for you) and Context.closeSession()
  5. Authenticate into OpenMRS by calling: Context.authenticate(username, password);
  6. You should be able to call into the Context and work with the services.


public static void main(String[] args) {
  File propsFile = new File(OpenmrsUtil.getApplicationDataDirectory(), "openmrs-runtime.properties");**
  Properties props = new Properties();
  OpenmrsUtil.loadProperties(props, propsFile);
  Context.startup("jdbc:mysql://localhost:3306/db-name?autoReconnect=true", "openmrs-db-user", "3jknfjkn33ijt", propsFile);
  try {
    Context.authenticate("admin", "test");
    List<Patient> patients = Context.getPatientService().getPatients("John");
    for (Patient patient : patients) {
      System.out.println("Found patient with name " + patient.getPersonName() + " and uuid: " + patient.getUuid());
  finally {

OpenMRS startup environment

In order to start OpenMRS with all it's modules (especially necessary if "mandatory" modules as with OpenMRS 1.6 are present), it is not sufficient to simply include the required jar files. Choose one of the following approaches to get your app started:

  1. Start the external app with the same user, that Tomcat and the OpenMRS webapp are using.
  2. If you are using a different user, specify the Global Property module_repository_folder with an absolute path to the modules directory of the OpenMRS webapp.
  3. Set the working (start) dir of your external app to the OpenMRS base folder of the webapp. (This should usually be the place where the openmrs-runtime.properties is located.)
  4. Overwrite the application_data_directory of the Overriding OpenMRS Default Properties

Note that scheduled tasks can/may/will fail during initialization.

Demarcating start and end of API work