Error: Could not find or load main class Clojure.main

When running a Clojure program, if the java virtual machine (JVM) is unable to locate the main class, it generates the Could not find or load main class Clojure.main error.

You can try one of the following options to resolve this error.

How to resolve

  • Ensure that clojure is installed on your system. You can download it from the official websitehttps://clojure.org/releases/downloads. The JVM version should be compatible with the clojure version.

  • Verify the syntax of the command to run the clojure program. Here’s the template:
    java -cp <classpath> clojure.main -m <your-namespace>/<your-main-function>
    
    Here, the <classpath> refers to the path that includes the necessary source files and other dependencies. Make sure that <your-namespace> and <your-main-function> are replaced with the correct values from the program.
  • Ensure that the file containing your main function should be in a directory structure that matches its namespace.

  • Verify that the <classpath> is correctly set to include the necessary clojure libraries and JAR files.

  • Verify the :gen-class declaration. Do not forget to add the (:gen-class) key to the ns call:
    (ns your.namespace
    (:gen-class))
    

Other options

You can also try a build tool like LeiningenA tool for automating and building Clojure dependencies and projects. or tools.deps to simplify the process of managing dependencies and running your Clojure program.

  • With Leiningen:

    lein uberjar
    
  • With tools.deps:

     clj -M -m <your-namespace>/<your-main-function>
    

Try it yourself

  • Step 1: Enter the following command in the root of the project directory.

nano project.clj
  • Step 2: Add the following code to the file and after making the changes, press Ctrl + X to save the changes and exit the file.

(defproject my-clojure-project "0.1.0-SNAPSHOT"
:description "A simple project to demonstrate Clojure error"
:dependencies [[org.clojure/clojure "1.10.0"]]
:main clojure.examples.example)
Content of project.clj file
  • Step 3: Run the following command:

java -cp target/my-clojure-project-0.1.0-SNAPSHOT-standalone.jar clojure.main

It will generate the error. Try re-building the project with the following command, lein uberjar, and perform step 3 again.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved