2010-12-04 69 views
2

我写学校的在Java项目(离散仿真库),当我运行示例应用程序我仍然得到这个错误:错误的类路径?为什么我仍然得到NoClassDefFoundError?

[java] Exception in thread "main" java.lang.NoClassDefFoundError: /home/lut/Plocha/IMS/examples/Random/random/RandomTest 
[java] Caused by: java.lang.ClassNotFoundException: .home.lut.Plocha.IMS.examples.Random.random.RandomTest 
[java]  at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
... 
[java]  at java.lang.ClassLoader.loadClass(ClassLoader.java:248) 
[java] Could not find the main class: /home/lut/Plocha/IMS/examples/Random/random.RandomTest. Program will exit. 
[java] Java Result: 1 

(/家/ LUT/Plocha/IMS是根目录在这里的文件树中,它被标记为 - 点)

我在根目录中有库。还有目录examples,其中是使用此库的小型示例应用程序,每个应用程序都有它自己的构建ant脚本。

的目录结构:

. 
├── build.xml <-- library build-file, examples are not included in this build 
├── doc 
├── examples 
│   ├── Random 
│   │   ├── build.xml <-- example app buil-file 
│   │   └── src 
│   │    └── random 
│   │     └── RandomTest.java <-- main class of the example 
│  └── Calendar 
│  ├── build.xml, src, etc. inside 
│  ... 
├── dest  <-- after library build, dsim.jar is here 
├── build 
└── src  <-- source files of the library 
    └── ims 
     ├── Distribution 
     │   └── RandomGenerator.java 
     ├── Exception 
     │   ├── RandomException.java 
     │   └── SimulationException.java 
     └── SimObjects 
      ├── Entity.java 
      ├── Event.java 
      ├── Facility.java 
      ... 

我可以建立两个主库(输出dsim.jar./dest文件夹),例如应用程序(在./examples/Random)没有问题,但是当我再尝试运行的例子应用程序(ant run),我仍然收到NoClassDefFoundError

该示例应用的build.xml(在./examples/Random)看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<project name="Example - Random generator" default="compile" basedir="."> 

    <property name="src" location="src"/> 
    <property name="build"  location="build"/> 
    <property name="dest"  location="dest"/> 
    <property name="dsim.dir" location="../../dest"/> 
    <property name="dest.jar" location="${dest}/random.jar"/> 
    <property name="main-class" location="random.RandomTest"/> 

    <path id="dsim-classpath"> 
    <fileset dir="${dsim.dir}"> 
     <include name="*.jar"/> 
    </fileset> 
    </path> 

    <target name="prepare" description="Create necesery directories"> 
    <mkdir dir="${build}"/> 
    <mkdir dir="${dest}"/> 
    </target> 

    <target name="compile" depends="prepare"> 
    <javac srcdir="${src}" destdir="${build}" encoding="utf8"> 
     <classpath refid="dsim-classpath"/> 
    </javac>  

    <jar destfile="${dest.jar}" basedir="${build}"> 
     <manifest> 
     <attribute name="Main-Class" value="${main-class}"/> 
     </manifest> 
    </jar> 
    </target> 

    <target name="run" depends="compile"> 
    <java jar="${dest.jar}" fork="true" classpathref="dsim-classpath"/> 
    </target> 
</project> 

RandomTest.java文件,行开头:关于类路径

package random; 

import ims.Distribution.RandomGenerator; 
import ims.Exception.SimulationException; 
import ims.SimObjects.Histogram; 

public class RandomTest { 
    public static void main(String[] args) throws SimulationException { 

我试着在谷歌搜索,有几个职位和它的问题,但即使我无法确定哪里可能是问题。我究竟做错了什么?我想念什么?

非常感谢。

+0

提示:用`jar xf jarfile`提取你的jar并检查META-INF/manifest.mf。它在主流中说什么? – extraneon 2010-12-04 22:24:49

+0

Main-Class:/home/lut/Plocha/IMS/examples/Random/random.RandomTest -/home/lut/Plocha/IMS是根目录(在文件树中标记为。 - dot) – Martin 2010-12-04 22:37:55

回答

2

班级名称为random.RandomTest

这是什么?

.home.lut.Plocha.IMS.examples.Random.random.RandomTest 

为什么类加载器在寻找这个?

这里有一个通用的build.xml,对我来说很好。创建目录结构,看看它是否还是你出来:

<?xml version="1.0" encoding="UTF-8"?> 
<project name="xslt-converter" basedir="." default="package"> 

    <property name="version" value="1.6"/> 
    <property name="haltonfailure" value="no"/> 

    <property name="out" value="out"/> 

    <property name="production.src" value="src"/> 
    <property name="production.lib" value="lib"/> 
    <property name="production.resources" value="config"/> 
    <property name="production.classes" value="${out}/production/${ant.project.name}"/> 

    <property name="test.src" value="test"/> 
    <property name="test.lib" value="lib"/> 
    <property name="test.resources" value="config"/> 
    <property name="test.classes" value="${out}/test/${ant.project.name}"/> 

    <property name="exploded" value="out/exploded/${ant.project.name}"/> 
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/> 
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/> 

    <property name="reports.out" value="${out}/reports"/> 
    <property name="junit.out" value="${reports.out}/junit"/> 
    <property name="testng.out" value="${reports.out}/testng"/> 

    <path id="production.class.path"> 
     <pathelement location="${production.classes}"/> 
     <pathelement location="${production.resources}"/> 
     <fileset dir="${production.lib}"> 
      <include name="**/*.jar"/> 
      <exclude name="**/junit*.jar"/> 
      <exclude name="**/*test*.jar"/> 
     </fileset> 
    </path> 

    <path id="test.class.path">        
     <path refid="production.class.path"/> 
     <pathelement location="${test.classes}"/> 
     <pathelement location="${test.resources}"/> 
     <fileset dir="${test.lib}"> 
      <include name="**/junit*.jar"/> 
      <include name="**/*test*.jar"/> 
     </fileset> 
    </path> 

    <path id="testng.class.path"> 
     <fileset dir="${test.lib}"> 
      <include name="**/testng*.jar"/> 
     </fileset> 
    </path> 

    <available file="${out}" property="outputExists"/> 

    <target name="clean" description="remove all generated artifacts" if="outputExists"> 
     <delete dir="${out}" includeEmptyDirs="true"/> 
     <delete dir="${reports.out}" includeEmptyDirs="true"/> 
    </target> 

    <target name="create" description="create the output directories" unless="outputExists"> 
     <mkdir dir="${production.classes}"/> 
     <mkdir dir="${test.classes}"/> 
     <mkdir dir="${reports.out}"/> 
     <mkdir dir="${junit.out}"/> 
     <mkdir dir="${testng.out}"/> 
     <mkdir dir="${exploded.classes}"/> 
     <mkdir dir="${exploded.lib}"/> 
    </target> 

    <target name="compile" description="compile all .java source files" depends="create"> 
     <!-- Debug output 
       <property name="production.class.path" refid="production.class.path"/> 
       <echo message="${production.class.path}"/> 
     --> 
     <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}"> 
      <classpath refid="production.class.path"/> 
      <include name="**/*.java"/> 
      <exclude name="**/*Test.java"/> 
     </javac> 
     <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}"> 
      <classpath refid="test.class.path"/> 
      <include name="**/*Test.java"/> 
     </javac> 
    </target> 

    <target name="junit-test" description="run all junit tests" depends="compile"> 
     <!-- Debug output 
       <property name="test.class.path" refid="test.class.path"/> 
       <echo message="${test.class.path}"/> 
     --> 
     <junit printsummary="yes" haltonfailure="${haltonfailure}"> 
      <classpath refid="test.class.path"/> 
      <formatter type="xml"/> 
      <batchtest fork="yes" todir="${junit.out}"> 
       <fileset dir="${test.src}"> 
        <include name="**/*Test.java"/> 
       </fileset> 
      </batchtest> 
     </junit> 
     <junitreport todir="${junit.out}"> 
      <fileset dir="${junit.out}"> 
       <include name="TEST-*.xml"/> 
      </fileset> 
      <report todir="${junit.out}" format="frames"/> 
     </junitreport> 
    </target> 

    <taskdef resource="testngtasks" classpathref="testng.class.path"/> 
    <target name="testng-test" description="run all testng tests" depends="compile"> 
     <!-- Debug output 
       <property name="test.class.path" refid="test.class.path"/> 
       <echo message="${test.class.path}"/> 
     --> 
     <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50"> 
      <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/> 
     </testng> 
    </target> 

    <target name="exploded" description="create exploded deployment" depends="testng-test"> 
     <copy todir="${exploded.classes}"> 
      <fileset dir="${production.classes}"/> 
     </copy> 
     <copy todir="${exploded.lib}"> 
      <fileset dir="${production.lib}"/> 
     </copy> 
    </target> 

    <target name="package" description="create package file" depends="exploded"> 
     <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/> 
    </target> 

</project> 
1

好了,所以这个问题是不良资产属性附加伤害名字(我有位置,而不是在主类属性的定义值)。

<property name="main-class" location="random.RandomTest"/> 
<!-- shoudl be --> 
<property name="main-class" value="random.RandomTest"/> 

然后加载程序找到主类,但另一个问题出现了:装载机无法找到库类(在./src/类)

[java] java.lang.NoClassDefFoundError: ims/Exception/SimulationException 
[java] Caused by: java.lang.ClassNotFoundException: ims.Exception.SimulationException 

但为什么呢?有类路径引用。或者它是jar和classpath属性的排他性问题(评论在第一个@ duffymo的答案下)?如何解决我的build.xml使其工作? 谢谢。