ClassNotFoundException 与 ant 的 java 任务和类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1129449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:17:28  来源:igfitidea点击:

ClassNotFoundException with ant's java task and classpath

javaantclasspath

提问by denchr

I am trying to compile and run a simple java class within eclipse. The compile task works fine, and since I do not specify a destination folder the build files are in the same directory as the source. Which is alright, at the moment all I need is to learn how I can run the class with the main() method.

我正在尝试在 eclipse 中编译和运行一个简单的 java 类。编译任务工作正常,因为我没有指定目标文件夹,所以构建文件与源文件位于同一目录中。没关系,目前我只需要学习如何使用 main() 方法运行该类。

I have tried using the fully qualified name of the class (with package name, etc) and the classname alone, but always I get a java.lang.ClassNotFoundException

我曾尝试使用类的完全限定名称(带有包名等)和类名,但我总是得到 java.lang.ClassNotFoundException

    Buildfile: C:\Users....\build.xml
    run:
         [java] java.lang.NoClassDefFoundError: code/control/MyClass
         [java] Caused by: java.lang.ClassNotFoundException: code.control.MyClass
         [java]     at java.net.URLClassLoader.run(Unknown Source)
         [java]     at java.security.AccessController.doPrivileged(Native Method)
         [java]     at java.net.URLClassLoader.findClass(Unknown Source)
         [java]     at java.lang.ClassLoader.loadClass(Unknown Source)
         [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
         [java]     at java.lang.ClassLoader.loadClass(Unknown Source)
         [java]     at java.lang.ClassLoader.loadClassInternal(Unknown Source)
         [java] Could not find the main class: code.control.MyClass.  Program will exit.
         [java] Exception in thread "main" 
         [java] Java Result: 1
    compile:
    default:
    BUILD SUCCESSFUL
 Total time: 234 milliseconds

Below, are the targets taken from my build.xml file:

下面是从我的 build.xml 文件中获取的目标:

<target name="default" depends="compile" description="learn">

</target>

  <target name="compile" depends="run">
            <javac srcdir="src/" />
   </target>

  <target name="run">
  <java classname="code.control.MyClass" fork="true"/>
</target>

I can't figure out why the class is not found. MyClass contains the main() method and since i specify no classpath it should look at the current directory, which is the src/ right?

我无法弄清楚为什么找不到课程。MyClass 包含 main() 方法,因为我没有指定类路径,所以它应该查看当前目录,即 src/ 对吗?

The development directory is the usual eclipse file structure:

开发目录是通常的eclipse文件结构:

projectName/src/code/control/MyClass

项目名称/src/代码/控件/MyClass

If it is a classpath problem how could I resolve it? I always had problem grasping the concept "put it on your classpath" ... If someone could provide a little bit of explanation with the classpath in the ant context, I would be very thankful.

如果是类路径问题,我该如何解决?我总是无法理解“将它放在你的类路径上”的概念......如果有人能在 ant 上下文中对类路径提供一点解释,我将非常感激。

Thanks for any help on this. The version of ant is 1.7.0

感谢您对此的任何帮助。蚂蚁的版本是1.7.0

回答by seth

The classpathis where the Java runtime looks for your .class files, similar to how your OS uses the PATH variable to find executables.

类路径是Java运行时的外观为您的.class文件,类似于你的操作系统如何使用PATH变量找到可执行文件。

Try this in your build script:

在你的构建脚本中试试这个:

   <target name="run">
    <java fork="true" classname="code.control.MyClass">
        <classpath>
            <path location="src/"/>
        </classpath>
    </java>

There's a HelloWorld version for antthat walks through building a Java program with ant.

有一个适用于 antHelloWorld 版本,它介绍了如何使用 ant 构建 Java 程序。

回答by janetsmith

you should include classpath, e.g.

你应该包括类路径,例如

<java classpath="${bin}" classname="code.control.MyClass">

where ${bin} is your output folder.

其中 ${bin} 是您的输出文件夹。

回答by sravan

change your build.xml as below and try out:

如下更改您的 build.xml 并尝试:

<target name="default" depends="run" description="learn">

</target>

  <target name="compile" >
            <javac srcdir="src/" />
   </target>

  <target name="run" depends="compile">
  <java classname="code.control.MyClass" fork="true"/>
</target>