java 我的java代码有什么问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11526948/
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
What's wrong with my java code
提问by Yuncy
I'm learning java. My sample's directory is like the following:
我正在学习java。我的示例目录如下所示:
./src
./src/yxy
./src/yxy/pack
./src/yxy/pack/testf
./src/yxy/pack/testf/Person.java
./Hello.java
./Hello.class./testpack
./testpack/yxy
./testpack/yxy/pack
./testpack/yxy/pack/testf
./testpack/yxy/pack/testf/Person.class
./testpack.jar
./src
./src/yxy
./src/yxy/pack
./src/yxy/pack/testf
./src/yxy/pack/testf/Person.java
./Hello.java
./Hello.class./testpack
./testpack/yxy
./testpack/yxy/pack
./testpack/yxy/pack/testf
./testpack/yxy/pack/testf/Person.class
./testpack.jar
The src direcotory is used as a library. It provides the Person class.
The output of the src is located in testpack direcotory.
I generate a testpack.jar according to the testpack direcotory.
The Hello.java is the main java file. It uses the Person class.
src 目录用作库。它提供了 Person 类。
src 的输出位于 testpack 目录中。
我根据testpack目录生成了一个testpack.jar。
Hello.java 是主要的java 文件。它使用 Person 类。
The Person.javasource code is the following:
的Person.java源代码是下面的:
package yxy.pack.testf;
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
The Hello.javasource code is the following:
的Hello.java源代码是下面的:
import java.io.*; //IOException
import java.util.*; //Enumeration
import yxy.pack.testf.Person;
public class Hello{
public static void main(String[] args)throws IOException {
Person p=new Person("abc");
Class cls=Person.class;
ClassLoader clsLoader=cls.getClassLoader();
String clsFile=cls.getName();
System.out.println("end");
}
}
I use the following method to generate the testpack.jar under the current directory.
我使用以下方法生成当前目录下的testpack.jar。
javac -verbose -d testpack src\yxy\pack\testf\Person.java
cd testpack
jar -cvf testpack.jar ./yxy
move testpack.jar ./../testpack.jar
cd ..
javac -verbose -d testpack src\yxy\pack\testf\Person.java
cd testpack
jar -cvf testpack.jar ./yxy
move testpack.jar ./../testpack.jar
cd ..
I use the following method to build and run the app:
我使用以下方法来构建和运行应用程序:
javac -classpath .\testpack.jar Hello.java
java -classpath .\testpack.jar Hello
javac -classpath .\testpack.jar Hello.java
java -classpath .\testpack.jar 你好
this is the error I get:
这是我得到的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello Caused by: java.lang.ClassNotFoundException: Hello at java.net.URLClassLoader.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) Could not find the main class: Hello. Program will exit.
I don't why this happen. How can I fix?
我不明白为什么会这样。我该如何解决?
java Hello
你好
Exception in thread "main" java.lang.NoClassDefFoundError: yxy/pack/testf/Person at Hello.main(Hello.java:10) Caused by: java.lang.ClassNotFoundException: yxy.pack.testf.Person at java.net.URLClassLoader.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) ... 1 more
How can I fix this?
我怎样才能解决这个问题?
=== Anyway, I found out how to fix. Just use the following in cmd.
=== 无论如何,我找到了解决方法。只需在cmd中使用以下内容。
>java -classpath testpack.jar;. Hello
I must add testpack.jar and current path . to the classpath.
我必须添加 testpack.jar 和 current path 。到类路径。
回答by David Kroukamp
Well it could be many things, If you're a beginner in Java I'd reccomend starting with an IDE, howver if you dont want this then here is a link on 3 ways to resolve NoClassDefFoundError in Java
嗯,这可能有很多事情,如果您是 Java 的初学者,我建议您从 IDE 开始,但是如果您不想要这个,那么这里是解决 Java 中 NoClassDefFoundError 的 3 种方法的链接
Here is also an extract from another user who explains why this error occurs:
以下是另一位用户的摘录,他解释了发生此错误的原因:
I always get that error when I create a class file in a package and then attempt to run the program from within the package. The solution is to go up one level in your file system so that you are on the level of the package, not of the package contents.
For example, if your package's name is greetings and your class file's name is HelloWorld, your program contains the line:
package greetings;
The package name corresponds to a directory in your file system, e.g. C:\Java\greetings. Your class file is within that directory, i.e. its file name is C:\Java\greetings\HelloWorld.class.
If you position to c:\Java\greetings and then execute:
java HelloWorld
you get a NoClassDefFound error. If you go up one directory, in this case to C:\Java, instead of C:\Java\greetings, and then do this:
java greetings.HelloWorld
your program should work for you.
当我在包中创建类文件然后尝试从包中运行程序时,我总是会遇到该错误。解决方案是在您的文件系统中上升一个级别,以便您处于包的级别,而不是包内容的级别。
例如,如果你的包名是greetings,你的类文件名是HelloWorld,你的程序就包含以下行:
包裹问候;
包名对应于文件系统中的一个目录,例如 C:\Java\greetings。您的类文件位于该目录中,即其文件名为 C:\Java\greetings\HelloWorld.class。
如果你定位到 c:\Java\greetings 然后执行:
你好世界
您会收到 NoClassDefFound 错误。如果你上一个目录,在这种情况下是 C:\Java,而不是 C:\Java\greetings,然后执行以下操作:
java问候语.HelloWorld
你的程序应该适合你。