没有任何类名的 NoClassDefFoundError
我正在尝试从 ant 运行 java 任务。我正在尝试运行“org.apache.tools.ant.launch.Launcher”类。我不断收到“NoClassDefFoundError”,但没有指定任何类名。我还收到“ClassNotFoundException”,并显示消息“无法找到主类:程序将退出”。这是错误的片段
[java] Exception in thread "main" java.lang.NoClassDefFoundError:
[java] Caused by: java.lang.ClassNotFoundException:
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
[java] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
[java] Could not find the main class: . Program will exit.
[java] Java Result: 1
现在我尝试从 ant jar 运行一个 ant 类,并使用“classpathref”属性指定该类文件所在的类路径,但是我仍然收到此消息。我检查了 ant jar 以检查清单,并且“主”类已正确指定(它是“org.apache.tools.ant.launch.Launcher”)。我已经用尽了我所有的资源。请帮忙! ! !
ps:我的环境是Ubuntu 9.04上的Eclipse
I am trying to run a java task from ant. I am trying to run the "org.apache.tools.ant.launch.Launcher" class. I keep on getting the "NoClassDefFoundError" without any class name being specified. I am also getting a "ClassNotFoundException" along with that displaying a message "Could not find the main class: . Program will exit". Here's a snippet of the error
[java] Exception in thread "main" java.lang.NoClassDefFoundError:
[java] Caused by: java.lang.ClassNotFoundException:
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
[java] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
[java] Could not find the main class: . Program will exit.
[java] Java Result: 1
Now I am trying to run an ant class from an ant jar and i specifiy the classpath where this class file resides using the "classpathref" attribute, however I still get this message. I checked the ant jar to check the Manifest and the "main" class is specified properly (it's "org.apache.tools.ant.launch.Launcher") . I have exhausted all my resources. Please help ! ! !
ps: My environment is Eclipse on Ubuntu 9.04
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如有疑问,请调用 ant -v 并观察所有变量声明以及发送到 Java 的整个命令行。
某些类似路径的量会被急切地求值,而另一些则被延迟地求值。当我的 Ant 脚本打算创建一个供后续任务使用的 jar 时,我在使用前者时遇到了很多问题。然后,当我调用该调用时,它已经从类路径中删除了我的 jar。
如果我不得不做出疯狂的猜测,我敢打赌你的命令行看起来像:
java ... -classpath org.apache.tools.ant.launch.Launcher
而不是
java ... -classpath foo.jar;bar。 jar org.apache.tools.ant.launch.Launcher
像你期望的那样
When in doubt, invoke ant -v and watch all your variable declarations, and the whole commandline sent to Java.
Certain path-like quantities are eagerly evaluated, while others are lazily evaluated. I've had plenty of problems where I used one of the former, when my Ant script intended to create a jar that would be used by a later task. Then by the time I invoked the call, it had already pruned my jar from the classpath.
If I had to make a wild guess, I'd bet your commandline looked something like:
java ... -classpath org.apache.tools.ant.launch.Launcher
instead of
java ... -classpath foo.jar;bar.jar org.apache.tools.ant.launch.Launcher
like you expected
我最近也遇到了类似的问题。罪魁祸首是java任务下的2个标签,它们没有设置它们的值,所以它们导致了2个空命令参数,最后在命令行中出现了2个空格。
由于某种原因,Unix 不能正确处理它。 Red Hat 5 和 Ubuntu 都显示相同的错误。在 Windows 7 上没问题。
将这些参数设置为默认虚拟值解决了该问题。
I had a similar problem recently. The culprits were 2 tags under the java task, that didn't have their values set, so they resulted in 2 empty command arguments and in the end in 2 spaces in the command line.
For some reason Unix doesn't handle it right. Both Red Hat 5 and Ubuntu displayed the same error. It was OK on Windows 7.
Setting those arguments to have default dummy values solved the issue.
您的类路径很可能配置错误。
CLASSPATH 至少应包括:
(来自 ant 手册)
此外,您似乎正在当前目录中重新启动 ant(执行相同的 build.xml)。也许您需要设置“dir”属性。
Most likely your classpath is misconfigured.
At a minimum the CLASSPATH should include:
(from the ant manual)
Also you seem to be relaunching ant in the current directory (executing the same build.xml). Maybe you'll want to set the "dir" property.
看起来 Ant 任务正在尝试运行 Java,但以某种方式将空字符串传递给 JVM 作为要运行的类的名称。如果我直接使用带引号的空字符串运行 JVM,我可以获得相同的堆栈跟踪:(
这是在 Windows 上,但我认为这没有多大区别。)
我只能建议跟进 Alexander Pogrebnyak 对 akf 的评论回答。也许
webtest.lib
属性中有空格?另外,是否有充分的理由直接通过java调用
ant
,而不是使用ant 任务?It looks like the Ant task is trying to run Java, but is somehow passing an empty string to the JVM as the name of the class to run. I can get the same stacktrace if I run the JVM directly with a quoted empty string:
(This is on Windows, but I don't think that makes much of a difference.)
I can only suggest following up on Alexander Pogrebnyak's comment to akf's answer. Perhaps the
webtest.lib
property has spaces in it?Also, is there a good reason for calling
ant
directly via java, rather than using the ant task?我
最近当 尝试运行一些 Maven 脚本,我遇到了这个异常:
花了一些时间尝试各种组合后,我发现这是因为我在环境变量中设置了 M2_HOME 和 M3_HOME。一旦我从环境变量中删除了 M2_HOME,我就可以让这一切再次恢复正常。也许这可以为某些人节省一些宝贵的时间。
https://blogs.oracle.com/sreekanth/entry/java_lang_noclassdeffounderror_org_codehaus
Recently when I am trying to run some Maven scripts, I am getting this exception:
After spending some time trying various combinations , I found that this is because I have both M2_HOME and M3_HOME set in my environment variables.Once I removed M2_HOME from my environment variables, I could get this working back again.May be this could save some serious time for some one.
这可能是一个误导性错误,实际上与类路径中缺少的类无关。如果您使用 Tomcat,可能是由于 $CATALINA_BASE/conf 中缺少 conf 文件,
也可能是 ant 安装配置错误,请检查您的 JAVA_HOME 和 ANT_HOME 环境变量或尝试其他 ant 安装。
This can be a misleading error that is not actually about a class missing from the classpath. If you are using Tomcat it can be due to missing conf files in $CATALINA_BASE/conf
It could also be a misconfigured ant installation, please check your JAVA_HOME and ANT_HOME env variables or try another ant installation.
Ant 启动器需要以下参数,
如果您不粘贴整个 build.xml 文件,恐怕我们无法继续回答您。
请尝试提供如下完整示例:
这对于消除可能的误解非常有帮助。
希望这有帮助,
埃尔纳尼
Ant launcher expects the following params
I am affraid we can't proceed answering you if you don't paste your whole build.xml file.
Just try to give your full sample as below:
This would be extremely helpful to provide you with a possible misunderstanding.
Hope this helps,
Ernani
从这一行来看:
您对 java.exe 的调用似乎在需要类名的地方找到了
.
。也许您试图在命令行上指示类路径,但忽略了使用-cp
或-classpath
标志作为前缀。From this line:
it looks as though your call to java.exe is finding a
.
where it expects a class name. Perhaps you are trying to indicate the classpath on the commandline but are neglecting to preface that with the-cp
or-classpath
flag.