运行clojure jar时ClassNotFoundException

时间:2012-10-22 09:29:16

标签: clojure netbeans-6.9 enclojure

我正在尝试运行一个clojure jar文件,我收到以下错误:

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.

使用maven on netbeans 6.9.1创建了clojure项目。而clojure代码如下:

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

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

POM文件如下:

<?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>

我做得不对?

1 个答案:

答案 0 :(得分:1)

如果您刚入门,我建议您使用leiningen,此时您可以尝试Ankur's advice above或在project.clj中设置:main

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

然后只需运行lein run即可启动您的程序。如果你想要一个jar你可以执行w / o lein,用lein uberjar构建它。

有关详情,请参阅leiningen tutorial

如果你想坚持使用maven(我没有用太多),你应该查看clojure-maven-plugin的文档,它已经在你的pom中,但可能没有正确设置。它提供clojure:compile目标,可能会满足您的需求。

我认为显而易见的问题是,在运行java -jar C:\Users\Nahashon\.m2\repository\org\enclojure\sample\0.0.1\sample-0.0.1.jar之前,您的目标是什么?