如何抛出NoClassDefFoundError [英] How is NoClassDefFoundError thrown

查看:104
本文介绍了如何抛出NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在eclipse中构建了一个名为com.hello的包,我写了一个简单的HelloWorld程序。 Eclipse自动添加package com.hello;在我的程序之上。而HelloWorld.java被放在

  F:\workspace\helloWorld\src\com\hello; 

HelloWorld.class已放入

  F:\workspace\helloWorld\bin\com\hello。 

它在Eclipse中工作得很好。但是当我进入目录 F:\workspace\helloWorld\bin\com\\hello 并使用命令行java HelloWorld时,我得到 NoClassDefFoundError的。我知道它可能与类路径有关。但是我不太确定。

解决方案

你的课程是在一个包 com.hello 。要运行它,您必须确保您的包中的基本目录 F:\workspace\helloWorld\bin 案例,在类路径。



尝试运行如下:

  java -cp F:\workspace\helloWorld\bin com.hello.HelloWorld 

您也可以转到 F:\workspace\helloWorld\bin 目录,然后使用

  java com.hello.HelloWorld 

这将工作,因为Java将使用当前目录作为默认(如果您没有设置 CLASSPATH 环境变量)。


I built a package called "com.hello" in eclipse and I wrote an easy HelloWorld program. Eclipse automatically added "package com.hello;" on top of my program. And HelloWorld.java was put in

F:\workspace\helloWorld\src\com\hello;

HelloWorld.class was put in

F:\workspace\helloWorld\bin\com\hello.

It worked very well in Eclipse. But when I entered the directory "F:\workspace\helloWorld\bin\com\hello" and used command line with "java HelloWorld," I got NoClassDefFoundError. I know it may have something to do with the classpath. But I'm not quite sure.

解决方案

Your class is in a package com.hello. To run it, you must make sure the base directory of the package, which is F:\workspace\helloWorld\bin in your case, is in the classpath.

Try running it like this:

java -cp F:\workspace\helloWorld\bin com.hello.HelloWorld

You can also go to the directory F:\workspace\helloWorld\bin and then run it with

java com.hello.HelloWorld

This will work because Java will use the current directory as the default (if you do not have the CLASSPATH environment variable set).

这篇关于如何抛出NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆