1

I am trying to run a clojure jar file and I get the error below:

C:\Users\Nahashon>java -jar C:\Users\Nahashon\.m2\repository\org\enclojure\sample\0.0.1\sample-0.0.1.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/mode/mdzw

Caused by: java.lang.ClassNotFoundException: com.mode.mdzw
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.mode.mdzw. Program will exit.

The clojure project is created using maven on netbeans 6.9.1. and the clojure code is as below:

(ns com.mode.mdzw
(:gen-class))

(defn -main []
(println "Looks like it works!"))

The POM file is as below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
    <clojure.version>1.1.0</clojure.version>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>org.enclojure</groupId>
<artifactId>sample</artifactId>
<version>0.0.1</version>
<name>mdzw</name>
<description>mdzw</description>
<build>
 <sourceDirectory>src/main/clojure</sourceDirectory>
 <testSourceDirectory>src/test/clojure</testSourceDirectory>
    <resources>
        <resource>
            <directory>src/main/clojure</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/clojure</directory>
        </testResource>
    </testResources>
<plugins>
  <plugin>
    <groupId>com.theoryinpractise</groupId>
    <artifactId>clojure-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <sourceDirectories>
            <sourceDirectory>src/main/clojure</sourceDirectory>
        </sourceDirectories>
        <clojureOptions>-Xmx1G</clojureOptions>
    </configuration>
    <executions>
      <execution>
        <id>compile-clojure</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
  </plugin>

  <!-- -->
   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>classes.com.mode.mdzw</mainClass>
                    <classpathPrefix>dependency</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

</plugins>
</build>
<repositories>
  <repository>
  <id>central</id>
  <url>http://repo1.maven.org/maven2</url>
  </repository>
<repository>
  <id>clojure-releases</id>
  <url>http://build.clojure.org/releases</url>
</repository>
<repository>
  <id>incanter</id>
  <url>http://repo.incanter.org</url>
</repository>
<repository>
  <id>clojure-snapshots</id>
  <url>http://build.clojure.org/snapshots</url>
</repository>
<repository>
  <id>clojars</id>
  <url>http://clojars.org/repo/</url>
</repository>
</repositories>
<dependencies>
  <dependency>
   <groupId>org.clojure</groupId>
   <artifactId>clojure</artifactId>
   <version>${clojure.version}</version>
  </dependency>
  <dependency>
   <groupId>org.clojure</groupId>
   <artifactId>clojure-contrib</artifactId>
   <version>${clojure.version}</version>
  </dependency>
  <dependency>
   <groupId>swank-clojure</groupId>
   <artifactId>swank-clojure</artifactId>
   <version>1.1.0-SNAPSHOT</version>
    <exclusions>
      <exclusion>
        <groupId>org.clojure</groupId>
        <artifactId>clojure</artifactId>
      </exclusion>
      <exclusion>
        <groupId>org.clojure</groupId>
        <artifactId>clojure-contrib</artifactId>
      </exclusion>
    </exclusions>
   </dependency>
  </dependencies>
</project>

What am I not doing right?

4
  • It seems that AOT compilation was not done .. check your jar file and see if the com/mode/mdzw path structure is there or not and there should be a class file called mdzw.class Commented Oct 22, 2012 at 11:18
  • You are right Ankur, there is no mdzw.class file. How can I achieve this? Commented Oct 23, 2012 at 6:01
  • You need to set :aot :all in your clojure project.clj file then do lein jar and then use this newly created jar Commented Oct 23, 2012 at 9:45
  • Thanks Ankur, It seems lein is better than maven. Commented Oct 24, 2012 at 6:05

1 Answer 1

1

I'd recommend using leiningen if you're just getting started, at which point you can try Ankur's advice above or set a :main in project.clj.

(defproject org.enclojure/sample "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.4.0"]]
  :main com.mode.mdzw)

Then just run lein run to launch your program. If you want a jar you can execute w/o lein, build it with lein uberjar.

See the leiningen tutorial for more.

If you want to stick with maven (which I haven't used much), you should probably look at the documentation for the clojure-maven-plugin, which is already in your pom, but maybe not set up right. It provides a clojure:compile goal that probably does what you need.

I suppose the obvious question is, what goal(s) are you running before running java -jar C:\Users\Nahashon\.m2\repository\org\enclojure\sample\0.0.1\sample-0.0.1.jar?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Duelin. I think I will try out lein. It seems it's better than maven for clojure going by the recommendations I have seen on many blogs I visited.
leiningen is simpler, but lots of Clojure projects, including Clojure itself and (I believe) all the contrib projects you see here github.com/clojure and here build.clojure.org use Maven, so you're in good company if you want to stick with Maven.
McKibet, if this was useful you could vote it up and/or mark it as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.