我正在用 Java 编写学校的项目(离散模拟库),但在运行示例应用程序时仍然出现此错误:
[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
(/home/lut/Plocha/IMS 是根目录。在下面的文件树中,它被标记为 . - dot)
我在根目录中有库。还有一个目录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
(in ./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 {
我尝试在谷歌上搜索,有几篇关于类路径及其问题的帖子,但即使我无法确定哪里可能出现问题。我究竟做错了什么?我想念什么?
非常感谢。