我在 Ant 中有一个问题一直困扰着我,我似乎没有找到解决方案。我在java中有一个RMI项目,已经完成,and文件如下:
<project name="StockageRMI" default="compile">
<target name="compile">
<javac destdir="classes">
<src path="src/db/"/>
<classpath>
<fileset dir="lib/">
<include name="**/*.jar" />
</fileset>
<fileset dir="classes/">
<include name="**/*.class" />
</fileset>
</classpath>
</javac>
<javac destdir="classes">
<src path="src/stockage"/>
<classpath>
<fileset dir="lib/">
<include name="**/*.jar" />
</fileset>
<fileset dir="classes/">
<include name="**/*.class" />
</fileset>
</classpath>
</javac>
<javac destdir="classes">
<src path="src/server"/>
<classpath>
<fileset dir="lib/">
<include name="**/*.jar" />
</fileset>
<fileset dir="classes/">
<include name="**/*.class" />
</fileset>
</classpath>
</javac>
<javac destdir="classes">
<src path="src/client"/>
<classpath>
<fileset dir="lib/">
<include name="**/*.jar" />
</fileset>
<fileset dir="classes/">
<include name="**/*.class" />
</fileset>
</classpath>
</javac>
</target>
<target name="rmic" depends="compile">
<rmic classname="stockage.StockageImpl" base="classes/">
<classpath>
<fileset dir="lib/">
<include name="**/*.jar" />
</fileset>
<fileset dir="classes/">
<include name="**/*.class" />
</fileset>
</classpath>
</rmic>
</target>
<target name="runServer" depends="rmic,runStockage">
<java classname="server.ServerImpl" fork="true" failonerror="true" >
<classpath>
<fileset dir="lib/">
<include name="**/*.jar" />
</fileset>
<fileset dir="classes/">
<include name="**/*.class" />
</fileset>
</classpath>
</java>
</target>
<target name="runStockage">
<java classname="stockage.StockageImpl" fork="true" failonerror="true" >
<classpath>
<fileset dir="lib/">
<include name="**/*.jar" />
</fileset>
<fileset dir="classes/">
<include name="**/*.class" />
</fileset>
</classpath>
</java>
</target>
</project>
目标“编译”和“rmic”工作得很好,问题是当我尝试运行“runStockage”时。我收到错误:
Exception in thread "main" java.lang.NoClassDefFoundError: stockage/StockageImpl
[java] Caused by: java.lang.ClassNotFoundException: stockage.StockageImpl
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[java] Java Result: 1
这很奇怪,因为如果我尝试在终端中执行此操作,java 命令可以正常工作,并且它具有与 javac 相同的类路径,因为它具有相同的依赖项:
javac -cp lib/mongo-java-driver-2.11.1.jar:lib/rmiio-2-1.0.0.jar:lib/commons-logging-1.1.3.jar:classes -d classes src/stockage/StockageImpl.java
rmic -classpath lib/mongo-java-driver-2.11.1.jar:lib/rmiio-2-1.0.0.jar:lib/commons-logging-1.1.3.jar:classes -d classes stockage.StockageImpl
java -classpath lib/mongo-java-driver-2.11.1.jar:lib/rmiio-2-1.0.0.jar:lib/commons-logging-1.1.3.jar:classes server.ServerImpl
上述三个命令都可以正常工作,问题出在 ant 中。如您所见,javac、java 和 rmic 使用相同的依赖项和 jar 文件,所以我在 Ant 中做了同样的事情,但它不起作用。我确信这中间有些愚蠢,但在过去的 6 个小时里我似乎无法弄清楚。有人有线索吗?