Java NoClassDefFoundError 没有任何类名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2159006/
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
NoClassDefFoundError without any class name
提问by Seagull
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
我正在尝试从 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.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 ! ! !
现在我正在尝试从 ant jar 运行一个 ant 类,并且我使用“classpathref”属性指定了此类文件所在的类路径,但是我仍然收到此消息。我检查了 ant jar 以检查清单,并且正确指定了“主”类(它是“org.apache.tools.ant.launch.Launcher”)。我已经耗尽了我所有的资源。请帮忙 !!!
ps: My environment is Eclipse on Ubuntu 9.04
ps:我的环境是Ubuntu 9.04上的Eclipse
回答by akf
From this line:
从这一行:
[java] Could not find the main class: . Program will exit.
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.
看起来好像您对 java.exe 的调用正在寻找.
它需要类名的地方。也许您试图在命令行上指示类路径,但忽略了以-cp
or-classpath
标志作为开头。
回答by Alb
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
这可能是一个误导性错误,实际上与类路径中缺少的类无关。如果您使用的是 Tomcat,可能是因为 $CATALINA_BASE/conf 中缺少 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 安装配置错误,请检查您的 JAVA_HOME 和 ANT_HOME 环境变量或尝试另一个 ant 安装。
回答by kritzikratzi
Most likely your classpath is misconfigured.
很可能您的类路径配置错误。
At a minimum the CLASSPATH should include:
CLASSPATH 至少应包括:
- ant.jar and ant-launcher.jar
- jars/classes for your XML parser
- the JDK's required jar/zip files
- ant.jar 和 ant-launcher.jar
- XML 解析器的 jars/classes
- JDK 所需的 jar/zip 文件
(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(执行相同的 build.xml)。也许您想设置“dir”属性。
回答by Ernani Joppert
Ant launcher expects the following params
Ant 启动器需要以下参数
java -Dant.home=c:\ant org.apache.tools.ant.launch.Launcher [options] [target]
I am affraid we can't proceed answering you if you don't paste your whole build.xml file.
如果您不粘贴整个 build.xml 文件,我恐怕我们无法继续回答您。
Just try to give your full sample as below:
只需尝试提供您的完整样本,如下所示:
<java
classname="org.apache.tools.ant.launch.Launcher"
fork="true"
failonerror="true"
dir="${sub.builddir}"
timeout="4000000"
taskname="startAnt"
>
<classpath>
<pathelement location="${ant.home}/lib/ant-launcher.jar"/>
</classpath>
<arg value="-buildfile"/>
<arg file="${sub.buildfile}"/>
<arg value="-Dthis=this"/>
<arg value="-Dthat=that"/>
<arg value="-Dbasedir=${sub.builddir}"/>
<arg value="-Dthe.other=the.other"/>
<arg value="${sub.target}"/>
</java>
This would be extremely helpful to provide you with a possible misunderstanding.
这对于为您提供可能的误解非常有帮助。
Hope this helps,
希望这可以帮助,
Ernani
埃尔纳尼
回答by Luke Woodward
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:
看起来 Ant 任务正在尝试运行 Java,但以某种方式将空字符串作为要运行的类的名称传递给 JVM。如果我直接使用带引号的空字符串运行 JVM,我可以获得相同的堆栈跟踪:
C:\>java "" Exception in thread "main" java.lang.NoClassDefFoundError: Caused by: java.lang.ClassNotFoundException: at java.net.URLClassLoader.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: . Program will exit.
(This is on Windows, but I don't think that makes much of a difference.)
(这是在 Windows 上,但我认为这没有太大区别。)
I can only suggest following up on Alexander Pogrebnyak's comment to akf's answer. Perhaps the webtest.lib
property has spaces in it?
我只能建议跟进 Alexander Pogrebnyak 对 akf 回答的评论。也许该webtest.lib
物业有空间?
Also, is there a good reason for calling ant
directly via java, rather than using the anttask?
另外,是否有充分的理由ant
直接通过 java调用,而不是使用ant任务?
回答by Jason
When in doubt, invoke ant -v and watch all your variable declarations, and the whole commandline sent to Java.
如有疑问,请调用 ant -v 并观察所有变量声明,以及发送到 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.
某些类似路径的数量会被急切地评估,而另一些则被懒惰地评估。当我的 Ant 脚本打算创建一个供以后任务使用的 jar 时,我在使用前者时遇到了很多问题。然后当我调用这个调用时,它已经从类路径中删除了我的 jar。
If I had to make a wild guess, I'd bet your commandline looked something like:
如果我不得不做一个疯狂的猜测,我敢打赌你的命令行看起来像:
java ... -classpath org.apache.tools.ant.launch.Launcher
java ... -classpath org.apache.tools.ant.launch.Launcher
instead of
代替
java ... -classpath foo.jar;bar.jar org.apache.tools.ant.launch.Launcher
java ... -classpath foo.jar;bar.jar org.apache.tools.ant.launch.Launcher
like you expected
像你预期的那样
回答by Langen Mallya
https://blogs.oracle.com/sreekanth/entry/java_lang_noclassdeffounderror_org_codehaus
https://blogs.oracle.com/sreekanth/entry/java_lang_noclassdeffounderror_org_codehaus
java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
By sreekanth on Nov 23, 2010
Recently when I am trying to run some Maven scripts, I am getting this exception:
最近,当我尝试运行一些 Maven 脚本时,出现此异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.classworlds.launcher.Launcher
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.codehaus.plexus.classworlds.launcher.Launcher. Program will exit.
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.
在花了一些时间尝试各种组合之后,我发现这是因为我在我的环境变量中设置了 M2_HOME 和 M3_HOME。一旦我从我的环境变量中删除了 M2_HOME,我就可以让它再次工作。也许这可以节省一些严重的一些时间。
回答by Roman Svystun
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.
我最近遇到了类似的问题。罪魁祸首是 java 任务下的 2 个标签,它们没有设置它们的值,因此它们导致 2 个空命令参数,最后在命令行中出现 2 个空格。出于某种原因,Unix 不能正确处理它。Red Hat 5 和 Ubuntu 都显示相同的错误。在 Windows 7 上没问题。将这些参数设置为默认虚拟值解决了这个问题。