我寫學校的在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 {
我試着在谷歌搜索,有幾個職位和它的問題,但即使我無法確定哪裏可能是問題。我究竟做錯了什麼?我想念什麼?
非常感謝。
提示:用`jar xf jarfile`提取你的jar並檢查META-INF/manifest.mf。它在主流中說什麼? – extraneon 2010-12-04 22:24:49
Main-Class:/home/lut/Plocha/IMS/examples/Random/random.RandomTest -/home/lut/Plocha/IMS是根目錄(在文件樹中標記爲。 - dot) – Martin 2010-12-04 22:37:55