从 cmd 执行时出现 java.lang.ClassNotFoundException
我在一个文本文件中编写了以下代码并将其保存为Exercise1.java:
import java.util.*;
public class Exercise1
{
public static void main(String[] args)
{
char myChar;
int myInt;
System.out.println(myChar);
System.out.println(myInt);
}
}
然后在cmd中我编写
javac D:\Projects\Exercise1.java
了两个错误,均表示声明的变量可能未初始化。但是如果我初始化变量,上面的代码会生成“Exercise1.class”文件。然后,当我编写时,
java D:\Projects\Exercise1
我得到 java.lang.ClassNotFoundException。现在我想知道的是:
- Java 中的原始类型是否没有初始化为默认值(我认为并读到它们确实如此)。
- 为什么我不能运行我的程序并得到异常,因为似乎没有任何错误?
I've written the below code in a text file and saved it as Exercise1.java:
import java.util.*;
public class Exercise1
{
public static void main(String[] args)
{
char myChar;
int myInt;
System.out.println(myChar);
System.out.println(myInt);
}
}
then in cmd I write
javac D:\Projects\Exercise1.java
I get 2 errors both saying the declared variables might not be initialized. But if I initialize the variables the above code generates the "Exercise1.class" file. And then when I write
java D:\Projects\Exercise1
I get java.lang.ClassNotFoundException. Now what I wonder are:
- Aren't primitive types initialized to default values in Java (I thought and read they did).
- Why can't I run my program and get the Exception as there do not not seem to be any mistakes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
java
命令不采用文件名 - 它采用类名。你的类的名称只是“Exercise1” - 如果它在包中,它可能类似于“foo.bar.Exercise1”。然后,您单独指定类路径来告诉JVM如何查找类,例如...或者您可以更改目录并利用这样的事实:如果没有指定类路径,JVM将包含当前目录在类路径中:
编辑:我假设您已经修复了代码中的错误。在编译没有错误之前,不要尝试运行代码。这些错误是由于未初始化的局部变量造成的。仅实例和静态变量被赋予默认值。
The
java
command doesn't take a filename - it takes a classname. The name of your class is just "Exercise1" - if it were in a package, it might be something like "foo.bar.Exercise1". You then separately specify the classpath to tell the JVM how to find classes, e.g.... or you can change directory and take advantage of the fact that without a classpath specified, the JVM will include the current directory in the classpath:
EDIT: I'd assumed you'd already fixed the errors in the code. Don't try to run the code before it compiles without errors. The errors are due to your uninitialized local variables. Only instance and static variables are given default values.
不确定您是如何编译程序的,它应该在以下两行处失败:
因为您没有给出任何值。
然后,尽管您可以执行此行来编译:
如果您不更改类路径,您应该位于 Projects 文件夹中,然后运行,
以便打印 myChar 和 myInt 的值
Not sure how you compiled your program, it should failed at the two following lines:
Because you haven't given any values.
Then, although you can execute this line to compile:
If you do not change the classpath, you should be in the Projects folder, and then run
so it prints the values of myChar and myInt
@Jon:到目前为止他还没有声明任何包裹。
最常见的问题是类路径问题。尝试更改该目录的路径,然后编译。或者尝试从 set classpat=address_of 目录更改类路径
您必须初始化一个“本地”变量,否则 java 将给出错误。 Java确实为变量提供了默认值,但不为局部变量提供默认值
http://download.oracle.com/javase/tutorial/java/nutsandbolts /datatypes.html
@Jon : He hasn't declared any packages as of now.
The most common problem is that classpath issue. Try changing the path to the said directory and then compile. Or try changing the class path from set classpat=address_of directory
You have to initialize a "local" variable else java will give error. Java does provide the default value to the variables but not for local variables
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
回答你的第一点:
只有当类的对象被实例化时,Java 才会初始化为默认值。由于这里您没有实例化类,因此它们没有初始化。以下代码将变量初始化为默认值:
这里我没有初始化变量,但我已经实例化了练习类。实例化时,变量的值设置为默认值。如果你是java新手并且对类不熟悉,我建议你阅读一些关于Java的好书。
要回答第二个问题,您可以将 CLASSPATH 和 PATH 添加到环境变量中。为此,请转到系统属性->高级->环境变量。
to answer your first point:
Java does initialized to the default values only if the object of a class is instantiated. Since here you are not instantiating a class, they are not initialized. The following code will initialize the variables to the default values:
Here i have not initialized the variables but i have instantiated the exercise class. On instantiation, the values of the variables are set to the default values. If you are new to java and you are not familiar with classes, i suggest you read up some good books on Java.
To answer your second question, you can add your CLASSPATH and PATH to the environment variables. To do this go to system properties->advanced->environment variables.
将局部变量更改为静态实例变量后,我可以获取类文件,而无需将它们显式初始化为某些值并运行程序并打印默认值。感谢大家的努力。
After changing my local variables to static instance variables I could both able to get class file without having to explicitly initialize them to some values and run the program and get the default values printed. Thank you all for you effort.