当我尝试编译使用放在包中的类和接口的程序时,我遇到了这个错误。
C:\Users\Florin\Documents\Anul3\PD\Teme\Program\ConvertorValutar>MyAnt.bat
C:\Users\Florin\Documents\Anul3\PD\Teme\Program\ConvertorValutar>set ANT_HOME=c
\Users\Florin\Documents\Anul3\PD\Teme\apache-ant-1.8.1\
C:\Users\Florin\Documents\Anul3\PD\Teme\Program\ConvertorValutar>set JAVA_HOME=
:\Program Files\Java\jdk1.7.0_17
C:\Users\Florin\Documents\Anul3\PD\Teme\Program\ConvertorValutar>c:\Users\Flori
\Documents\Anul3\PD\Teme\apache-ant-1.8.1\\bin\ant.bat
Buildfile: C:\Users\Florin\Documents\Anul3\PD\Teme\Program\ConvertorValutar\bui
d.xml
init:
[mkdir] Created dir: C:\Users\Florin\Documents\Anul3\PD\Teme\Program\Conver
orValutar\build
[mkdir] Created dir: C:\Users\Florin\Documents\Anul3\PD\Teme\Program\Conver
orValutar\build\main
Compile:
[javac] Compiling 6 source files to C:\Users\Florin\Documents\Anul3\PD\Teme
Program\ConvertorValutar\build
[javac] Note: C:\Users\Florin\Documents\Anul3\PD\Teme\Program\ConvertorValu
ar\src\main\server\CursValutar.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
Run:
[java] java.lang.NoClassDefFoundError: server/CursValutarServer (wrong nam
: main/server/CursValutarServer)
[java] at java.lang.ClassLoader.defineClass1(Native Method)
[java] at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
[java] at java.security.SecureClassLoader.defineClass(SecureClassLoade
.java:142)
[java] at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
[java] at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
[java] at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.
ava:482)
[java] Exception in thread "main"
[java] Java Result: 1
BUILD SUCCESSFUL
即使说成功成功,我在尝试运行编译类时遇到错误:
Exception in thread "main" java.lang.NoClassDefFoundError: MyMServer (wrong nam
: main/server/impl/MyMServer)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
build.xml的内容是:
<project name="Socket" default="Run" basedir=".">
<!-- set global properties for this build.dir -->
<property name="main.class" value="server.CursValutarServer"/>
<property name="build.dir" location="build"/>
<property name="src.dir" location="src" />
<property name="host" value="localhost"/>
<property name="port" value="7999"/>
<path id="myclasspath">
<pathelement path="${build.dir}/main"/>
</path>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build.dir directory structure used by compile -->
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.dir}/main" />
</target>
<target name="Compile" depends="init" description="compile the source " >
<javac srcdir="${src.dir}" destdir="${build.dir}"
classpathref="myclasspath" includeantruntime="false"/>
</target>
<target name="Run" depends="Compile">
<java classname="${main.class}" classpathref="myclasspath" fork="true"/>
</target>
<target name="Client">
<java classname="client.CursValutarClient" classpathref="myclasspath" fork="true">
<arg line="${host} ${port}" />
</java>
</target>
</project>
答案 0 :(得分:0)
您正在将类编译到“build”目录中:
<javac srcdir="${src.dir}" destdir="${build.dir}" ..
但是类路径设置为“build / main”:
<path id="myclasspath">
<pathelement path="${build.dir}/main"/>
</path>